Advertisement
Guest User

Untitled

a guest
Oct 28th, 2013
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <memory.h>
  5. #include <ctype.h>
  6. #include <vector>
  7. #include <stack>
  8. #include <queue>
  9. #include <map>
  10. #include <algorithm>
  11.  
  12. using namespace std;
  13.  
  14. int n;
  15.  
  16. int sum(string s)
  17. {
  18. int r = 0;
  19. for (int i = 0; i < n; i++)
  20. if (s[i] == '1')
  21. r += i+1;
  22. return r;
  23. }
  24.  
  25. int main()
  26. {
  27. #ifndef ONLINE_JUDGE
  28. freopen("input.txt", "rt", stdin);
  29. freopen("output.txt", "wt", stdout);
  30. #endif
  31.  
  32. cin >> n;
  33. string s, t;
  34.  
  35. int k;
  36.  
  37. while (getline(cin, t))
  38. {
  39. s = "";
  40. for (int i = 0; i < t.length(); i++)
  41. if (t[i] == '0' || t[i] == '1')
  42. s += t[i];
  43.  
  44. k = s.length();
  45.  
  46. if (k == 0)
  47. continue;
  48.  
  49. if (k == n)
  50. {
  51. if (sum(s) % (n+1) == 0)
  52. cout << s << "\n";
  53. else
  54. for (int i = n-1; i >= 0; i--)
  55. if (s[i] == '1')
  56. {
  57. s[i] = '0';
  58. if (sum(s) % (n+1) == 0)
  59. {
  60. cout << s << "\n";
  61. break;
  62. }
  63. s[i] = '1';
  64. }
  65. } else
  66. if (k > n)
  67. {
  68. for (int i = 0; i < k; i++)
  69. {
  70. t = s.substr(0,i) + s.substr(i+1);
  71. if (sum(t) % (n+1) == 0)
  72. {
  73. cout << t << "\n";
  74. break;
  75. }
  76. }
  77. } else
  78. {
  79. for (int i = 0; i <= k; i++)
  80. for (char c = '0'; c <= '1'; c++)
  81. {
  82. t = s.substr(0,i) + c + s.substr(i);
  83. if (sum(t) % (n+1) == 0)
  84. {
  85. cout << t << "\n";
  86. break;
  87. }
  88. }
  89. }
  90.  
  91. if (cin.eof())
  92. break;
  93. }
  94.  
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement