首页 > 试题广场 >

数字统计

[编程题]数字统计
  • 热度指数:9468 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解

请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。

比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,在数22中出现2次,所以数字2在该范围内一共出现了6次。


输入描述:
输入共1行,为两个正整数L和R,之间用一个空格隔开。


输出描述:
输出共1行,表示数字2出现的次数。
示例1

输入

2 22

输出

6
示例2

输入

2 100

输出

20

备注:
1≤L≤R≤10000。
// 拼成一串s,再计数
System.out.print(s.length() - s.replaceAll("2", "").length());

发表于 2025-01-26 12:22:47 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int num = 0;
            for (int i = a; a <= b; a++) {
                String a1 = String.valueOf(a);
                if (a1.length() <= 1) {
                    if (a1.indexOf('2') > -1) {
                        num++;
                    }
                } else {
                    for (int j = 0; j < a1.length(); j++) {
                        if (a1.substring(j, j + 1).indexOf('2') > -1) {
                            num++;
                        }
                    }
                }
            }
            System.out.println(num);
        }
    }
}
发表于 2023-11-29 21:26:22 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int l=sc.nextInt(),r=sc.nextInt();
        int count=0;
        for(int i=l;i<=r;i++){
            count+=sum(i);
        }
        System.out.println(count);
    }
    public static int sum(int x){
        int count=0;
        while(x!=0){
            if(x%10==2) count++;
            x/=10;
        }
        return count;
    }
}

发表于 2022-08-09 20:58:44 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int L = scanner.nextInt();
        int R = scanner.nextInt();
        int count = 0;
        for(int i = L;i <= R;i++){
            count = count + toCountNumTwoTime(i);
        }
        System.out.println(count);
    }
    public static int toCountNumTwoTime(int num){
        int countTwo = 0;
        int a;
        while(num / 10 != 0 || num % 10 != 0){
            a = num % 10;
            num = num / 10;
            if(a == 2){
                countTwo++;
                }
            }
            return countTwo;
        }
}

发表于 2022-07-05 15:15:34 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int count = 0;
        for(int i = a;i<=b;i++){
            count = count + m5(i);  //范围内2总共出现的次数
        }
        System.out.printf("%d",count);
    }
    public static int m5(int a){     //修改之前的函数,改为判断某个数中2出现的次数
        int sum = 0;
        int x;
        while(a!=0){
                x = a%10;
                a = a/10;
                if(x == 2){
                    sum++;  //数字二出现的次数
                }
        }
        return sum;  
    }
}

发表于 2022-03-15 14:54:31 回复(0)

问题信息

难度:
6条回答 4404浏览

热门推荐

通过挑战的用户

查看代码
数字统计