程序代码:
public class OneCounter {
public static int counter(int n) {
int count = 0;
StringBuilder buffer = new StringBuilder();
for (int i = 0; i <= n; ++i)
buffer.append(i);
for (int i = 0; i < buffer.length(); ++i)
if (buffer.charAt(i) == '1')
++count;
return count;
}
public static void main(String[] args) {
System.out.println(counter(13));
System.out.println(counter(133));
System.out.println(counter(90));
}
}
public static int counter(int n) {
int count = 0;
StringBuilder buffer = new StringBuilder();
for (int i = 0; i <= n; ++i)
buffer.append(i);
for (int i = 0; i < buffer.length(); ++i)
if (buffer.charAt(i) == '1')
++count;
return count;
}
public static void main(String[] args) {
System.out.println(counter(13));
System.out.println(counter(133));
System.out.println(counter(90));
}
}
----------------解决方案--------------------------------------------------------
回复 2楼 lz1091914999
提示: 作者被禁止或删除 内容自动屏蔽