|
|
back to boardPlease help me!!!!! Who can tell me why I got WA? const Max=1005; var N:integer; procedure work; var work_i,work_j,work_k,modi_j,work_temp1,work_temp2:integer; this_str,Modi_chr:char; work_s:longint; data_str:array[1..Max] of char; work_save:array[0..max] of integer; begin readln(N);work_i:=0; while not eof do begin work_s:=0;work_j:=0; fillchar(work_save,sizeof(work_save),0); fillchar(data_str,sizeof(data_str),'0'); while not eoln do begin read(this_str); if this_str<>' ' then begin inc(work_j);data_str[work_j]:=this_str; work_save[work_j]:=ord(this_str)-48; if work_save[work_j]=1 then work_s:=work_s+work_j; end; end; if work_j>0 then begin work_temp1:=work_save[work_j];work_save[work_j]:=0; for work_k:=work_j-1 downto 0 do begin work_temp2:=work_temp1; work_temp1:=work_save[work_k]; work_save[work_k]:=work_save[work_k+1]+work_temp2; end; if work_j<N then begin work_k:=0;Modi_j:=0; repeat inc(work_k); if (work_s+work_save[work_k-1]) mod (n+1) =0 then begin Modi_j:=work_k;modi_chr:='0'; end else if (work_s+work_k+work_save[work_k- 1])mod(n+1)=0 then begin modi_j:=work_k;modi_chr:='1'; end; until Modi_j>0; end else if work_j>N then begin work_k:=0;Modi_j:=0; repeat inc(work_k); if data_str[work_k]='1' then if (work_s-work_save[work_k]-work_k) mod (n+1)=0 then
modi_j:=work_k else else if (work_s-work_save[work_k]) mod(n+1)=0 then modi_j:=work_k; until Modi_j>0; end else begin work_k:=0;modi_j:=0;modi_chr:='0'; repeat inc(work_k); if data_str[work_k]='1' then if (work_s-work_k) mod (n+1)=0 then modi_j:=work_k; until work_k>N; end; for work_k:=1 to Modi_j-1 do write(data_str[work_k]); if (work_j<=n)and(modi_j>0) then write(modi_chr); if (work_j<N)and(modi_j<N) then write(data_str[modi_j]); for work_k:=modi_j+1 to work_j do write(data_str[work_k]); writeln; end; readln; end; end; begin work; end. Thanks!!!! I am not good at Pascal, I hope my c++ code will help you a little. You just need to test all the possiblities. if len_of_word == N check if it's right or relace 0 with 1 if len_of_word == N-1 check one symbol is missing if len_of_word == N+1 check one symbol is added #include <iostream> #include <cstring> using namespace std; const int MAX_N = 1001; char word[MAX_N]; int ones[MAX_N]; int N; void Print(int st, int ed) { for (int i = st; i <= ed; ++i) { cout << word[i]; } } int main() { cin >> N; while (1) { char ch; int idx = 0; if (!(cin >> ch)) return 0; while (ch != '\n') { if (ch == '0' || ch == '1') word[idx++] = ch; if (!cin.get(ch)) return 0; } word[idx] = '\0'; memset(ones, 0, sizeof(ones));
int len = strlen(word); int total = 0; for (int i = 0; i < len; ++i) { if (word[i] == '1') { total += (i+1); ones[i] = 1; } } for (int i = len-2; i >= 0; --i) { ones[i] += ones[i+1]; } // the word is right if (len == N && total % (N+1) == 0) { cout << word << endl; continue; } // Any symbol 0 is replaced by 1 if (len == N) { for (int i = 0; i < len; ++i) { if (word[i] == '1' && (total-(i+1))%(N+1) == 0) { word[i] = '0'; cout << word; break; } } } else // Any symbol is removed if (len == N-1) { // insert an 0 or 1 before pos i for (int i = 0; i <= len; ++i) { if ( (total+ones[i])%(N+1) == 0 ) { Print(0, i-1); cout << '0'; Print(i, len-1); break; } else if ( (total+ones[i]+(i+1))%(N+1) == 0) { Print(0, i-1); cout << '1'; Print(i, len-1); break; } } } else // A symbol is inserted at any position if (len == N+1) { for (int i = 0; i < len; ++i) { if ((word[i] == '1' && (total-ones[i+1]-(i+1))%(N+1) == 0) || (word[i] == '0' && (total-ones[i+1])%(N+1) == 0)) { Print(0, i-1); Print(i+1, len-1); break; } } } cout << endl; } // return 0; } |
|
|