文章目录
本题解并非官方题解,仅供参考~
一、题解:
第一题.
题目:
- 思路:数学问题:36x30÷10=108;
108
第二题:
题目:
- 思路:每次循环都对1000求余,防止爆int. 或者用快速幂也可以
//2.608
#include<bits/stdc++.h>
using namespace std;
int ans=1;
void qpow(int base,int power){
while(power>=1){
if(power%2==0){
base=(base*base)%1000;
power/=2;
}
if(power%2!=0){
ans=(ans*base)%1000;;
power--;
}
cout<<ans<<endl;
}
}
int main(){
//暴力模拟
int res=1;
for(int i=1;i<=2023;i++)
res=(res*2)%1000;
cout<<res<<endl;
//或者用快速幂qpow(2,2023);
}
第三题:
题目:
- 思路:从一开始遍历,直到找到第23个数位和相等的正整数,这里用到了一个函数itoa();
itoa(int value, char*string, int radix);
第一个参数是要转化的值,第二个参数是将转化后的数存放在一个字符数组中,第三个数是需要转化的进制
//3. 4169
#include<bits/stdc++.h>
using namespace std;
int main(){
int num=0;
for(int i=1;i<=(1<<30);i++){
char s1[10000],s2[10000];
itoa(i,s1,8);
itoa(i,s2,2);
int sum1=0,sum2=0;//记录数位和
for(int i=0;i<strlen(s1);i++)
sum1+=(s1[i]-'0');
for(int i=0;i<strlen(s2);i++)
sum2+=(s2[i]-'0');
if(sum1==sum2)
num++;
if(num==23){//找到第23个数位和相等的正整数
cout<<i<<endl;
break;
}
}
}
第四题:
题目:
思路:将这些数存放在一个数组中,找到每个数的约数,取最大值即可,注意是最早出现的那个
//901440
#include<bits/stdc++.h>
using namespace std;
int arr[37]={0, 393353, 901440, 123481, 850930, 423154, 240461, 373746, 232926, 396677, 486579, 744860, 468782,
941389, 777714, 992588, 343292, 385198, 876426, 483857, 241899, 544851, 647930, 772403, 109929,
882745, 372491, 877710, 340000, 659788, 658675, 296521, 491295, 609764, 718967, 842000, 670302
};
int cnt[37];//存约数个数
int main(){
for(int i=1;i<=36;i++){
int n=arr[i],ans=0;
for(int j=1;j<=n/j;j++){
if(n%j==0){
ans++;
if(n/j!=j)
ans++;
}
}
cout<<ans<<endl;
}
}
第五题:
题目:
思路:bfs遍历一遍即可得到答案
#include<bits/stdc++.h>
using namespace std;
string g[31]={
" ",
" 0000100010000001101010101001001100000011",
" 0101111001111101110111100000101010011111",
" 1000010000011101010110000000001011010100",
" 0110101010110000000101100100000101001001",
" 0000011010100000111111001101100010101001",
" 0110000110000000110100000000010010100011",
" 0100110010000110000000100010000101110000",
" 0010011010100110001111001101100110100010",
" 1111000111101000001110010001001011101101",
" 0011110100011000000001101001101110100001",
" 0000000101011000010011111001010011011100",
" 0000100000011001000100101000111011101100",
" 0010110000001000001010100011000010100011",
" 0110110000100011011010011010001101011011",
" 0000100100000001010000101100000000000010",
" 0011001000001000000010011001100101000110",
" 1110101000011000000100011001001100111010",
" 0000100100111000001101001000001010010001",
" 0100010010000110100001100000110111110101",
" 1000001001100010011001111101011001110001",
" 0000000010100101000000111100110010101101",
" 0010110101001100000100000010000010110011",
" 0000011101001001000111011000100111010100",
" 0010001100100000011000101011000000010101",
" 1001111010010110011010101110000000101110",
" 0110011101000010100001000101001001100010",
" 1101000000010010011001000100110010000101",
" 1001100010100010000100000101111111111100",
" 1001011010101100001000000011000110110000",
" 0011000100011000010111101000101110110001",
};
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
queue< pair<int, int> > q;
void bfs(int x,int y){
q.push({x,y});
while(!q.empty()){
auto t=q.front();
q.pop();
for(int i=0;i<4;i++){
int tx=t.first+dx[i];
int ty=t.second+dy[i];
if(tx<1||ty<1||tx>30||ty>40) continue;
if(g[tx][ty]=='1'||g[tx][ty]=='2') continue;
g[tx][ty]='2';
q.push({tx,ty});
}
}
}
int ans=0;
int main(){
g[1][1]='2';
bfs(1,1);
for(int i=1;i<=30;i++)
cout<<g[i]<<endl;
for(int i=1;i<=30;i++)
for(int j=1;j<=40;j++)
if(g[i][j]=='2')
ans++;
cout<<ans;
return 0;
}
第六题:
题目:
#include<bits/stdc++.h>
using namespace std;
int x;
int main(){
/*
cin>>x;
int k=x/100000;//得到十万位数字
x=x%100000;
x=x*10+k;
cout<<x<<endl;
return 0;
*/
//或者用字符串更简单哦
string s; cin>>s;
char c=s[0];
s=s.substr(1,5);
s=s+c;
cout<<s;
return 0;
}
第七题:
题目:
思路:从后往前判断一下即可
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
cin>>str;
for(int i=str.size()-1;i>=0;i--){
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'){
cout<<str[i];
break;
}
}
return 0;
}
第八题:
题目:
每次都去除各位上的数字相乘
思路:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
signed main(){
cin>>n;
while(n>=10){
int temp=1;
while(n>0){
if(n%10!=0)
temp*=n%10;
n/=10;
}
n=temp;
cout<<temp<<endl;
}
return 0;
}
第九题:
题目:
- 思路:
同5,bfs,只不过多了一个条件,用__gcd函数来求得最大公约数
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int g[N][N],n,m,r,c,ans=0;
bool vis[N][N];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
queue< pair<int,int> > q;
bool check(int x,int y){
int m=__gcd(x,y);
//cout<<x<<' '<<y<<' '<<m<<endl;
if(m>1) return true;
return false;
}
void bfs(int x,int y){
q.push({x,y});
while(!q.empty()){
auto t=q.front();
//cout<<t.first<<' '<<t.second<<endl;
q.pop();
for(int i=0;i<4;i++){
int tx=t.first+dx[i];
int ty=t.second+dy[i];
if(tx<1||ty<1||tx>n||ty>m) continue;
if(vis[tx][ty]) continue; //已经走过
if(check(g[t.first][t.second],g[tx][ty])){
q.push({tx,ty});
vis[t.first][t.second]=true;
vis[tx][ty]=true;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>g[i][j];
cin>>r>>c;
bfs(r,c);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(vis[i][j])
ans++;
cout<<ans<<endl;
return 0;
}
第十题:
题目:
- 思路:前缀和
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100010;
int n,k,a[N],s[N];
int ans=0;
signed main(){
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
s[i]=s[i-1]+a[i];
for(int i=1;i+k-1<=n;i++)
ans=max(ans,s[i+k-1]-s[i-1]);
cout<<ans;
}