You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
471 B
30 lines
471 B
2 years ago
|
#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;
|
||
|
}
|