BC153 [NOIP2010]数字统计
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int left = in.nextInt();
int right = in.nextInt();
int n = towNum(left,right);
System.out.println(n);
}
public static int towNum(int x, int y) {
StringBuilder sb = new StringBuilder();
for(int i = x; i <= y;i++) {
sb.append(i);
}
String s = sb.toString();
int count = 0;
for(int i = 0;i< s.length();i++) {
if(s.charAt(i)=='2') count++;
}
return count;
}
}
String,StringBuffer,StringBuilder的区别:
解法2:
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int left = in.nextInt();
int right = in.nextInt();
int ret = 0;
for(int i = left; i <= right;i++) {
int tmp = i;
while(tmp != 0) {
if(tmp%10 ==2) ret++;
tmp /= 10;
}
}
System.out.println(ret);
}
}
原文地址:https://blog.csdn.net/m0_74312445/article/details/143509199
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!