-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddDigits0726.java
More file actions
38 lines (31 loc) · 818 Bytes
/
Copy pathAddDigits0726.java
File metadata and controls
38 lines (31 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class AddDigits0726{
public static void main(String[] args){
int a = 64;
System.out.println(final_sum(a));
}
private static int digit_sum(int input){
// 去除所有末位0
while (input%10==0){
input /= 10;
}
int sum = 0;
while (input!=0){
sum += input%10;
input /= 10;
}
return sum;
}
private static int final_sum(int input){
int current_sum = input;
while (current_sum >=10){
current_sum = digit_sum(current_sum);
}
return current_sum;
}
}
// 666的写法 https://leetcode.com/articles/add-digits/
// class Solution {
// public int addDigits(int num) {
// return num == 0 ? 0 : 1 + (num - 1) % 9;
// }
// }