到1亿的自然数,求全部数的拆分后的数字之和
到1亿的自然数,求所有数的拆分后的数字之和
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);
}
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);
}