Posted on 2008-04-08 14:20
superman 阅读(239)
评论(0) 编辑 收藏 引用 所属分类:
URAL
1 /* Accepted 1.968 204 KB */
2 #include <string>
3 #include <iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 int n;
10 cin >> n;
11 string word;
12 while(cin >> word)
13 {
14 if(word.size() == n - 1)
15 for(int i = 0; i < n; i++)
16 {
17 int sum = 0;
18 for(int j = 0;j < i; j++)
19 sum += (word[j] == '1' ? j + 1: 0);
20 for(int j = i; j < n - 1; j++)
21 sum += (word[j] == '1' ? j + 2: 0);
22 if(sum == 0 || sum % (n + 1) == 0 || (sum + i + 1) % (n + 1) == 0)
23 {
24 for(int j = 0;j < i; j++)
25 cout << word[j];
26 cout << (sum % (n + 1) == 0 ? '0' : '1');
27 for(int j = i; j < n - 1; j++)
28 cout << word[j];
29 cout << endl;
30 break;
31 }
32 }
33 if(word.size() == n)
34 {
35 for(int i = 0; i < n; i++)
36 if(word[i] == '1')
37 {
38 int sum = 0;
39 word[i] = '0';
40 for(int j = 0; j < n; j++)
41 sum += (word[j] == '1' ? j + 1 : 0);
42 if(sum == 0 || sum % (n + 1) == 0)
43 break;
44 word[i] = '1';
45 }
46 cout << word << endl;
47 }
48 if(word.size() == n + 1)
49 for(int i = 0; i < n + 1; i++)
50 {
51 int sum = 0;
52 for(int j = 0; j < i; j++)
53 sum += (word[j] == '1' ? j + 1 : 0);
54 for(int j = i + 1; j < n + 1; j++)
55 sum += (word[j] == '1' ? j : 0);
56 if(sum == 0 || sum % (n + 1) == 0)
57 {
58 for(int j = 0; j < i; j++)
59 cout << word[j];
60 for(int j = i + 1; j < n + 1; j++)
61 cout << word[j];
62 cout << endl;
63 break;
64 }
65 }
66 }
67
68 return 0;
69 }
70