Java每日一题02

1到11有4个“1”,1中一个“1”,10中一个“1”,11中两个“1”,1000中,有多少个“1”,欢迎给出你的解法!

package test07;

public class CountOne {
	public static void main(String[] args) {
		int count = 0;
		for (int i = 1; i <= 1000; i++) {
			String s = String.valueOf(i);
			for (int j = 0; j < s.length(); j++) {
				if (s.charAt(j) == '1')
					count++;
			}
		}
		System.out.println(count);
	}
}


你可能感兴趣的:(java,算法,J#,sun)