Programing/백준, 프로그래머스(C++)
[C++][백준 1515] 수 이어쓰기
hye3193
2025. 1. 1. 23:20
https://www.acmicpc.net/problem/1515
제출코드
#include <iostream>
#include <string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
string numbers;
cin >> numbers;
int n, idx = 0;
string strN;
while(idx < numbers.length())
{
n++;
strN = to_string(n);
for (int j = 0; j < strN.length(); j++)
{
if (strN[j] == numbers[idx])
{
idx++;
if (idx == numbers.length())
break;
}
}
}
cout << n;
}
n의 인덱스 0번(가장 높은 자릿수)부터 탐색하며 입력의 인덱스 위치와 비교하여 존재하면 idx를 증가시켜가며 탐색하였다
처음에는 지워지지 않은 연속된 수를 탐색하지 못하게 코드를 작성했다가 이를 고려할 수 있게 구현하였다