生活随笔
收集整理的這篇文章主要介紹了
寒假每日一题(入门组)【week5 完结】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 426. 開心的金明【DP】
- 703. 數獨檢查【模擬】
- 1101. 獻給阿爾吉儂的花束【bfs】
- 89. a^b【快速冪】
- 433. ISBN號碼【模擬】
- 428. 數列【進制】
- 421. 陶陶摘蘋果
426. 開心的金明【DP】
https://www.acwing.com/problem/content/428/
#include<bits/stdc++.h>
using namespace std
;
int v
[35],w
[35],n
,m
;
int f
[30][30005];
int main(void)
{cin
>>m
>>n
;for(int i
=1;i
<=n
;i
++) cin
>>v
[i
]>>w
[i
];for(int i
=1;i
<=n
;i
++){for(int j
=0;j
<=m
;j
++){f
[i
][j
]=f
[i
-1][j
];if(j
>=v
[i
]) f
[i
][j
]=max(f
[i
][j
],f
[i
-1][j
-v
[i
]]+w
[i
]*v
[i
]);}}cout
<<f
[n
][m
];return 0;
}
703. 數獨檢查【模擬】
https://www.acwing.com/problem/content/705/
#include<bits/stdc++.h>
using namespace std
;
const int N
=110;
int a
[N
][N
],n
;
bool check()
{for(int i
=0;i
<n
*n
;i
++){set
<int>st
;for(int j
=0;j
<n
*n
;j
++){st
.insert(a
[i
][j
]);if(a
[i
][j
]<=0||a
[i
][j
]>n
*n
) return false;}if(st
.size()!=n
*n
) return false;}for(int i
=0;i
<n
*n
;i
++){set
<int>st
;for(int j
=0;j
<n
*n
;j
++){st
.insert(a
[j
][i
]);}if(st
.size()!=n
*n
) return false;}for(int i
=0;i
<n
*n
;i
+=n
){for(int j
=0;j
<n
*n
;j
+=n
){int x
=i
,y
=j
;int xx
=i
+n
-1,yy
=j
+n
-1;if(xx
>=n
*n
||yy
>=n
*n
) continue;set
<int>st
;for(int k1
=x
;k1
<=xx
;k1
++){for(int k2
=y
;k2
<=yy
;k2
++){st
.insert(a
[k1
][k2
]);}}if(st
.size()!=n
*n
) return false;}}return true;
}
int main(void)
{int t
; cin
>>t
;for(int k
=1;k
<=t
;k
++){cin
>>n
;for(int i
=0;i
<n
*n
;i
++)for(int j
=0;j
<n
*n
;j
++) cin
>>a
[i
][j
];printf("Case #%d: ",k
);if(check()) puts("Yes");else puts("No");}return 0;
}
1101. 獻給阿爾吉儂的花束【bfs】
https://www.acwing.com/problem/content/1103/
#include<bits/stdc++.h>
using namespace std
;
const int N
=1010;
int t
,st
[N
][N
],stx
,sty
,edx
,edy
,n
,m
;
int dx
[4]={-1,0,0,1};
int dy
[4]={0,-1,1,0};
string s
[N
];
int bfs(int x
,int y
)
{queue
<pair
<int,int>>q
; q
.push({x
,y
});st
[x
][y
]=0;while(q
.size()){auto temp
=q
.front(); q
.pop();x
=temp
.first
,y
=temp
.second
;if(x
==edx
&&y
==edy
) return st
[x
][y
];for(int i
=0;i
<4;i
++){int tempx
=x
+dx
[i
];int tempy
=y
+dy
[i
];if(tempx
<0||tempx
>=n
||tempy
<0||tempy
>=m
) continue;if(s
[tempx
][tempy
]=='#') continue;if(st
[tempx
][tempy
]!=0x3f3f3f3f) continue;st
[tempx
][tempy
]=st
[x
][y
]+1;q
.push({tempx
,tempy
});}}return 1e9;
}
int main(void)
{cin
>>t
;while(t
--){memset(st
,0x3f,sizeof st
);cin
>>n
>>m
;for(int i
=0;i
<n
;i
++) cin
>>s
[i
];for(int i
=0;i
<n
;i
++){for(int j
=0;j
<m
;j
++){if(s
[i
][j
]=='S') stx
=i
,sty
=j
;if(s
[i
][j
]=='E') edx
=i
,edy
=j
;}}int ans
=bfs(stx
,sty
);if(ans
==1e9) puts("oop!");else cout
<<ans
<<endl
;}return 0;
}
89. a^b【快速冪】
https://www.acwing.com/problem/content/91/
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
LL
quick_mi(LL a
,LL b
,LL p
)
{LL sum
=1;while(b
){if(b
&1) sum
=sum
*a
%p
;b
>>=1;a
=a
*a
%p
;}return sum
%p
;
}
int main(void)
{LL a
,b
,p
; cin
>>a
>>b
>>p
;cout
<<quick_mi(a
,b
,p
);return 0;
}
433. ISBN號碼【模擬】
https://www.acwing.com/problem/content/435/
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{string s
; cin
>>s
;int sum
=0;for(int i
=0,k
=1;i
<s
.size()-1;i
++) if(s
[i
]>='0'&&s
[i
]<='9') sum
=sum
+(s
[i
]-'0')*k
,k
++;sum
=sum
%11;char c
=sum
+'0';if(sum
==10) c
='X';if(c
==s
[s
.size()-1]) puts("Right");else{for(int i
=0;i
<s
.size()-1;i
++) cout
<<s
[i
];cout
<<c
;}return 0;
}
428. 數列【進制】
https://www.acwing.com/problem/content/430/
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{int k
,n
,sum
=0; cin
>>k
>>n
;string s
;while(n
) s
=s
+to_string(n
%2),n
/=2;for(int i
=0,w
=1;i
<s
.size();i
++,w
*=k
) sum
+=(s
[i
]-'0')*w
;cout
<<sum
;
}
421. 陶陶摘蘋果
https://www.acwing.com/problem/content/423/
#include<bits/stdc++.h>
using namespace std
;
const int N
=15;
int a
[N
],n
,cnt
;
int main(void)
{for(int i
=0;i
<10;i
++) cin
>>a
[i
];cin
>>n
;for(int i
=0;i
<10;i
++) {if(n
+30>=a
[i
]) cnt
++;}cout
<<cnt
;return 0;
}
總結
以上是生活随笔為你收集整理的寒假每日一题(入门组)【week5 完结】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。