|
|
back to boardP1007(Code words).Please help Posted by Mih 31 Dec 2001 01:12 what's wrong with this code: #include<iostream.h> #include<string.h> #define MAXN 1006 void solve(); void outp(int a[MAXN]); int N; int main(){ cin>>N; while(cin&&!cin.eof()) solve(); return 0; } void solve(){ int a[MAXN]; char s[MAXN]; int kvo1=0,sum=0,pos=1; cin>>s; int len=strlen(s); for(int i=0;i<len;i++){ if(s[i]=='1') sum+=pos,kvo1++; a[pos-1]=s[i]-'0'; pos++; } if(pos==(N+1)){ if(sum%(N+1)==0){ outp(a); return; } for(int i=0;i<pos;i++) if(a[i]) if((sum-i-1)%(N+1)==0){ a[i]=0; outp(a); return; } } if(pos==N){ int pass=0; for(int i=0;i<=pos;i++){ if((sum+kvo1-pass)%(N+1)==0){ for(int j=pos-1;j>i;j--) a[j]=a[j-1]; a[i]=0; outp(a); return; } if((sum+kvo1-pass+i+1)%(N+1)==0){ for(int j=pos-1;j>i;j--) a[j]=a[j-1]; a[i]=1; outp(a); return; } if(a[i]) pass++; } } if(pos==N+2){ int pass=0; for(int i=0;i<=pos;i++){ if(a[i]){ pass++; if((sum-kvo1+pass-i-1)%(N+1)==0){ for(int j=i;j<=pos;j++) a[j]=a[j+1]; outp(a); return; } } if(!a[i]){ if((sum-kvo1+pass)%(N+1)==0){ for(int j=i;j<=pos;j++) a[j]=a[j+1]; outp(a); return; } } } } } void outp(int a[MAXN]){ for(int i=0;i<N;i++) cout<<a[i]; cout<<"\n"; } |
|
|