Menu Close

Compute the amount of money to be handed over with conditions

Mohan is very particular about numbers. There are ‘K’ digits that he dislikes: D1, D2,…Dk. Mohan is shopping, and now paying at the cashier. His total is ‘N’ yen (the currency of Japan), thus he has to hand at least ‘N’ yen to the cashier (and possibly receive the change).

However, as mentioned before, he is very particular about numbers. When he hands money to the cashier, the decimal notation of the amount must not contain any digits that he dislikes. Under this condition, he will hand the minimum amount of money.

Input:
The input is given from Standard Input in the following format:
N K
D1 D2...Dk 

Output:
Print the output the amount of money that mohan will hand to the cashier.
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int N, K, x, re,i;
int b[10];
bool check(int x);
int main(){ scanf("%d %d", &N, &K);
  int *D = (int *)malloc(sizeof(int)*K);
  for( i = 1; i <= K; i++){
    scanf("%d", D);
    b[*D]++;
  }
  for( i = N;;i++){
    if(check(i)){
      re = i;
      break;
    }
  }
  printf("%d",re);
return 0;
}
bool check(int x){
 while(x){
 if(b[x%10])return 0;
 x/=10;
}
 return true;
}

INPUT_1:
1021  8
1  2  3  4  5  6  7  8

OUTPUT:
9000


INPUT_2:
1001  7
4  3  5  7  2  8  3

OUTPUT:
1001


INPUT_3:
2500  5
1  2  3  4  5

OUTPUT:
6000


INPUT_4:
5570  6
2  1  5  4  3  6

OUTPUT:
7000


ILLUSTRATION

EXECUTED USING gcc

Morae Q!