|
|
back to boardWhat can be wrong here? Help me, if you can... Posted by Denis 18 Jan 2008 02:00 #include <fstream> #include <stdio.h> #include <iostream> #include <string> using namespace std; int n; string S; int arr[1010] = {0}; char s[1010]; int main () { //freopen ("a.in", "r", stdin); //freopen ("a.out", "w", stdout); int i, j, k, t, sum; int tsum; scanf ("%d", &n); i = 0; S = ""; char c; while (scanf ("%c", &c) == 1) { //cin>>S; //cout<<i<<" "; if (c == '\n' && S != "") { //S = ""; /*for (j = 0; s[j] > 0; ++j) { if (s[j] == '0' || s[j] == '1') { S += s[j]; } }*/ if (i > 0) { cout<<endl; } ++i; sum = 0; tsum = 0; if (S.length() == n) { for (j = 0; j < n; ++j) { if (S[j] == '1') { sum += (j+1); } } sum %= (n+1); if (sum == 0) { cout<<S; S = ""; } else { S[sum-1] = '0'; //S = ""; cout<<S; S = ""; } } else if (S.length() > n) { t = 0; for (j = 0; j < S.length(); ++j) { if (S[j] == '1') { sum += (j+1); ++t; } arr[j] = sum%(n+1); } sum %= (n+1); /*for (j = S.length()-1; j >= 0; --j) { if (S[j] == '1') { tsum += (j+1); } arr[j][1] = tsum%(n+1); }*/ if ((sum+n+1-t)%(n+1) == 0) { S = S.substr (1, n); cout<<S; S = ""; continue; } t = 0; tsum = 0; for (j = S.length()-1; j > 0; --j) { if ((tsum-t+arr[j-1])%(n+1) == 0) { for (k = 0; k < j; ++k) { cout<<S[k]; } for (k = j+1; k < S.length(); ++k) { cout<<S[k]; } //cout<<endl; //continue; S = ""; break; } if (S[j] == '1') { ++t; tsum += (j+1); } } } else { sum = 0, tsum = 0, t = 0; int add; for (j = 0; j < S.length(); ++j) { if (S[j] == '1') { sum += (j+1); } arr[j] = sum%(n+1); } sum %= (n+1); //arr[n-1] = sum; if (sum == 0) { cout<<S<<0; S = ""; continue; } else if ((sum+n)%(n+1) == 0) { cout<<S<<1; S = ""; continue; } for (j = S.length()-1; j >= 0; --j) { if ((arr[j]+tsum+t)%(n+1) == 0) { for (k = 0; k <= j; ++k) { cout<<S[k]; } cout<<0; for (k = j+1; k < S.length(); ++k) { cout<<S[k]; } //cout<<endl; S = ""; break; } if ((arr[j]+tsum+t+j+1)%(n+1) == 0) { for (k = 0; k <= j; ++k) { cout<<S[k]; } cout<<1; for (k = j+1; k < S.length(); ++k) { cout<<S[k]; } S = ""; //cout<<endl; break; } if (S[j] == '1') { ++t; tsum += (j+1); } } } S = ""; } else { if (c == '0' || c == '1') { S += c; } } } return 0; } |
|
|