1、Double
1 #include <stdio.h> 2 int main() 3 { 4 int arr[20] = {0}, i, x, count=0; 5 for(i=0; ; ++i){ 6 scanf("%d",&x); 7 if(x==0) break; 8 arr[i] = x; 9 } 10 for(int j=0; j<i; ++j){ 11 if(arr[j]%2) continue; 12 for(int k=0; k<i; ++k){ 13 if(arr[j]/2==arr[k]) 14 count++; 15 } 16 } 17 printf("%d",count); 18 return 0; 19 }
2、Houseboat
1 #include <stdio.h> 2 #include <math.h> 3 4 inline void one(int i){ 5 double x,y; 6 scanf("%lf%lf",&x,&y); 7 double r = sqrt(x*x+y*y); 8 double area = 0.5*3.14159265*r*r; 9 int n =ceil(area/50); 10 printf("%d %d ",i,n); 11 } 12 int main(){ 13 int n ; 14 int i=1; 15 scanf("%d",&n); 16 while(n--){ 17 one(i); 18 i++; 19 } 20 return 0; 21 }
3、Sum of Consecutive
1 #include <stdio.h> 2 #include <math.h> 3 #define N 1000 4 int isPrime(int n){ 5 for(int i=2; i<n; ++i) 6 if(n%i==0) 7 return 0; 8 return 1; 9 } 10 int main() 11 { /* 1 */ 12 int arr[N] = {0}, index=0, sum = 0; 13 for(int j=2;sum<10000 ; ++j) 14 if(isPrime(j)){ 15 sum += j; 16 arr[index++] = j; 17 } 18 /* 2 */ 19 int n; 20 scanf("%d",&n); 21 while(n){ 22 int count = 0; 23 for(int j=0;j<index ; ++j){ 24 sum = 0; 25 for(int k=j;k<index ; ++k) 26 { 27 sum += arr[k]; 28 if(sum==n){ 29 count++; 30 } 31 if(sum>n) break; 32 } 33 } 34 printf("%d ",count); 35 scanf("%d",&n); 36 } 37 return 0; 38 }
4、Specialized Number
1 #include <stdio.h> 2 int fun(int n, int base) 3 { 4 int sum = 0; 5 while(n) 6 { 7 sum += n%base; 8 n /= base; 9 } 10 return sum; 11 } 12 int main() 13 { 14 for(int n = 2992; n<9999; ++n){ 15 int sum = fun(n,10); 16 if(sum ==fun(n,12) && sum == fun(n,16)) 17 printf("%d ",n); 18 } 19 return 0; 20 }
5、Favorite Number
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 100000 5 6 int prime_number = 0; 7 int prime_list[MAXNUM]; 8 bool is_prime[MAXNUM]; 9 int ans[MAXNUM + 2]; 10 int dp[MAXNUM + 2]; 11 12 void set_prime() { 13 int i, j; 14 memset(is_prime, 0, sizeof(is_prime)); 15 for (i = 2; i < MAXNUM; i++) { 16 if (is_prime[i] == 0) { 17 prime_list[prime_number++] = i; 18 if (i >= MAXNUM / i) continue; 19 for (j = i * i; j < MAXNUM; j+=i) { 20 is_prime[j] = 1; 21 } 22 } 23 } 24 } 25 26 int main() { 27 int i, j, k; 28 memset(dp, -1, sizeof(dp)); 29 set_prime(); 30 ans[0] = 0; 31 dp[1] = 0; 32 for (i = 1; i <= MAXNUM; i++) { 33 ans[i] = ans[i - 1] + dp[i]; 34 if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + 1) { 35 dp[i + 1] = dp[i] + 1; 36 } 37 for (j = 0; j < prime_number; j++) { 38 if (i > MAXNUM / prime_list[j]) break; 39 k = i * prime_list[j]; 40 if (dp[k] == -1 || dp[k] > dp[i] + 1) { 41 dp[k] = dp[i] + 1; 42 } 43 } 44 } 45 while (scanf("%d%d", &i, &j) == 2 && (i || j)) { 46 printf("%d ", ans[j] - ans[i - 1]); 47 } 48 }
6、Sticks
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 #include<algorithm> 5 #include<string.h> 6 int stick[64]; 7 bool vis[64]; 8 int n, sum, can; 9 bool dfs(int m, int rest, int c) 10 { 11 if (rest == 0) 12 { 13 if (m == 2 || m == 1) 14 return true; 15 c = 0; 16 while (vis[c]) c++; 17 if (dfs(m - 1, can, c)) 18 return true; 19 return false; 20 } 21 else 22 { 23 int i; 24 if (c > n - 1) return false; 25 for (i = c; i<n; i++) 26 { 27 if (vis[i]) 28 continue; 29 if (stick[i]>rest) 30 continue; 31 if (stick[i] == stick[i - 1] && !vis[i - 1]) 32 continue; 33 vis[i] = true; 34 if (dfs(m, rest - stick[i], i + 1)) 35 return true; 36 vis[i] = 0; 37 if (stick[i] == rest) 38 return false; 39 if (i == c&&rest == can) 40 return false; 41 } 42 return false; 43 } 44 } 45 46 bool cmp(int a, int b) 47 { 48 return a > b; 49 } 50 51 int main() 52 { 53 while (scanf("%d", &n) == 1 && n) 54 { 55 int i, m = 0; 56 sum = 0; 57 for (i = 0; i < n; i++) 58 { 59 scanf("%d", stick + i); 60 sum += stick[i]; 61 m = max(m, stick[i]); 62 } 63 sort(stick, stick + n, cmp); 64 i = 64; 65 if (sum / m<i) i = sum / m; 66 for (i; i>0; i--) 67 { 68 if (sum%i) 69 continue; 70 can = sum / i; 71 memset(vis, 0, sizeof(vis)); 72 if (dfs(i, can, 0)) 73 break; 74 } 75 printf("%d ", can); 76 } 77 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int a[69];//存小棒长大小最好大于64 6 int visited[69];//标记小棒是否被用 7 int n;//小棒个数 8 int cmp(const void *a,const void *b) 9 { 10 return *(int*)b-*(int*)a; 11 } 12 /*DFS:递归搜索,以剩下小棒数和拼接一根棒所需长度作为递归参数*/ 13 /*作为搜索的棒长len,拼成len长的棒还需的长度remains_len,剩下的未作为拼接的小棒个数num*/ 14 int DFS(int len,int remains_len,int num) 15 { /*当还需长度为0且未作接接的小棒个数为0时,说明拼接成功*/ 16 if(remains_len==0&&num==0) 17 return len; 18 /*当完成拼接一个棒时,要重新进行下一根木棒拼接,给remains_len重新赋值*/ 19 if(remains_len==0) 20 remains_len=len; 21 for(int i=0;i<n;i++) 22 { /*已经作为成功拼接的小棒,不再作为拼接对象*/ 23 if(visited[i]==1) 24 continue; 25 if(remains_len>=a[i]) 26 { /*暂时标记为已经用了*/ 27 visited[i]=1; 28 /*当接受这根小棒a[i],能完成所有任务,则返回成功拼接棒长*/ 29 if(DFS(len,remains_len-a[i],num-1)) 30 return len; 31 /*当用a[i]不能完成任务就不用此棒,将其标记为该步未用此棒*/ 32 visited[i]=0; 33 /*a[i]=remains_len或remians_len=len时本来表示即将拼成一根棒, 34 或刚进行新一轮拼接(即if中判断的是主问题性质完全相同的子问题) 35 但经过上面的if判断,子问题不能完成所有任务, 36 那么整体不可能完成任务,不再考虑,搜索失败(剪枝)*/ 37 if(a[i]==remains_len||len==remains_len) 38 break; 39 /*当a[i]不能完成任务,与它相同的小棒也不能完成任务(剪枝) */ 40 while(a[i]==a[i+1]) 41 i++; 42 } 43 } 44 return 0; 45 } 46 int main() 47 { 48 while(scanf("%d",&n) && n) 49 { 50 int sum=0;//所有小棒长度 51 int len,k; 52 for(int i=0;i<n;i++) 53 { 54 scanf("%d",&a[i]); 55 sum+=a[i]; 56 } 57 qsort(a,n,sizeof(int),cmp); 58 for(len=a[0];len<=sum;len++) 59 { 60 memset(visited,0,sizeof(visited));/*每次尝试都要置所有小棒为可用*/ 61 if(sum%len==0) 62 { 63 k=DFS(len,0,n); 64 if(k) 65 break; 66 } 67 } 68 printf("%d ",k); 69 } 70 return 0; 71 }
7、Checksum algorithm
1 #include <string.h> 2 #include <stdio.h> 3 4 int main() 5 { 6 char str[10000]; 7 int len,i,a[26],sum; 8 for(i = 0;i<26;i++) 9 { 10 a[i] = i+1; 11 } 12 while(gets(str)) 13 { 14 if(strcmp(str,"#") == 0) 15 break; 16 len = strlen(str); 17 sum = 0; 18 for(i = 0;i<len;i++) 19 { 20 if(str[i]>='A' && str[i]<='Z') 21 sum+=(i+1)*a[str[i]-'A']; 22 } 23 printf("%d ",sum); 24 } 25 26 return 0; 27 }
8、Scoring
1 #include <stdio.h> 2 #include <string.h> 3 #define N 256 4 int main() 5 { 6 int n; 7 int submit, time, firstsub = -1, firsttime = -1, number, use_time, t; 8 char team[N] = "", firstteam[N] = ""; 9 10 scanf("%d",&n); 11 12 while (n--) 13 { 14 scanf("%s",team); 15 use_time = t = 0; 16 for (number = 0; number<4; number++) 17 { 18 scanf("%d%d",&submit,&time); 19 if (time>0){ 20 use_time += time + 20*(submit - 1); 21 t++; 22 } 23 } 24 //解题数量多||解题数量一致但用时少 25 if ((t>firstsub) || (t == firstsub && use_time<firsttime)) 26 { 27 strcpy(firstteam,team); 28 firstsub = t; 29 firsttime = use_time; 30 } 31 } 32 33 printf("%s %d %d ", firstteam,firstsub,firsttime); 34 return 0; 35 }
9、Redistribute Wealth
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int Money[1000001],C[1000001]; 5 6 int cmp(const void *a,const void *b){ 7 return *(int *)a - *(int *)b; 8 } 9 //绝对值 10 int abs(int a,int b){ 11 if(a < b){ 12 return b - a; 13 } 14 else{ 15 return a - b; 16 } 17 } 18 19 int main () 20 { 21 long long int i,N,Min,Sum,M; 22 while(scanf("%lld",&N)==1 && N) 23 { 24 Sum = 0; 25 Min = 0; 26 //N个人 27 for(i = 0;i < N;i++){ 28 scanf("%d",&Money[i]); 29 Sum += Money[i]; 30 } 31 //平均值 32 M = Sum / N; 33 C[0] = 0; 34 //初始化C数组 35 for(i = 1;i < N;i++){ 36 C[i] = C[i-1] + Money[i] - M; 37 } 38 //排序,选择中位数 39 qsort(C,N,sizeof(int),cmp); 40 long long int x1 = C[N/2]; 41 //计算转移的金币数 42 for(i = 0;i < N;i++){ 43 Min += abs(x1,C[i]); 44 } 45 printf("%lld ",Min); 46 } 47 return 0; 48 }
10、Dexter need help
1 #include<stdio.h> 2 #include<math.h> 3 4 int main() 5 { 6 int n,ans; 7 while(scanf("%d",&n)==1 && n) 8 { 9 ans=log10(n)/log10(2)+1; 10 printf("%d ",ans); 11 } 12 return 0; 13 }