C, C++ 문제풀이

SW Expert Academy 1288. 새로운 불면증 치료법

륵기 2020. 4. 5. 18:02
728x90
반응형

문제 링크 : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV18_yw6I9MCFAZN&categoryId=AV18_yw6I9MCFAZN&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
     
     
    int T;
    cin >> T;
    for (int i = 0; i < T; i++)
    {
        int N;// 양 번호
        int arr[11] = { 0, }; // 0~9 까지의 숫자 인덱스 배열, 숫자 등장 시 값 1씩 증가
        int cnt = 0; // count 변수
        int res; // xN 양 번호 변수
        int temp; //N담기 위한 변수
        int t2;
        cin >> N;
        for (int j = 0;cnt<10; j++)
        {   
            temp = N*(j);
            res = temp;
            while (temp>0)
            // 1295가 들어왔을 때, 각 자리수를 받아내기 위해 10으로 계속 mod 계산
            {
                t2 = temp % 10;
                if (arr[t2] == 0) {
                    cnt++;
                    arr[t2] ++;
                      
                    }
                    //1295 % 10 = 5
                    //1295 / 10 = 129....
                temp /= 10;
  
            }   
        }
        cout << '#' << i + 1 << ' ' << res << '\n';

    }

    return 0;
}

처음에 for문으로만 다 짜다가, 일일히 조건을 넣어서 하려니 코드도 길어지고 번잡하게 구현이 됬었다.

 

728x90
반응형