# UVA 10922 2 the 9s ## 題目連結 [UVA 10922](https://vjudge.net/problem/UVA-10922) ### 題目內容 A well-known trick to know if an integer N is a multiple of nine is to compute the sum S of its digits. If S is a multiple of nine, then so is N. This is a recursive test, and the depth of the recursion needed to obtain the answer on N is called the 9-degree of N. Your job is, given a positive number N, determine if it is a multiple of nine and, if it is, its 9-degree. ### 輸入限制 The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits. ### 輸出限制 The output of the program shall indicate, for each input number, if it is a multiple of nine, and in case it is, the value of its nine-degree. See the sample output for an example of the expected formatting of the output. ### 解題思路 這題要求每位數相加是否為9的倍數,9-degree是要求總共有多少9的倍數 ### 程式碼 ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ string n; while(cin>>n && n!="0"){ int cot=0; string s=n; while(true){ int ans=0; for(int i=0;i<n.length();i++){ ans+=n[i]-'0'; } if(ans%9==0){ cot++; } n=to_string(ans); if(n.length()==1){ break; } } if(cot==0){ cout<<s<<" is not a multiple of 9."<<endl; } else{ cout<<s<<" is a multiple of 9 and has 9-degree "<<cot<<"."<<endl; } } } ``` ## 測資 ### Sample input 999999999999999999999 9 9999999999999999999999999999998 0 ### Sample output 999999999999999999999 is a multiple of 9 and has 9-degree 3. 9 is a multiple of 9 and has 9-degree 1. 9999999999999999999999999999998 is not a multiple of 9. ## 中文題目連結 [zerojudge d672](https://zerojudge.tw/ShowProblem?problemid=d672)