The 10th SWJTU Collegiate Programming Contest - Online Round

Problem A: SRTP
 對兩行分別求和,比較大小
 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;  8 int main()  9 { 10     int t,a,i,j,sum1,sum2,n,m; 11     cin>>t; 12  { 13         for(i=1;i<=t;i++) 14  { 15             cin>>n>>m; 16             sum1=sum2=0; 17             for(j=1;j<=n;j++) 18  { 19                 cin>>a; 20                 sum1+=a; 21 
22  } 23             for(j=1;j<=m;j++) 24  { 25                 cin>>a; 26                 sum2+=a; 27 
28  } 29             if(sum1>sum2)   cout<<"Terry"<<endl; 30             else if(sum1<sum2) cout<<"Dante"<<endl; 31             else cout<<"Draw"<<endl; 32  } 33  } 34 
35 }
A
 
 
Problem B: 2048
模擬U,D,R,L四種操做
 1 #include<cstdio>
 2 #include<cstring>
 3 
 4 int a[10][10];  5 
 6 void fun(char c)  7 {  8       int i,j,n1,n2,k;  9       int b[10],tmp[10];  10 
 11       if(c=='U')  12  {  13             for(j=1;j<=4;j++)  // ??
 14  {  15                         n1=0;  16                         for(i=1;i<=4;i++)  17                              if(a[i][j]!=0) b[++n1]=a[i][j];  18                         n2=0;  19                        /* printf("b:");  20  for(i=1;i<=n1;i++)  21  printf("%d ",b[i]);  22  printf("\n");  23                         */
 24                         for(i=1;i<=n1;i++)  25                               if(i==n1) tmp[++n2]=b[i];  26                               else {  27                                     if(b[i]==b[i+1]) tmp[++n2]=b[i]*2,i++;  28                                     else tmp[++n2]=b[i];  29  }  30 
 31 
 32                         for(i=1;i<=n2;i++)  33                               a[i][j]=tmp[i];  34                         for(i=n2+1;i<=4;i++)  35                               a[i][j]=0;  36  }  37 
 38 
 39  }  40 
 41       if(c=='D')  42  {  43             for(j=1;j<=4;j++)  // ??
 44  {  45                         n1=0;  46                         for(i=4;i>=1;i--)  47                              if(a[i][j]!=0) b[++n1]=a[i][j];  48                         n2=0;  49                        /* printf("b:");  50  for(i=1;i<=n1;i++)  51  printf("%d ",b[i]);  52  printf("\n");  53                         */
 54                         for(i=1;i<=n1;i++)  55                               if(i==n1) tmp[++n2]=b[i];  56                               else {  57                                     if(b[i]==b[i+1]) tmp[++n2]=b[i]*2,i++;  58                                     else tmp[++n2]=b[i];  59  }  60 
 61 
 62                         for(i=1,k=4;i<=n2;i++,k--)  63                               a[k][j]=tmp[i];  64                         for(int i1=k;i1>=1;i1--)  65                               a[i1][j]=0;  66  }  67 
 68  }  69 
 70 
 71       if(c=='L')  72  {  73             for(i=1;i<=4;i++)   // ??
 74  {  75 
 76                   n1=0;  77                   for(j=1;j<=4;j++)  78                         if(a[i][j]!=0) b[++n1]=a[i][j];  79                   n2=0;  80                   for(j=1;j<=n1;j++)  81                   if(j==n1) tmp[++n2]=b[j];  82                   else {  83                         if(b[j]==b[j+1]) tmp[++n2]=b[j]*2,j++;  84                         else tmp[++n2]=b[j];  85 
 86  }  87 
 88                   for(j=1;j<=n2;j++)  89                         a[i][j]=tmp[j];  90                   for(j=n2+1;j<=4;j++)  91                         a[i][j]=0;  92 
 93  }  94  }  95 
 96       if(c=='R')  97  {  98             for(i=1;i<=4;i++)   // ??
 99  { 100 
101                   n1=0; 102                   for(j=4;j>=1;j--) 103                         if(a[i][j]!=0) b[++n1]=a[i][j]; 104                   n2=0; 105                   for(j=1;j<=n1;j++) 106                   if(j==n1) tmp[++n2]=b[j]; 107                   else { 108                         if(b[j]==b[j+1]) tmp[++n2]=b[j]*2,j++; 109                         else tmp[++n2]=b[j]; 110 
111  } 112 
113                   for(j=1,k=4;j<=n2;j++,k--) 114                         a[i][k]=tmp[j]; 115                   for(int j1=k;j1>=1;j1--) 116                         a[i][j1]=0; 117 
118  } 119  } 120 
121 
122 } 123 
124 
125 
126 int main() 127 { 128       int i,j; 129       char s[10005]; 130 
131       while(~scanf("%d%d%d%d",&a[1][1],&a[1][2],&a[1][3],&a[1][4])) 132  { 133             for(i=2;i<=4;i++) 134                   for(j=1;j<=4;j++) 135                         scanf("%d",&a[i][j]); 136 
137             scanf("%s",s); 138             int len=strlen(s); 139             for(i=0;i<len;i++) 140  { 141  fun(s[i]); 142 
143 
144  } 145 
146             for(i=1;i<=4;i++) 147  { 148                   for(j=1;j<=4;j++) 149                         printf("%5d",a[i][j]); 150                   printf("\n"); 151  } 152             
153             printf("\n"); 154  } 155 
156 
157 
158       return 0; 159 
160 }
B
 
 
Problem C: What's Her Name?
統計g,u,a,n,j,i出現次數。
(g-1),u,a,(n/2),j,i中的最小值即爲字符串可能出現的最大次數
注意:(g-1)可能小於零,此時特判答案爲0.
 1 #include <iostream>
 2     #include <cmath>
 3     #include <string>
 4     #include <cstdio>
 5     #include <cstring>
 6     #include <algorithm>
 7     using namespace std;  8     int main()  9  { 10         //string ans="guanjing";
11         int flag[110]; 12       // freopen("data.in","r",stdin); 13        // freopen("data.out","w",stdout);
14         int t,a,i,j,sum1,sum2,n,m,sum,l,cnt,now,ans; 15         string s; 16         while(cin>>s) 17  { 18             cnt=0; 19             l=s.length(); 20             memset(flag,0,sizeof(flag)); 21             for(i=0;i<l;i++) 22             if(s[i]=='g') flag[1]++; 23             else if (s[i]=='u') flag[2]++; 24             else if (s[i]=='a') flag[3]++; 25             else if (s[i]=='n') flag[4]++; 26             else if (s[i]=='j') flag[5]++; 27             else if (s[i]=='i') flag[6]++; 28 
29             ans=min(flag[1]-1,min(flag[2],min(flag[3],min(flag[4]/2,min(flag[5],flag[6]))))); 30             if (ans<0) ans=0; 31             cout<<ans<<endl; 32  } 33     }
C

 

 

Problem D: Skip Classes?ios

水題git

 1 #include <iostream>
 2 #include<cstdio>
 3 using namespace std;  4 int a[1005];  5 int main()  6 {  7     int x,y,i,j,k,n,m;  8     int ans;  9     while(~scanf("%d%d%d",&n,&m,&k)) 10  { 11           x=0; 12           ans=0; 13           for(i=0;i<n;i++) 14  { 15                   scanf("%d",&a[i]); 16                   if(a[i]==1) x++; 17  } 18            y=n-x; 19            if(y>k) 20  { 21                  x+=y-k; 22                  if(x>m) ans+=x-m; 23            }else{ 24                   if(x>m) ans+=x-m; 25  } 26            printf("%d\n",ans); 27  } 28     return 0; 29 }
D

 

 

Problem E: Sequencecanvas

動態規劃ide

dp[n][k]=dp[n][k]+dp[n/i][k-1]    (i=1~n)url

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <string>
 5 using namespace std;  6 int dp[2003][2003];  7 int solve(int n, int k)  8 {  9     if(n==1) return 1; 10     if(k==1) return n; 11     if(dp[n][k]) return dp[n][k]; 12 
13     for (int i = 1; i <= n; ++i) 14  { 15         dp[n][k]=(dp[n][k]+solve(n/i,k-1))%(20120354); 16  } 17     return dp[n][k]; 18 } 19 int main() 20 { 21     int n, k; 22     while(~scanf("%d%d",&n,&k)) 23     cout<<solve(k, n)<<endl; 24     return 0; 25 }
E
 
 
Problem F: Share Candies
1 不會。
2 請各位看官@憨大哥@管大神
F

 

 
 
Problem G: Fibonacci Prime
除了前幾個,下標爲素數的菲波那契數其實就是題目說的菲波那契素數。
K最大又只有20,背一下100之內的素數。。
 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;  8 int outcome[65]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,81,89};  9 
10 int main() 11 { 12     //freopen("data.in","r",stdin); 13    // freopen("data.out","w",stdout);
14     unsigned long long ans; 15     int t,a,i,j,sum1,sum2,n,m,sum; 16     long long f[300]; 17     f[1]=f[2]=1; 18     for(i=3;i<=299;i++) 19         f[i]=f[i-1]+f[i-2]; 20     while(cin>>n) 21  { 22     if(n==1) cout<<2<<endl; 23     else if(n==2) cout<<3<<endl; 24     else if(n==3) cout<<5<<endl; 25     else if(n==4) cout<<13<<endl; 26     else cout<<f[outcome[n]]<<endl; 27 
28  } 29 }
G

 

 

Problem H: A + B Againspa

一、無限循環小數化分數:code

好比說對於一個無限循環小數0.(4),用咱們中學數學中學到的方法,令X = 0.(4),則10 * X = 4.(4) = 4 + 0.(4) = 4 + X     =>     X = 4 / 9,同理,首先將題中所給出的小數轉化爲0.(***)的形式,而後再採用上述所示的方法便可。orm

度娘
blog

二、分數相加
三、約分
 1     #include <iostream>
 2     #include<cstdio>
 3     #include<cstring>
 4     using namespace std;  5 
 6     long long gcd(long long a,long long b)  7  {  8           return b==0?a:gcd(b,a%b);  9  } 10 
11     void fun(char *s,long long *a,long long *b) 12  { 13           long long i,j,k,p,n,nl,ml,m,flag=0,g; 14           long long  len=strlen(s); 15 
16           for(j=0;j<len;j++) 17           if(s[j]=='(') {flag=1; break;} 18 
19           if(flag)  // ????????
20  { 21                 for(i=0;i<len && s[i]!='.';i++); 22                 p=0;  //????????
23 
24                 for(long long i1=0;i1<i;i1++) 25                       p=p*10+s[i1]-'0'; 26 
27                 n=0; 28                  for(long long i1=i+1;i1<j;i1++) 29                       n=n*10+s[i1]-'0'; 30                 nl=j-i-1; 31 
32                 m=n; 33                 for(k=j+1;k<len && s[k]!=')';k++); 34 
35                 for(long long i1=j+1;i1<k;i1++) 36                     m=m*10+s[i1]-'0'; 37 
38                 ml=k-i-2; 39                 *b=9;   // ????
40                 for(long long i1=1;i1<ml-nl;i1++) 41                       *b=*b*10+9; 42                 for(long long i1=1;i1<=nl;i1++) 43                       *b=*b*10; 44 
45                  *a=(m-n)+(*b)*p; 46 
47                  g=gcd(*a,*b); 48                 *a=*a/g; *b=*b/g; 49 
50           }else{  // ??????????
51                 for(i=0;i<len && s[i]!='.';i++); 52                 p=0;  //????????
53 
54                 for(long long i1=0;i1<i;i1++) 55                       p=p*10+s[i1]-'0'; 56 
57                 nl=len-1-i; 58                 n=1;   // ????
59                 for(long long i1=1;i1<=nl;i1++) 60                       n=n*10; 61 
62                 ml=nl; 63                 m=0;  // ????
64                 for(long long i1=i+1;i1<len;i1++) 65                       m=m*10+s[i1]-'0'; 66 
67                 *a=n*p+m; 68                 *b=n; 69                 
70                 g=gcd(*a,*b); 71                 *a=*a/g; *b=*b/g; 72               // 73 
74  } 75  } 76     int main() 77  { 78         int ncase; 79         long long a1,b1,a2,b2; 80         long long a,b,g; 81         char s1[1005],s2[1005]; 82 
83         scanf("%d",&ncase); 84         while(ncase--) 85  { 86               scanf("%s%s",s1,s2); 87               fun(s1,&a1,&b1); 88               fun(s2,&a2,&b2); 89                 a=a1*b2+a2*b1; 90                 b=b1*b2; 91                 g=gcd(a,b); 92                 printf("%lld/%lld\n",a/g,b/g); 93  } 94 
95 
96 
97         return 0; 98     }
H

 

 

Problem I: Digits' Sumip

暴力打表 或者 快速冪

對於N=64的數據進行特判。

 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 int outcome[65]={1,2,4,8,7,5,10,11,13,8,7,14,19,20,22,26,25,14,19,29,31,26,25,41,37,29,40,35,43,41,37,47,58,62,61,59,64,56,67,71,61,50,46,56,58,62,70,68,73,65,76,80,79,77,82,92,85,80,70,77,82,74,85,89,88};
 9 unsigned long long mul(unsigned long long a,unsigned long long b)
10 {
11     unsigned long long ans;
12     ans=1;
13     while(b)
14     {
15         if(b&1)
16             ans=ans*a;
17         b>>=1;
18         a*=a;
19     }
20     return ans;
21 }
22 int main()
23 {
24     //freopen("data.in","r",stdin);
25     //freopen("data.out","w",stdout);
26     unsigned long long ans;
27     int t,a,i,j,sum1,sum2,n,m,sum;
28     while(cin>>n)
29         cout<<outcome[n]<<endl;
30         
31     用快速冪打表:    
32     //for(n=0;n<=63;n++)
33     
34      //   ans=mul(2,n);
35        // cout<<ans<<endl;
36      //   sum=0;
37      //   while(ans)
38     //    {
39      //       sum+=ans%10;
40     //        ans/=10;
41     //    }
42     //      if(n==64) sum=88;
43     //    cout<<sum<<endl;
44     
45 }
I
相關文章
相關標籤/搜索