|
|
back to board1007 #include <iostream.h> #include <string.H> #include <stdio.h> void Done1() ; void Done2() ; void Done3() ; int N ; char a[1010] ; int tot [2] ; long sum1 ; void main() { cin >> N ; char ss[1010] = "X" ; cin >> ss ; while ( ss[0] != 0 ) { sum1 = 0 ; tot[0]=0 ; tot [ 1] = 0 ; int ai = strlen ( ss ) ; for ( int i = 0 ;i < ai ;i ++ ) { if ( ss[i] =='0' ) tot[0] ++ ; else { tot[1] ++ ; sum1 += i + 1; } } strcpy ( a , ss ) ; if ( ai == N ) Done1 () ; else if ( ai == N-1 ) Done2 () ; else if ( ai == N+1 ) Done3 () ; ss[0] = 'X' ; ss[1] = 0 ; cin >> ss ; } } void Done1 () { if ( tot[0]==tot[1] || sum1 % ( N+1 ) == 0 ) cout << a << endl ; else if ( tot [0] == tot[1] -2 ) { for ( int i = 0 ; i < N ;i ++ ) { if ( a[i] == '1' ) { a[i] = '0' ; cout << a <<endl ; break ; } } } else if ( tot[0]==tot[1]+2 ) { for ( int i = 0 ; i < N ;i ++ ) { if ( a[i] == '0' ) { a[i] = '1' ; cout << a <<endl ; break ; } } } else { sum1 %= N+1 ; if ( a[ sum1-1 ] == '1' ) { a[sum1-1] = '0' ; cout << a <<endl ; } else if ( a[N+1-sum1-1]=='0' ) { a[N+1-sum1-1] ='1' ; cout << a <<endl ; } } } void Done2() //short { int ff = 0 ; if ( N%2==0 ){ if (tot[0]==tot[1]-1 ) { strcat ( a , "0" ) ; ff = 1; cout << a << endl ; } else if ( tot[0]==tot[1]+1 ) { strcat ( a , "1" ) ; ff =1 ; cout << a << endl ; } } if ( !ff ) { int sum1o = sum1 ; char ccc ; int iii ; strcat ( a , "0" ) ; for ( int i = N-1 ; i >= 0 ; i-- ) { if ( a[i] =='1' ) sum1o+= 1 ; if ( sum1o% (N+1) == 0 ) { iii = i ; ccc = '0' ; break ; } else if ( (sum1o+i+1)%(N+1) == 0 ) { iii = i ; ccc = '1' ; break ; } } if ( i>= 0 ) { for ( int j = N-1 ; j >= iii+1 ;j -- ) a[j] = a[j-1] ; a[iii]=ccc ; cout << a <<endl ; } } } void Done3() //long { int iii = -1 ; if ( N%2==0 ){ if (tot[0]==tot[1]-1 ) { for ( int i = 0 ;i <= N ; i ++ ) { if ( a [i ]=='1' ) { iii = i ; break ; } } } else if ( tot[0]==tot[1]+1 ) { for ( int i = 0 ;i <= N ; i ++ ) { if ( a [i ]=='0' ) { iii = i ; break ; } } } } if ( iii == -1 ) { int sum1o = sum1 ; for ( int i = N-1 ; i >= 0 ; i-- ) { if ( a[i+1] =='1' ) sum1o-= 1 ; int xx = sum1o ; if ( a[i] == '1' ) xx -= i +1 ; if ( xx% (N+1) == 0 ) { iii= i ; break ; } } } if ( iii != -1 ) { for ( int i = iii ;i <= N-1 ; i++ ){ a[i ] = a[i+1] ; } a[N ] = 0 ; cout << a <<endl ; } } |
|
|