본문 바로가기

problem solving

2331번: 반복수열

(수열이 반복되는 지점까지의 이동횟수 - 처음으로 반복되는 지점부터 왕복하는데 이동횟수)를 계산하면 

답을 구할 수 있다.

#include <iostream>
#include <cstring>
 
int check[1000000= { 0 };
 
int square(int n, int degree) {
    int ans = n;
    for (int i = 0; i < degree - 1; i++) {
        ans *= n;
    }
    return ans;
}
 
int seq(int *val, int degree) {
    int count = 1;
    check[*val]++;
    while (true) {
        int temp = 0;
        while (true) {
            temp += square(*val % 10, degree);
            if (*val < 10) { break; }
            *val /= 10;
        }
        check[temp]++;
        //std::cout << temp<< ", " << "\n";
        *val = temp;
        if (check[temp] > 1) {
            break;
        }
        count++;
    }
    return count;
}
 
int main(void) {
    int val;
    std::cin >> val;
    int degree;
    std::cin >> degree;
 
    int totalCount = seq(&val, degree);
    memset(check, 0sizeof(check));
    int excount = seq(&val, degree);
    
 
    int count = totalCount - excount;
    std::cout << count << "\n";
    return 0;
}
 
 
cs

'problem solving' 카테고리의 다른 글

[미해결]2632번: 피자판매  (0) 2020.03.01
2579번: 계단 오르기  (0) 2020.03.01
2089번: -2진수  (0) 2020.02.27
2004번: 조합 0의 개수  (0) 2020.02.25
1517번: 버블 소트  (0) 2020.02.24