一、数字统计
1.1 数字统计解法一
public class Test {
public static void main(String[] args) {
int count = 0;
Scanner scanner = new Scanner(System.in);
int ret1 = scanner.nextInt();
int ret2 = scanner.nextInt();
for (int i = ret1; i <= ret2 ; i++) {
if(i % 10 == 2){
count++;
}
if(i / 10 == 2){
count++;
}
}
System.out.println(count);
}
}
结果如下:
1.2 数字统计解法二
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int l = scanner.nextInt(),r = scanner.nextInt();
int ret = 0;
for (int i = l; i <= r; i++) {
int tmp = i;
while (tmp != 0){
if(tmp % 10 == 2){
ret++;
}
tmp = tmp / 10;
}
}
System.out.println(ret);
}
}
结果如下:# 三、点击消除
在这里插入代码片