목차

     

     


     

     

     

    [백준 2331번 C/C++] 반복수열

     

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

     

    2331번: 반복수열

    첫째 줄에 반복되는 부분을 제외했을 때, 수열에 남게 되는 수들의 개수를 출력한다.

    www.acmicpc.net


     

     

    해결전략

     

    구현

     

     


     

    코드

     

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int a, p, nextNum, i=0, cnt=0;
    int box[10000];
    
    int main() {
    	ios_base::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    	cin >> a >> p;
    	box[0] = a;
    	
    
    	while(true)
    	{
    		cnt++;
    		nextNum = 0;
    
    		while (a > 0)
    		{
    			nextNum += pow(a % 10, p);
    
    			a /= 10;
    		}
    		box[++i] = nextNum;
    		a = nextNum;
    
    		for(int k=0; k<cnt; k++)
    		{
    			if (nextNum == box[k])
    			{
    				cout << k;
    
    				return 0;
    			}			
    		}//for
    	}
    
    	return 0;
    }