首页 > 试题广场 >

[NOIP2010]数字统计

[编程题][NOIP2010]数字统计
  • 热度指数:5550 时间限制: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)
#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)
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)
#include <stdio.h>
int count(int l,int r)
{
    int i,sum = 0;
    for(i=l;i<=r;i++)
    {
        int k = i;
        while(k)
        {
            int t = k%10;
            k /= 10;
            if(t==2)
            {
                sum++;
            }
        }
    }
    return sum;
}
int main()
{
    int l,r;
    scanf("%d %d",&l,&r);
    int ret = count(l,r);
    printf("%d",ret);
    return 0;
}

发表于 2023-11-17 10:26:15 回复(0)
#include <stdio.h>
int main() {
   int a,b;
   int i = 0;
   int count = 0;
   int sum = 0;
   while(scanf("%d %d",&a,&b) != EOF)
   {
    for(i = a;i<=b;i++)
    {
        int num = i;
        while(num)
        {
            int a = num%10;
            num = num/10;
            if(a == 2)
            {
                count++;
            }
        }
    }
    printf("%d",count);
   }
    return 0;
}
发表于 2023-06-05 14:26:26 回复(0)
#include <stdio.h>
int pa(int n,int m)
{
    int count=0;
    int i,j,k=0,p=0;
    for(i=n;i<=m;i++)
    {
        j=i;
        while(j)
        {
         if(j%10==2)
         count++;
         j/=10;
        }
    }
    return count;
}
int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    printf("%d",pa(n,m));
    return 0;
}

发表于 2023-04-21 18:46:30 回复(0)
#include <stdio.h>
int a(int L,int R)
{
    int i,j,count=0;
    for(i=L;i<=R;i++)
    {
        int val=i;
        while(val)
        {
            if(val%10==2)
            count++;
            val/=10;

        }
    }
    return count;
}
int main() 
{
    int L,R;
    scanf("%d %d",&L,&R);
    int h=a(L,R);
    printf("%d",h);
    return 0;
}

发表于 2023-03-13 21:00:24 回复(0)
#include <stdio.h>
int checkTwo(int l, int r) {
    int tmp = 0, cnt = 0;
    for (int i = l; i <= r; i++) {
        tmp = i;
        while (tmp) {
            // Check LSB(Least Significant Bit)
            if (tmp % 10 == 2) {
                cnt++;
            }
            // Check the next digit of tmp
            tmp /= 10;
        }
    }
    return cnt;
}

int main() {
    int l, r;
    while (scanf("%d %d", &l, &r) != EOF) {
        printf("%d\n", checkTwo(l, r));
    }
    return 0;
}
发表于 2023-03-01 20:12:03 回复(0)
public class Program {
    public static void Main() {
        string[] inPut = System.Console.ReadLine().Split(" ");
        var(start, end) = (int.Parse(inPut[0]), int.Parse(inPut[1]));

        int count = 0;
        for (int i = start; i <= end; i++) {
            char[] array = i.ToString().ToCharArray();
            for (int j = 0; j < array.Length; j++)
                if (array[j] == '2')
                    count += 1;
        }
        System.Console.WriteLine(count);
    }
}

发表于 2022-12-13 11:51:30 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        let arr = line.split(" ").map(Number);
        let l = arr[0];
        let r = arr[1];
        function main(a, b) {
            let count = 0;
            for (let i = a; i <= b; i++) {
                let x = i;
                while (x != 0) {
                    let g = x % 10;
                    x = parseInt(x/10)
                    if(g==2){
                        count++
                    }
                }
            }
            return count
        }
        console.log(main(l,r))
    }
})();

发表于 2022-11-08 17:02:01 回复(0)