首页 > 试题广场 >

记数问题

[编程题]记数问题
  • 热度指数: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 次。

备注:

头像 叶花永不相见
发表于 2022-02-24 20:46:19
试计算在区间1 到n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次? 例如,在1到11 中,即在1、2、3、4、5、6、7、8、9、10、11 中,数字1 出现了4 次。 #include<stdio.h> int main() { int n, x; sca 展开全文
头像 刘柏
发表于 2020-10-17 14:21:03
记数问题 用 vector +count 对于这个问题 要统计0~9 出现的个数。可以分为两种情况:一,数字小于10可以直接push_back();放入vector里。 二,数字大于10首先想到的就是把数字一位一位 拆分出来 放到vector 最后统计个数。拆分个位:n=n%10;进位 :n/=10 展开全文
头像 月采琉疆
发表于 2021-02-02 16:26:32
sprintf函数的使用是把n以“%d”的格式写到str字符串数组中(还是从右至左) #include<cstdio> #include<cstring> void fun(int *num, char *str){ for(int i = 0; i < str 展开全文
头像 Hanson_Zhong
发表于 2022-04-16 14:05:28
https://ac.nowcoder.com/acm/contest/19306/1002 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 试计算在区间1 到n 的所有整数中 展开全文
头像 zzfyupup
发表于 2022-06-06 19:59:56
#include<stdio.h> int main() { int a,b; int i; int count=0; scanf("%d %d",&a,&b); for(i=1;i<=a;i++) { 展开全文
头像 多刷刷
发表于 2022-09-26 15:45:47
#借助count()实现 n,x = map(int,input().split()) ls = ''.join([str(i) for i in range(1,n+1)]) print(ls.count(str( 展开全文
头像 Ghostboy
发表于 2019-09-05 20:11:52
题目描述试计算在区间1 到n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?例如,在1到11 中,即在1、2、3、4、5、6、7、8、9、10、11 中,数字1 出现了4 次。 输入描述:输入共1行,包含2个整数n、x,之间用一个空格隔开。输出描述:输出共1行,包含一个整数,表示x出现的 展开全文
头像 cfn
发表于 2023-03-05 17:27:39
n,x=map(int,input().split()) list1=[] for i in range(1,n+1): if str(x) in str(i): list1.append(str(i).count(str(x))) print(sum(list1))
头像 柯kkkkkkkkkk
发表于 2025-09-15 19:06:40
#include <iostream> using namespace std; int main() { int n, x,zhi=0; cin>>n>>x; for(int i=1;i<=n;i++){ int 展开全文
头像 夜语声烦-
发表于 2022-02-07 01:21:27
好吧,说实话我还以为是计数类DP,没想到完全想复杂了,不过我这是万能的,可以求任意x~y直接任意z的出现次数,用前缀和就行cnt(y,z) - cnt(x-1,z) # include <iostream> # include <cmath> using namespace 展开全文