题目:http://codeforces.com/contest/250/problem/A
简单题目
View Code
1 int main() 2 { 3 int i,j; 4 int n; 5 int a[110]; 6 int tnum[110]; 7 while(scanf("%d",&n) != EOF) 8 { 9 _clr(tnum,0); 10 for(i = 0; i < n; i++) 11 scanf("%d",&a[i]); 12 int sum = 0; 13 int tsum = 0; 14 int k = 0; 15 for(i = 0; i < n; i++) 16 { 17 if(a[i] < 0) sum ++; 18 tsum ++; 19 if(sum == 3) 20 { 21 //cout<<"i = "<<i<<endl; 22 tnum[k++] = tsum - 1; 23 tsum = 0; 24 sum = 0; 25 i --; 26 } 27 } 28 tnum[k ++] = tsum; 29 printf("%d\n",k); 30 printf("%d",tnum[0]); 31 for(i = 1; i < k; i++) 32 printf(" %d",tnum[i]); 33 cout<<endl; 34 } 35 return 0; 36 }
题目:http://codeforces.com/contest/250/problem/B
字符串的处理题目,写的好纠结。先是处理那些两个':'中间那些不够四个的补零,然后如果是两个':',则保留不做处理,最后补零
View Code
1 const int N = 50; 2 int main() 3 { 4 char str[N]; 5 char sbr[N]; 6 int n,i,j; 7 //freopen("data.txt","r",stdin); 8 scanf("%d",&n); 9 getchar(); 10 while(n--) 11 { 12 _clr(str,0); 13 _clr(sbr,0); 14 int num = 0; 15 cin>>str; 16 //gets(str); 17 //puts(str); 18 if(strlen(str) == 2) 19 { 20 printf("0000:0000:0000:0000:0000:0000:0000:0000\n"); 21 continue; 22 } 23 int len = strlen(str); 24 for(i = 0; i < len; i++) 25 { 26 if(str[i] == ':') num ++; 27 } 28 if(num == 8 && str[0] == ':' && str[1] == ':') 29 { 30 str[0] = '0'; 31 } 32 33 if(len == 39) 34 { 35 printf("%s\n",str); 36 continue; 37 } 38 int sum = 0; 39 int k = 0; 40 int temp; 41 for(i = 0; i < len; i++) 42 { 43 if(str[i] != ':') 44 { 45 sum ++; 46 } 47 else if(str[i - 1] != ':') 48 { 49 temp = 4 - sum; 50 while(temp --) sbr[k ++] = '0'; 51 for(j = i - sum; j <= i; j++) sbr[k ++] = str[j]; 52 sum = 0; 53 } 54 else {sbr[k ++] = ':';sum = 0;} 55 } 56 if(k == 36 && sbr[k - 1] == ':' && sbr[k - 2] == ':') 57 { 58 for(i = 0; i < k - 1; i++) 59 printf("%c",sbr[i]); 60 for(i = 0; i < 4; i++) printf("0"); 61 printf("\n"); 62 continue; 63 } 64 if(sum) 65 { 66 temp = 4 - sum; 67 while(temp --) sbr[k++] = '0'; 68 for(j = i - sum; j < i; j++) sbr[k ++] = str[j]; 69 } 70 //cout<<"sbr = "<<sbr<<endl; 71 //cout<<"sum = "<<k<<endl; 72 temp = 39 - k; 73 if(sbr[k - 2] == ':' && sbr[k - 1] == ':') temp ++; 74 for(i = 0; i < k; i++) 75 { 76 if(sbr[i] == ':' && sbr[i + 1] == ':') 77 { 78 printf("%c",sbr[i]); 79 for(j = 1; j <= temp; j++) 80 { 81 if(j && j % 5 == 0) printf(":"); 82 else printf("0"); 83 } 84 i++; 85 if(i != (k - 1)) printf(":"); 86 } 87 else printf("%c",sbr[i]); 88 } 89 cout<<endl; 90 91 } 92 return 0; 93 }