首页 > 试题广场 >

记数问题

[编程题]记数问题
  • 热度指数:33336 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}试计算在区间 1n\left(1 \leqq n \leqq 10^6\right) 的所有整数中,数字 x0 \leqq x \leqq 9)共出现了多少次。

输入描述:
\hspace{15pt}在一行中输入两个整数 n,x,用空格隔开。其中 n 表示区间上界,x 表示要统计的数字。


输出描述:
\hspace{15pt}输出一个整数,表示数字 x 在区间 [1,n] 中出现的次数。
示例1

输入

11 1

输出

4

说明

在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 次。
示例2

输入

20 1

输出

12

说明

在区间 120 中,数字 1 出现在 1,10,11,12,13,14,15,16,17,18,19 中,共 12 次。

备注:

哈希表
int main() {
    int hash[10] = {0}; //定义哈希表,范围0~9
    int n, x;
    scanf("%d %d", &n, &x);
    for (int i = 1; i <= n; i++) { //遍历区间
        int tem = i;
        while (tem) {      //计算
            hash[tem % 10]++;
            tem /= 10;
        }
    }
    printf("%d", hash[x]); //输出

}

发表于 2022-06-29 21:02:34 回复(1)
m,n=map(int,input().split())
cc=''
for i in range(1,m+1):
    i=str(i)
    cc+=i
con=cc.count(str(n))
print(con)
发表于 2022-03-01 17:56:58 回复(0)
使用join()函数将列表中的所有元素合并为一个字符串
n,x=map(int,input().split())
a = [str(i) for i in range(1,n+1)]
b = "".join(a)
print(b.count(str(x)))


编辑于 2024-02-06 14:07:39 回复(0)
n, x = map(int, input().split())
c = 0
for i in range(n):
    c += str(i + 1).count(str(x))
print(c)

发表于 2025-11-04 11:34:02 回复(0)
n,x = map(int, input().split())
times = 0
for i in range(1, n + 1, 1):
    while i > 0:
        if i % 10 == x:
            times += 1
        i //= 10
print(times)

发表于 2025-07-25 02:32:30 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        int n=0;
        int x=0;
        int cnt=0;
        Scanner in=new Scanner(System.in);
        n=in.nextInt();
        x=in.nextInt();
        for(int i=1;i<=n;i++)
        {
            int temp=i;
            while(temp!=0)
            {
                if(temp%10==x) cnt++;
                temp/=10;
            }
        }
        System.out.println(cnt);
    }
}

发表于 2022-07-20 17:47:57 回复(0)
#include <iostream>
using namespace std;
#include <string>

int main() {
    int n,x;
    cin >> n >> x;
    string s;
    int count = 0;
    for(int i = 1; i <= n; ++i){
        s += to_string(i);
    }
    for(const auto str : s){
        if(str - '0' == x) count++;
    }
    cout << count << endl;
}

发表于 2025-12-13 21:22:27 回复(0)
#include <stdio.h>

int main()
{
    long n=0;
    scanf("%ld",&n);
    int x=0;
    scanf("%d",&x);
    int num=0;
    for(long i=1;i<=n;i++)
    {
        for(long i1=i;i1!=0;i1=i1/10)
        {
            if(i1%10==x)
            {
                num++;
            }
        }
    }
    printf("%d",num);
    return 0;
}
这道题还是相对来说比较简单的。上面的是楼主的思路,希望能给有需要的人一点启发
发表于 2025-11-24 09:57:00 回复(0)
#include <stdio.h>

int main() {
    int n,x,number,k=0;
    scanf("%d %d",&n,&x);
    for(; n > 0 ; n--){
        number = n;
            while(number>0){
            if((number%10) == x)k++;
            number /= 10;
        }
    }
    printf("%d",k);

}
发表于 2025-11-11 20:57:20 回复(0)
n, x = map(int, input().split())

x = str(x)
count = 0

for i in range (1, n+1):
    count += str(i).count(x)

print(count)
发表于 2025-07-15 06:13:26 回复(0)
#include <stdio.h>

int main() {
    int n,x;
    int sum = 0;
    scanf("%d %d", &n, &x);
    for (int i = 1; i <= n ;i++ ) {
        int tmp = i;
        while (tmp) {
            if(tmp % 10 == x)
                sum++;
            
            tmp /= 10;
        }
    }
    printf("%d", sum);
    return 0;
}


发表于 2024-06-21 02:44:58 回复(0)
#include <stdio.h>

int main()
{
    int n, x;
    int count = 0;
    scanf("%d %d", &n, &x);

    for (int i = 1; i <= n; i++)//每一次循环判断一个数,直到n为止
    {
        int a = i;
        while (a)//利用while循环判断每一位数字,直到a等于0,
        {
            if (a % 10 == x)//如果个位上的数字是x,count加一
                count++;
            a /= 10;//将个位上的数字去除,十位上的数字变成个位上的数字
        }
    }

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

编辑于 2024-02-28 09:26:10 回复(0)
这个效率高
import re

n, x = map(int, input().split())
myList = [i for i in range(1, n + 1)]
print(len(re.findall(str(x), "".join(str(i) for i in myList))))

发表于 2022-11-04 09:12:44 回复(0)
import java.util.Scanner;

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

发表于 2022-08-09 21:29:42 回复(0)
#include<stdio.h>
int main()
{
    int n , x , mid_var0 , mid_var1 , mid_var2 , sum = 0 ;
    scanf("%d %d",&n,&x);
    for(int i = 1 ; i <= n ; i++)
    {
        mid_var0 = i ;
        while(mid_var0 != 0)
        {
            mid_var1 = mid_var0 % 10 ;
            if(mid_var1 == x) sum++;
            mid_var0 /= 10 ;
        }
    }
    printf("%d",sum);
    return 0;
}

发表于 2022-06-23 20:11:33 回复(0)
#include <stdio.h>

void Judge(int i, int b, int* count){
    while(i > 0){
        if(i % 10 == b)
            *count += 1;
        i /= 10;
    }
 }   
int main(){
    int a, b;
    int count = 0;
    scanf("%d %d", &a, &b);
    for(int i = 1; i <= a; i++){
        Judge(i, b, &count);
    }
    printf("%d", count);
    return 0;

}

发表于 2022-05-27 17:11:10 回复(0)
凭我朴素的直觉,写了一个非常低效的答案,当反面例子看吧:
n,x = map(int,input().split())
t = 0
for i in range(1,n+1):
    for j in range(len(str(i))):
        if str(x) in str(i)[j]:
# 如果x在i的第j个字符出现,那t计数一次
            t += 1
        else:
            t = t
print (t)


#  会报错
# 运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。

参考了讨论区“我有一只猫”大佬的方法:
m,n=map(int,input().split())
cc=''
for i in range(1,m+1):
    i=str(i)
    cc+=i
    # 意思是把所有数字的文本先加在一起,组成一个名为cc的长文本,
con=cc.count(str(n))
# 再在cc之中用count,数n这个数字出现的次数
# 妙哇!
print(con)


发表于 2022-03-04 19:51:20 回复(1)
#include <iostream>
using namespace std;
int times(int n, int x)
{
    int cnt = 0;
    while (n)
    {
        int e = n % 10;
        if (e == x) cnt ++;
        n /= 10;
    }
    return cnt;
}

int main()
{
    int n, x;
    cin >> n >> x;
    int res = 0;
    for (int i = 1; i <= n; i ++ )
        res += times(i, x);
    cout << res << endl;

}

发表于 2022-02-26 14:26:15 回复(0)
#include <iostream>
using namespace std;

int main() {
    long number;
    int a;
    cin >> number >> a;
    int cnt = 0;
    for (long i = 1; i < number + 1; ++i){
        long o = i;
        while(o){
            if(o%10==a){
                ++cnt;
            }
            o /= 10;
        }
    }
    cout << cnt;
}
// 64 位输出请用 printf("%lld")
发表于 2025-12-11 19:21:54 回复(0)
int main() {
    int r;
    char c;
    cin >> r >> c;
    int sum = 0;
    for(int i = 1; i <= r; i ++) for(auto v : to_string(i)) if(v == c) sum ++;
    cout << sum;
}
发表于 2025-12-06 15:05:02 回复(0)