有多少个“1”呢

问题:请问下面这个长达1000位的整数中有多少个“1”呢?
59238732432570234870329840932743075248324832904848
83926752083429174392705780324093811432843025072848
28941114732986589237432149328956827432987492124738
47320084329840321048093274320743624732074032490328
48329408329048950923782184032743982638924924083928
75208342917439270578032409381143284302507284328946
11473298658923743214932895682743298749212473247328
08432984032104809327432075092378218403274398649326
48327497239847839274983269426389249240839267520838
29174392705780324093811432843025072843289411147328
86589237432149328956827432987492124732473200843296
40321048093274320750923782184032743982638924924088
92675208342917439270578032409381143284302507284326
94111473298658923743214932895682743298749212473248
32008432984032104809327432075092378218403274398266
89249240839267520834291743927057803240938114328438
25072843289411147329865892374321493289568274329878
92124732473200843298403210480932743207509237821846
32743982638924924398274892374892374832437294798328
82394893225923873243257023487032984093274307524838

分析:我们第一步是要把它的换行符号给干掉,然后应该用一个for循环遍历,字符串中的每一位与字符串“1”相比较,成功一次计数加一,其实这道题很简单,多说无益,直接上代码。
public class Test {
public static void main(String[] args) {
String str = "5923873243257023487032984093274307524832483290484883926752083429174392705780324093811432843025072848289411147329865892374321493289568274329874921247384732008432984032104809327432074362473207403249032848329408329048950923782184032743982638924924083928752083429174392705780324093811432843025072843289461147329865892374321493289568274329874921247324732808432984032104809327432075092378218403274398649326483274972398478392749832694263892492408392675208382917439270578032409381143284302507284328941114732886589237432149328956827432987492124732473200843296403210480932743207509237821840327439826389249240889267520834291743927057803240938114328430250728432694111473298658923743214932895682743298749212473248320084329840321048093274320750923782184032743982668924924083926752083429174392705780324093811432843825072843289411147329865892374321493289568274329878921247324732008432984032104809327432075092378218463274398263892492439827489237489237483243729479832882394893225923873243257023487032984093274307524838";
int t=0;

	for(int i=0; i<str.length(); i++){
		if("1".equals(String.valueOf(str.charAt(i)))){
			System.out.println(++t);
		}
	}
	
}

}

原文地址:https://www.cnblogs.com/Oraice/p/5168425.html