30 lines
471 B
C++
30 lines
471 B
C++
|
|
#include <bits/stdc++.h>
|
||
|
|
|
||
|
|
using namespace std;
|
||
|
|
// 数根
|
||
|
|
// 暴力法
|
||
|
|
void func1(int n) {
|
||
|
|
while (true) {
|
||
|
|
int sum = 0;
|
||
|
|
while (n) {
|
||
|
|
sum += n % 10;
|
||
|
|
n /= 10;
|
||
|
|
}
|
||
|
|
n = sum;
|
||
|
|
if (n < 10) break;
|
||
|
|
}
|
||
|
|
cout << n << endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 公式法
|
||
|
|
void func2(int n) {
|
||
|
|
cout << (n % 9 ? n % 9 : 9) << endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
int n;
|
||
|
|
cin >> n;
|
||
|
|
func1(n);
|
||
|
|
func2(n);
|
||
|
|
return 0;
|
||
|
|
}
|