首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

到1亿的自然数,求全部数的拆分后的数字之和

2012-09-02 
到1亿的自然数,求所有数的拆分后的数字之和public static void main(String[] args) throws UnsupportedEn

到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);

}

热点排行