본문 바로가기
알고리즘/백준 문제

1515 - 수 이어 쓰기

by HDobby 2023. 3. 2.

https://www.acmicpc.net/problem/1515

 

1515번: 수 이어 쓰기

세준이는 1부터 N까지 모든 수를 차례대로 공백없이 한 줄에 다 썼다. 그리고 나서, 세준이가 저녁을 먹으러 나간 사이에 다솜이는 세준이가 쓴 수에서 마음에 드는 몇 개의 숫자를 지웠다. 세준

www.acmicpc.net

생각해 보기

  • to_string을 사용하자
  • 현재 숫자의 가장 높은 자리부터 전부 비교하여 idx를 한 칸씩 이동하면 된다.

코드

더보기
#include<iostream>

using namespace std;

string input;

void solution(){
    int cnt=1,idx=0,len=input.length();
    while(idx<len){
        string tmp=to_string(cnt++);

        for(int i=0, slen=tmp.length();i<slen&&idx<len;++i){
            if(tmp[i]==input[idx])
                ++idx;
        }
    }
    cout<<cnt-1<<'\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>input;
    solution();

    return 0;
}

728x90

'알고리즘 > 백준 문제' 카테고리의 다른 글

17484 - 진우의 달 여행(Small)  (0) 2023.03.03
19441 - 햄버거 분배  (0) 2023.03.03
21921 - 블로그  (0) 2023.03.02
20920 - 영단어 암기는 괴로워  (0) 2023.02.28
1244 - 스위치 켜고 끄기  (0) 2023.02.28

댓글