`
ansjsun
  • 浏览: 199393 次
  • 性别: Icon_minigender_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);

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics