给出系数计算一个迭代的公式,反向代入即可。
1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 10; 7 int n; 8 int a[maxn], b[maxn]; 9 int gcd(int a, int b) { 10 return b == 0 ? a : gcd(b, a % b); 11 } 12 int main() 13 { 14 int T; 15 int kase = 1; 16 scanf("%d", &T); 17 while(T--) { 18 scanf("%d", &n); 19 for(int i = 1; i <= n; i++) { 20 scanf("%d", &a[i]); 21 } 22 for(int i = 1; i <= n; i++) { 23 scanf("%d", &b[i]); 24 } 25 26 int x = a[n]; 27 int y = b[n]; 28 for(int i = n - 1; i >= 1; i--) { 29 int t = x; 30 x = a[i] * x + y; 31 y = b[i] * t; 32 } 33 int g = gcd(x, y); 34 printf("Case #%d: %d %d\n", kase++, y / g, x / g); 35 } 36 return 0; 37 }
刚开始有N个人,每个人都有偶数个糖,每一轮每个人都把自己的糖的一半给自己右边的人,每一轮分的是时候如果有奇数个糖,就会从老师那里得到一块糖,加入循环。当每个人的糖数一样的时候停止。模拟过程即可。
1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 1001000; 7 int a[maxn]; 8 int n; 9 10 bool ch() { 11 /*for(int i = 0; i < n; i++) 12 printf("#%d ",a[i]); 13 puts("");*/ 14 int f = 0; 15 for(int i = 0; i < n; i++) { 16 if(a[i] & 1) { 17 a[i]++; 18 f = 1; 19 } 20 } 21 return f; 22 } 23 int ok() { 24 /*for(int i = 0; i < n; i++) 25 printf("@%d ",a[i]); 26 puts("");*/ 27 for(int i = 1; i < n; i++) { 28 if(a[i] != a[i - 1]) { 29 return 0; 30 } 31 } 32 return 1; 33 } 34 35 int main() 36 { 37 while(scanf("%d", &n), n) { 38 for(int i = 0; i < n; i++) { 39 scanf("%d", &a[i]); 40 } 41 42 int ans = 0; 43 int t = 0; 44 while(1) { 45 46 int X, Y; 47 X = ok(); 48 Y = !ch(); 49 if(X && Y) { 50 ans = a[0]; 51 break; 52 } 53 int tp = a[n-1] / 2; 54 for(int i = 0; i < n; i++) { 55 int x; 56 x = a[i] / 2; 57 a[i] = x + tp; 58 tp = x; 59 } 60 t++; 61 } 62 printf("%d %d\n", t - 1, ans); 63 } 64 return 0; 65 }
给出N个障碍的坐标,问每次消除一行或者一列,最少需要几次把所有的障碍消灭完。
直接二分匹配,寻找行和列的最大匹配。
1 #include<stdio.h> 2 #include<string.h> 3 int e[510][510],match[510],book[510]; 4 int n,m; 5 int dfs(int u); 6 7 int main() 8 { 9 int i,j,a,b,sum; 10 while(scanf("%d%d",&n,&m)!=EOF) 11 { 12 memset(e,0,sizeof(e)); 13 memset(match,0,sizeof(match)); 14 for(i=1;i<=m;i++) 15 { 16 scanf("%d%d",&a,&b); 17 e[a][b]=1; 18 } 19 sum=0; 20 for(i=1;i<=n;i++) 21 { 22 memset(book,0,sizeof(book)); 23 if(dfs(i)==1) 24 sum++; 25 } 26 printf("%d\n",sum); 27 } 28 return 0; 29 } 30 int dfs(int u) 31 { 32 int i; 33 for(i=1;i<=n;i++) 34 { 35 if(book[i]==0&&e[u][i]==1) 36 { 37 book[i]=1; 38 if(match[i]==0||dfs(match[i])==1) 39 { 40 match[i]=u; 41 return 1; 42 } 43 } 44 } 45 return 0; 46 }
暴力模拟。
1 #include<stdio.h> 2 #include<string.h> 3 4 struct T{ 5 int x, y, z; 6 int s[10]; 7 }; 8 T a[110]; 9 int n; 10 11 bool A(int k) 12 { 13 int s[10] = {0}, z, num , i, j, x; 14 z = k; 15 for(i = 0; i < 4; i++) 16 { 17 s[z%10]++; 18 z /= 10; 19 } 20 for(i = 0; i < n; i++) 21 { 22 for(j = num = 0; j < 10; j++) 23 { 24 if(s[j] > a[i].s[j]) 25 num += a[i].s[j]; 26 else 27 num += s[j]; 28 29 } 30 31 if(num != a[i].y) 32 return 0; 33 34 35 36 for(j = num = 0, x = a[i].x, z = k; j < 4; j++) 37 { 38 if(x % 10 == z % 10) 39 num++; 40 x /= 10; 41 z /= 10; 42 } 43 if(num != a[i].z) 44 return 0; 45 } 46 return 1; 47 } 48 49 int main() 50 { 51 int i, j, k, t, flag; 52 while(scanf("%d", &n), n) 53 { 54 memset(a, 0, sizeof(a)); 55 for(i = 0; i < n; i++) 56 { 57 scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z); 58 for(j = 0, k = a[i].x; j < 4; j++) 59 { 60 a[i].s[k%10]++; 61 k /= 10; 62 } 63 } 64 for(i = flag = 0; i < 10000; i++) 65 { 66 if(A(i)) 67 { 68 flag++; 69 t = i; 70 } 71 } 72 if(flag != 1) 73 printf("Not sure\n"); 74 else 75 printf("%d\n", t); 76 } 77 }
最大连续子序列之和,需要输出起点和终点。
1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 7 int main() 8 { 9 int n; 10 int i, max, a[10010], b[10010], begin, end, x; 11 while(scanf("%d", &n), n != 0) { 12 for(int i = 0; i < n; i++) { 13 scanf("%d", &a[i]); 14 } 15 16 b[0] = a[0]; 17 if(a[0] >= 0) 18 { 19 max = a[0]; 20 begin = end = 0; 21 } 22 else 23 { 24 max = -1; 25 begin = 0; 26 end = n - 1; 27 } 28 for(int i = 1, x = 0; i < n; i++) 29 { 30 if(b[i-1] > 0) 31 b[i] = b[i-1] + a[i]; 32 else 33 { 34 x = i; 35 b[i] = a[i]; 36 } 37 if(b[i] > max) 38 { 39 begin = x; 40 end = i; 41 max = b[i]; 42 } 43 } 44 if(max < 0) 45 max = 0; 46 printf("%d %d %d\n", max, a[begin], a[end]); 47 } 48 49 return 0; 50 }
计算am,如果am = a(m-1) - m 小于0且没有出现过,那么am = a(m-1) - m,否则am = a(m-1) + m。给出m计算am。根据规则打表即可。
1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int a[600010],book[10000010]; 6 int main() 7 { 8 int i; 9 a[0]=0; 10 a[1]=1; 11 book[0]=1; 12 book[1]=1; 13 for(i=2;i<=600000;i++) 14 { 15 if(a[i-1]-i>0&&book[a[i-1]-i]==0) 16 { 17 a[i]=a[i-1]-i; 18 book[a[i]]=1; 19 } 20 else 21 { 22 a[i]=a[i-1]+i; 23 book[a[i]]=1; 24 } 25 } 26 while(scanf("%d",&i)!=EOF) 27 { 28 if(i==-1) 29 break; 30 printf("%d\n",a[i]); 31 } 32 return 0; 33 }
最小生成树模板题,签到题。
1 #include<stdio.h> 2 #include<string.h> 3 #define inf 99999999 4 int n,m; 5 int e[1010][1010],dis[1010],book[1010]; 6 int main() 7 { 8 int i,j,k,ans,min,u,a,b; 9 while(scanf("%d",&n)!=EOF) 10 { 11 ans=0; 12 if(n==0) 13 break; 14 scanf("%d",&m); 15 for(i=1;i<=n;i++) 16 for(j=1;j<=n;j++) 17 if(i==j) 18 e[i][j]=0; 19 else 20 e[i][j]=1; 21 for(i=1;i<=m;i++) 22 { 23 scanf("%d%d",&a,&b); 24 e[a][b]=e[b][a]=0; 25 } 26 memset(book,0,sizeof(book)); 27 for(i=1;i<=n;i++) 28 dis[i]=e[1][i]; 29 book[1]=1; 30 for(k=1;k<=n-1;k++) 31 { 32 min=inf; 33 u=-1; 34 for(i=1;i<=n;i++) 35 if(book[i]==0&&dis[i]<min) 36 { 37 min=dis[i]; 38 u=i; 39 } 40 if(u==-1) 41 break; 42 book[u]=1; 43 ans+=dis[u]; 44 for(i=1;i<=n;i++) 45 if(book[i]==0&&dis[i]>e[u][i]) 46 dis[i]=e[u][i]; 47 } 48 printf("%d\n",ans); 49 } 50 return 0; 51 }
总的来说,题目比较简单,配合还不错,有些自己能写的题,还是实现起来有困难,最好不要让队友帮忙。