分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了.
/* 1.没有发生改变,check一下 2.将一位1变成0 3.添一位 4.减一位 */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n,len,ss[3010],b[3010]; char s[3010]; bool flag = false; bool check() { int res = 0; for (int i = 1;i <= n; i++) if (ss[i] == 1) res += i; if (res % (n + 1) == 0) return true; return false; } bool check2() { int res = 0; for (int i = 1; i <= n; i++) if (b[i] == 1) res += i; if (res % (n + 1) == 0) return true; return false; } void print() { for (int i = 1; i <= n; i++) printf("%d",ss[i]); printf("\n"); } void add(int x,int y) { memcpy(b,ss,sizeof(ss)); for (int i = n; i > x; i--) b[i] = b[i-1]; b[x] = y; if (check2()) { memcpy(ss,b,sizeof(ss)); flag = 1; } } void eras(int x) { memcpy(b,ss,sizeof(ss)); for (int i = x; i <= n; i++) b[i] = b[i + 1]; if (check2()) { memcpy(ss,b,sizeof(ss)); flag = 1; } } int main() { scanf("%d",&n); while(scanf("%s",s + 1) != EOF) { memset(b,0,sizeof(b)); len = strlen(s + 1); flag = false; if (abs(n - len) > 1) { printf("-1\n"); continue; } for (int i = 1; i <= len; i++) ss[i] = s[i] - '0'; if (n == len) { if (check()) //1 { print(); continue; } for (int i = 1; i <= n; i++) //2 { if (ss[i] == 1) { ss[i] = 0; if (check()) { flag = 1; break; } ss[i] = 1; } } } if (flag) { print(); continue; } if (len == n - 1) { for (int i = 1; i <= n + 1; i++) //3 for (int j = 0; j <= 1; j++) { add(i,j); //i位以及以后都要往后挪1 if (flag) break; } } if (flag) { print(); continue; } if (len == n + 1) { for (int i = 1; i <= n; i++) { eras(i); if (flag) break; } } if (flag) { print(); continue; } printf("-1\n"); } return 0; }