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 }
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("
");
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("
");
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("
");
151 }
152
153 printf("
");
154 }
155
156
157
158 return 0;
159
160 }
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 }
Problem D: Skip Classes?
水题
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
",ans);
27 }
28 return 0;
29 }
Problem E: Sequence
动态规划
dp[n][k]=dp[n][k]+dp[n/i][k-1] (i=1~n)
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 }
Problem F: Share Candies
1 不会。 2 请各位看官@憨大哥@管大神
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 }
Problem H: A + B Again
1、无限循环小数化分数:
比如说对于一个无限循环小数0.(4),用我们中学数学中学到的方法,令X = 0.(4),则10 * X = 4.(4) = 4 + 0.(4) = 4 + X => X = 4 / 9,同理,首先将题中所给出的小数转化为0.(***)的形式,然后再采用上述所示的方法即可。
3、约分
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
",a/g,b/g);
93 }
94
95
96
97 return 0;
98 }
Problem I: Digits' Sum
暴力打表 或者 快速幂
对于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 }