【AtCoder】ABC042 C. こだわり者いろはちゃん
AtCoderの問題
C: こだわり者いろはちゃん / Iroha's Obsession - AtCoder Beginner Contest 042 | AtCoder
参考書
難しさ(初心者目線)
・考え方*
・実装**
・面白さ*
問題概要
・が与えられる.
・0~9までのうち使用できない数字が与えられる.
・使用できる数値でN以上の最小値を求めよ.
方針
・Nが小さいので全探索できる(1~10*Nまで調べていく).
・1桁ずつ小さい数字から使用できるかを調べていく方法だとN=999で9が使えない時に繰り上がりを考えないといけないので面倒.
コード
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <cmath> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <stdlib.h> #include <stdio.h> using namespace std; #define ll long long #define PI acos(-1.0) #define FOR(I,A,B) for(int I = (A); I < (B); ++I) int main(){ int N; int K; cin>>N>>K; bool D[10];//嫌な数字ならtrue FOR(i, 0, 10) D[i] = false; FOR(i, 0, K){ int d; cin>>d; D[d] = true; } //全探索 FOR(i, 0, 10*N){ if(N<=i){ int check = true; string s = to_string(i); FOR(j, 0, s.length()){ if(D[s[j]-'0']) check = false; } if(check){ cout << i << endl; return 0; } } } }