首页 > 试题广场 >

数字统计

[编程题]数字统计
  • 热度指数:8432 时间限制: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。
#include<stdio.h>
int Find_2(int num) {
    int count = 0;
    while (num) {
        if (2 == num % 10)
            count++;
        num /= 10;
    }
    return count;
}
int main() {
    int L, R, ret = 0;
    scanf("%d %d", &L, &R);
    for (int i = L; i <= R; i++)
        ret += Find_2(i);
    printf("%d\n", ret);
    return 0;
}

发表于 2022-08-11 09:54:44 回复(0)
#include <stdio.h>
int judge(int num){
    int flag = 0;
    while(num){
        int res = num % 10;
        if(res == 2)
            flag ++;
        num /= 10;
    }
    return flag;
}

int main(){
    int l, r, count = 0;
    scanf("%d %d", &l, &r);
    for(int i = l; i <= r; i++)
        count += judge(i);
    printf("%d", count);
    return 0;
}

发表于 2022-06-15 19:09:54 回复(0)
#include<iostream>
using namespace std;

int main() {
    int l, r, num, cnt = 0;
    cin >> l >> r;
    for (int i = l; i <= r; ++i) {
        num = i;
        while (num) {
            if (num % 10 == 2) ++cnt;
            num /= 10;
        }
    }
    cout << cnt;
    return 0;
}

发表于 2022-03-31 21:27:51 回复(0)
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int timesof2(int x)
{
    int res = 0;
    while (x)
    {
        int e = x % 10;
        if(e == 2) res ++;
        x /= 10;
    }
    return res;
}

int main()
{
    int l, r;
    cin >> l >> r;
    int cnt = 0;
    for(int i = l; i <= r; i ++ )
        cnt += timesof2(i);
    cout << cnt << endl;
    
}

发表于 2022-02-26 16:25:05 回复(0)
这里有两者方式:
方式一:遍历,找到含有字符2的数,然后用内置的count()函数统计
方式二:遍历,把所有的数字转化为字符后加入为一个整体,最后对整体的数据进行判断
方式一:
def Asum(L,R):
    count = 0
    s = "2"
    for i in range(L,R+1):
        #找出含有字符“2”的数
        if s in str(i):
            #用内置的count()函数,统计含有2的字符中2的个数
            count +=str(i).count('2')
    print(count)
L,R= map(int,input().split())
Asum(L,R)

方式二:
l, r = map(int,input().split())
#先定义一个空白字符串
lt = ''
#将l~r的所有字符串全部加入到lt中去
for i in range(l,r+1):
    lt+=str(i)
#最后统计整合到一起的字符串中2的个数
a = lt.count('2')
print (a)   

发表于 2022-04-06 09:49:22 回复(0)
#include <stdio.h>

int main() {
    int L, R, count = 0;
    scanf("%d %d", &L, &R);
    for (int i = L; i <= R; i++) {
        int temp = i;
        while (temp) {
            if (temp % 10 == 2) {
                count++;
            }
            temp /= 10;
        }
    }
    printf("%d", count);
    return 0;
}

编辑于 2024-02-09 21:30:31 回复(0)
// 拼成一串s,再计数
System.out.print(s.length() - s.replaceAll("2", "").length());

发表于 2025-01-26 12:22:47 回复(0)
#include <stdio.h>
int count2(int l, int r) {
    int i = 0, j = 0, cnt = 0;
    for (i = l; i <= r; i++) {
        j = i;
        while (j) {
            if (j % 10 == 2) {
                cnt++;
            }
            j /= 10;
        }
    }
    return cnt;
}

int main() {
    int l = 0, r = 0;
    scanf("%d %d", &l, &r);
    printf("%d\n", count2(l, r));
    return 0;
}
发表于 2025-01-16 17:16:57 回复(0)
#include<stdio.h>

int Count(int a, int b)
{
   int i = 0;
   int sum = 0;
   for(i=a;i<=b;i++)
   {
      int z = i;
      int g = 0;
      while(z)
      {
         g = z %10;
        
         if(g==2)
         {
            sum++;
         }
         z = z /10;
      }
   }
   return sum;

}
int main()
{
    int a = 0;
    int b = 0;
    scanf("%d %d",&a,&b);
    int ret= Count(a,b);
    printf("%d",ret);
    return 0;
}

发表于 2024-12-24 11:10:47 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int a, b, can = 0, n = 0, ci = 0;
    scanf("%d %d", &a, &b);

    //将检查循环重复在指定范围内次
    for (int x = a; x <=b; x++) {
        n = x;

        //检查每个数位是否等于2 是则次数变量加一  当数为零则跳出循环
        for (int y = 0; y < 10; y++) {
            if (n == 0) {
                break;
            }
            can = n % 10;
            if (can == 2) {
                ci++;
            }
            n /= 10;
        }

    }
    
    printf("%d", ci);
    return 0;
}

发表于 2024-10-28 21:19:56 回复(0)
#include <stdio.h>
int function(int x,int y);
int function(int x,int y)
{
  int c,i,num,n;
  int static count=0;
  for(num=x;num<=y;num++)
  {
    n=num;
    while(n>0)
    {
        i=n%10;
        if(i==2)
        {
            count++;
        }
        n/=10;
    }
  }
  return count;
}
int main() {
    int L,R;
    scanf("%d %d",&L,&R);
    int recive=function(L,R);
    printf("%d",recive);
    return 0;
}

发表于 2024-09-29 15:48:12 回复(0)
use std::io::{self, *};

fn main() {
    let stdin = io::stdin();
    unsafe {
        for line in stdin.lock().lines() {
            let ll = line.unwrap();
            let numbers: Vec<&str> = ll.split(" ").collect();
            let a = numbers[0].trim().parse::<i32>().unwrap_or(0);
            let b = numbers[1].trim().parse::<i32>().unwrap_or(0);
            let c = count(a,b);
            print!("{}", c);
        }
    }
}
fn count(l: i32, r: i32) -> i32 {
    let mut c = 0;
    for num in l..=r {
        let s = num.to_string();
        for char in s.chars() {
            if char == '2' {
                c += 1;
            }
        }
    }
    c
}


发表于 2024-09-06 23:27:58 回复(0)
#include <stdio.h>

int count_num(int a, int b){
    int count = 0;
    int tmp;
    for (int i = a; i <= b; i++) {
        tmp = i; //防止i变化,用临时变量进行后续操作
        while (tmp > 0){
            if (tmp == 2){
                count++;
                break;
            }
            if (tmp != 2){
                if (tmp % 10 == 2){
                    count++;
                    tmp = tmp / 10;
                }
                else{
                    tmp = tmp / 10;
                }
            }
        }
    }
    return count;
}

int main() {
    int a, b;
    scanf("%d %d",&a,&b);
    int ret = count_num(a,b);
    printf("%d",ret);
    return 0;
}
//
// Created by Wilbur Lee on 11/7/2024.
//

发表于 2024-07-11 19:12:18 回复(0)
#include <stdio.h>

//函数名的意思是2出现的次数
int Number2OfOccurrences(int L, int R)
{
    int count = 0;  //记录二出现的次数
    for(int i = L; i <=R; i++)
    {
        int tmp = i;
        while (tmp) 
        {
            //判断余数为2,计数加一
            if(tmp % 10 == 2)
                count++;
            
            tmp /= 10;
        }
    }

    return count;
}

int main() {
    int L, R;
    scanf("%d %d", &L, &R);

    int sum = Number2OfOccurrences(L, R);
    printf("%d", sum);
}

发表于 2024-06-23 19:14:20 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    int count = 0;
    for (int i = a; i <= b; i++) 
    {
        if (i % 10 == 2) 
            count++;
        if (i / 10 % 10 == 2) 
            count++;
        if (i / 100 % 10 == 2) 
            count++;
        if (i /1000 % 10 == 2) 
            count++;    
    }
    cout << count << endl;
}

发表于 2024-05-11 09:44:47 回复(0)
#include<stdio.h>

int sums(int n)
{
    int x=0;
    while(n!=0)
    {
        if(n%10==2)
            x++;
        n/=10;
    }
    return x;
}

int main()
{
    int L,R;
    int sum=0;
    scanf("%d %d",&L,&R);
    for(int i=L;i<=R;i++)
    {
        sum=sum+sums(i);
    }
    printf("%d",sum);
    return 0;
}

编辑于 2024-04-09 18:22:18 回复(0)
#include <stdio.h>

int FindTwo(int x)
{
    int ret = 0;

    while(x > 0)
    {
        if(x % 10 == 2)
        {
            ret++;
        }
        x /= 10;
    }
    return ret;
}

int main() 
{
    int a = 0;
    int b = 0;
    int i = 0; 
    int count = 0;

    scanf("%d%d", &a, &b);

    for(i = a; i <= b; i++)
    {
        count += FindTwo(i);
    }

    printf("%d\n", count);
    
    return 0;
}

发表于 2024-03-28 19:22:38 回复(0)
#include <stdio.h>

int F(int i) {
    long sum = 0;
    while (i) {
        if(i % 10 == 2){
            sum++;
        }
        i /= 10;
    }
    return sum;
}

int main() {
    int L, R;
    scanf("%d %d", &L, &R);
    int sum=0;
    for(int i=L; i<=R; i++){
        sum += F(i);
    }
    printf("%d", sum);
    return 0;
}


编辑于 2024-02-21 21:05:01 回复(0)
全部数字连成一个字符串,统计“2”的个数
m,n = map(int,input().split())
s="".join(str(x) for x in range(m,n+1))
print(s.count("2"))


发表于 2024-02-12 21:05:05 回复(0)
  • 求每位数
    func main() {
      var l,r,cnt int
      fmt.Scan(&l,&r)
      for i:=l;i<=r;i++{
          x := i
          for x > 0 {
              if x % 10 == 2 {
                  cnt ++ 
              }
              x /= 10
          }
      }
      fmt.Println(cnt)
    }
发表于 2023-12-22 21:51:16 回复(0)