알고리즘

[알고리즘] 백준 6378 - 디지털 루트 (C++)

blueberrysoda 2025. 1. 1. 23:31

[문제 링크] : https://www.acmicpc.net/problem/6378

#include <iostream>
using namespace std;

int solve(string n){
    int res = 0;
    for(int i=0; i<n.size(); i++){
        res += n[i] - '0';
    }

    if(res < 10){
        return res;
    }
    else{
        return solve(to_string(res));
    }
}

int main(){
    string inp;
    while(true){
        cin >> inp;
        if(inp == "0"){
            break;
        }

        cout << solve(inp) << "\n";
    }
    return 0;
}