百度之星2017初赛A

雪崩,没晋级,补题

1001

分析:求n-1的约数个数

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "string"
 5 #include "cmath"
 6 using namespace std;
 7 int T;
 8 long long n;
 9 long long rev(long long res){
10     long long t=sqrt(res);
11     long long cnt=0;
12     if(t*t==res){
13         cnt=1;
14     }
15     t--;
16     for(int i=1;i<=t;i++){
17         if(res%i==0)
18             cnt+=2;
19     }
20     return cnt;
21 }
22 int main()
23 {
24     cin>>T;
25     while(T--){
26         cin>>n;
27         cout<<rev(n-1)<<endl;
28     }
29     return 0;
30 }
View Code

1005

分析:对于小于2月29的,先判断再加1,其他的先加1在判断。注意如果是2月29号,必须是闰年才行,同时我们可以采用平年改天星期几向后移一天,闰年向后移两天的方法来统计。

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "string"
 5 using namespace std;
 6 int T;
 7 //string s;
 8 bool judge(int x){
 9     if(x%4==0&&(x%100!=0||x%400==0))
10         return true;
11     return false;
12 }
13 int check(string h){
14     string res="";
15     res+=h[5];
16     res+=h[6];
17     res+=h[8];
18     res+=h[9];
19     string t="0229";
20     if(res<t)
21         return 0;
22     else if(res==t)
23         return 1;
24     else
25         return 2;
26 }
27 int main()
28 {
29     cin>>T;
30     while(T--){
31         string s;
32         cin>>s;
33         int num=0;
34         for(int i=0;i<4;i++){
35             num*=10;
36             num+=(s[i]-'0');
37         }
38         int t=0;
39         if(check(s)==0){
40             //int i=num,t=0;
41             while(1){
42                 if(judge(num)){
43                     t+=2;
44                 }else t++;
45                 num++;
46                 if(t%7==0)  break;
47                 //num++;
48             }
49         }else if(check(s)==2){
50             while(1){
51                 num++;
52                 if(judge(num)){
53                     t+=2;
54                 }else t++;
55                 if(t%7==0)  break;
56             }
57         }else{
58             while(1){
59                 num++;
60                 if(judge(num)){
61                     t+=2;
62                     if(t%7==0) break;
63                 }else t++;
64             }
65         }
66         cout<<num<<endl;
67     }
68     return 0;
69 }
View Code

1006

分析:这题学到了一种巧妙的处理方法,在矩形的周围染一圈0。这样的话,联通块0是否和边界有连接可以很方便判断出来了,接下来就是判断1和0的联通块的数量。

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "string"
 5 using namespace std;
 6 const int maxn=100+10;
 7 int n,m;
 8 int vis[maxn][maxn],f[maxn][maxn];
 9 int dx[]={-1,1,0,0};
10 int dy[]={0,0,-1,1};
11 void dfs(int x,int y){
12     vis[x][y]=1;
13     for(int i=0;i<4;i++){
14         int nx=x+dx[i],ny=y+dy[i];
15         if(nx>=0&&nx<=n+1&&ny>=0&&ny<=m+1&&!vis[nx][ny]&&f[nx][ny]==f[x][y])
16             dfs(nx,ny);
17     }
18 }
19 int main()
20 {
21     while(cin>>n>>m){
22         memset(vis,0,sizeof(vis));
23         memset(f,0,sizeof(f));
24         for(int i=1;i<=n;i++){
25             getchar();
26             for(int j=1;j<=m;j++){
27                 char ch;
28                 scanf("%c",&ch);
29                 if(ch=='1')
30                     f[i][j]=1;
31             }
32         }
33         int zero=0,one=0;
34         for(int i=0;i<=n+1;i++){
35             for(int j=0;j<=m+1;j++){
36                 if(!vis[i][j]){
37                     if(f[i][j]==0) zero++;
38                     else one++;
39                     dfs(i,j);
40                 }
41             }
42         }
43         if(zero==2&&one==1)  printf("0");
44         else if(zero==1&&one==1)  printf("1");
45         else  printf("-1");
46         printf("
");
47     }
48 }
View Code
原文地址:https://www.cnblogs.com/wolf940509/p/7352002.html