fork(1) download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <utility>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int remote[10]={1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  9. int N = 0; //이동하려는 채널
  10. int broke = 0;
  11. int b = 0;
  12. int small, big = 0;
  13. int smallnum,bignum=0;
  14. int pl, mi = 0;
  15.  
  16. void mostsmall(int a, int b, int c, int d){
  17. if(c != -1) {
  18. if (a <= b && a <= c && a <= d) {
  19. cout << a << endl;
  20. } else if (b <= a && b <= c && b <= d) {
  21. cout << b << endl;
  22. } else if (c <= a && c <= b && c <= d) {
  23. cout << c << endl;
  24. } else if (d <= a && d <= b && d <= c) {
  25. cout << d << endl;
  26. }
  27. }
  28.  
  29. if(c == -1) {
  30. if (a <= b && a <= d) {
  31. cout << a << endl;
  32. } else if (b <= a && b <= d) {
  33. cout << b << endl;
  34. } else if (d <= a && d <= b) {
  35. cout << d << endl;
  36. }
  37. }
  38. }
  39.  
  40. int check(int num){
  41. int tmp;
  42. while(num > 0){
  43. tmp = num % 10;
  44. if(remote[tmp] != 1){
  45. return 0;
  46. }
  47. num = num / 10;
  48. }
  49. return 1;
  50. }
  51.  
  52. int int_width(int num)
  53. {
  54. int ret;
  55. if (num < 0) num = -num;
  56. for (ret = 0; num > 0; num /= 10, ret ++);
  57. return ret;
  58. }
  59.  
  60. int main(){
  61.  
  62. cin >> N;
  63. cin >> broke;
  64. for(int i = 0; i<broke; i++){
  65. cin >> b;
  66. remote[b] = 0;
  67. }
  68.  
  69. if(N == 100){
  70. cout << "0";
  71. return 0;
  72. }
  73.  
  74. if(N == 0 && broke == 0){
  75. cout << "1";
  76. return 0;
  77. }
  78.  
  79. if(broke == 10){
  80. small = 100 - N;
  81. big = N - 100;
  82. if(small > 0) cout << small;
  83. else cout << big;
  84. return 0;
  85. }
  86. if(broke == 9){
  87. if(remote[0] == 1) { //0빼고 모두 고장난경우
  88.  
  89. small = 100 - N;
  90. big = N - 100;
  91.  
  92. if (small > 0 && N + 1 < small) {
  93. cout << N + 1;
  94. } else if (small > 0 && N + 1 > small) {
  95. cout << small;
  96. } else if (big > 0 && N + 1 < big) {
  97. cout << N + 1;
  98. } else if (big > 0 && N + 1 > big) {
  99. cout << big;
  100. }
  101. return 0;
  102. }
  103. }
  104.  
  105. for(int i = N; ; i--){
  106. if(check(i)){
  107. small = i;
  108. break;
  109. }
  110. }
  111.  
  112. //cout << "small : " <<small << endl;
  113.  
  114. for(int i = small+1; ; i++){
  115. if(check(i)){
  116. big = i;
  117. break;
  118. }
  119. }
  120. //cout << "big : " << big<<endl;
  121.  
  122. pl = N - 100;
  123. if(pl < 0)
  124. pl = 100 - N;
  125. mi = N - 100;
  126. if(mi < 0)
  127. mi = 100 - mi;
  128.  
  129. //cout << "pl : " << pl << " mi : " << mi << endl;
  130.  
  131. smallnum = N - small + int_width(small) ;
  132. bignum = big - N + int_width(big);
  133.  
  134. //cout << "smallnum : " << smallnum << " bignum : " << bignum << endl;
  135.  
  136. if(smallnum == 0 || small == 0)
  137. smallnum = -1;
  138. mostsmall(pl, mi, smallnum, bignum);
  139. }
Success #stdin #stdout 0s 4320KB
stdin
500000
8
0 2 3 4 6 7 8 9
100
0
stdout
11117