Posted on 2008-08-17 02:11
Hero 阅读(182)
评论(0) 编辑 收藏 引用 所属分类:
代码如诗--ACM
1 // 1007 C++ Accepted 1.531 153 KB
2
3 //开始数组开的太小,WA了好多
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8
9 int inn ;
10 char word[10010] ; int len ;
11 char tword[10010] ; int tlen ;
12 int sum ;
13
14 void fchange()
15 {
16 sum = 0 ;
17 for( int i=0; i<len; i++ ) {
18 if( '1' == word[i] ) sum += (i+1) ;
19 }
20 if( 0 == sum % (inn+1) ) {
21 printf( "%s\n", word ) ; return ;
22 }
23 for( int i=0; i<len; i++ ) {
24 if( '0' == word[i] ) continue ;
25 sum = 0 ;
26 strcpy( tword, word ) ; tword[i] = '0' ;
27 for( int k=0; k<len; k++ ) {
28 if( '1' == tword[k] ) sum += (k+1) ;
29 }
30 if( 0 == sum % (inn+1) ) {
31 printf( "%s\n", tword ) ; return ;
32 }
33 }
34 }
35
36 void finsert()
37 {
38 for( int i=inn-1; i>=0; i-- ) {
39 tword[i] = '0' ; tword[inn] = '\0' ;
40 for( int sn=0; sn<i; sn++ ) tword[sn] = word[sn] ;
41 for( int sn=i+1; sn<inn; sn++ ) tword[sn] = word[sn-1] ;
42 sum = 0 ;
43 for( int k=0; k<inn; k++ ) {
44 if( '1' == tword[k] ) sum += (k+1) ;
45 }
46 if( 0 == sum % (inn+1) ) {
47 printf( "%s\n", tword ) ; return ;
48 }
49
50 tword[i] = '1' ;
51 if( 0 == (sum+i+1)%(inn+1) ) {
52 printf( "%s\n", tword ) ; return ;
53 }
54 }
55 }
56
57 void fdelete()
58 {
59 for( int i=len-1; i>=0; i-- ) {
60 for( int sn=0; sn<i; sn++ ) tword[sn] = word[sn] ;
61 for( int sn=i; sn<inn; sn++ ) tword[sn] = word[sn+1] ;
62 tword[inn] = '\0' ; sum = 0 ;
63 for( int k=0; k<inn; k++ ) {
64 if( '1' == tword[k] ) sum += (k+1) ;
65 }
66 if( 0 == sum % (inn+1) ) {
67 printf( "%s\n", tword ) ; return ;
68 }
69 }
70 }
71
72
73 int main()
74 {
75 //freopen( "in.txt", "r", stdin ) ;
76 //freopen( "in.txt", "r", stdin ) ;
77
78 scanf( "%d", &inn ) ;
79 while( scanf( "%s", word ) != EOF )
80 {
81 len = strlen( word ) ;
82
83 if( inn == len ) fchange() ;
84 else if( inn-1 == len ) finsert() ;
85 else if( inn+1 == len ) fdelete() ;
86 }
87
88 return 0 ;
89 }