public static void main(String[] args) throws UnsupportedEncodingException {
//1到1亿的自然数,求所有数的拆分后的数字之和,如286 拆分成2、8、6,如1到11拆分后的数字之和 => 1 + ... + 9 + 1 + 0 + 1 + 1
int i = 5659553 ;
String str = Integer.toString(i) ;
char c = '0' ;
int result = 0 ;
for (int j = 0; j < str.length(); j++) {
result = result + (str.charAt(j)-c) ;
}
System.out.println(result);
}
//1到1亿的自然数,求所有数的拆分后的数字之和,如286 拆分成2、8、6,如1到11拆分后的数字之和 => 1 + ... + 9 + 1 + 0 + 1 + 1
int i = 5659553 ;
String str = Integer.toString(i) ;
char c = '0' ;
int result = 0 ;
for (int j = 0; j < str.length(); j++) {
result = result + (str.charAt(j)-c) ;
}
System.out.println(result);
}
本文展示了一个简单的Java程序,该程序将一个整数的每一位数字进行拆分并计算总和。例如,输入5659553,程序会将其拆分为5、6、5、9、5、5、3,并计算这些数字的总和。
949

被折叠的 条评论
为什么被折叠?



