P1980 计数问题

P1980 计数问题

/ 0评 / 1391次 / 0

题目描述

试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 11到1111中,即在 1,2,3,4,5,6,7,8,9,10,111,2,3,4,5,6,7,8,9,10,11 中,数字 11 出现了 44 次。

输入格式

22个整数n,xn,x,之间用一个空格隔开。

输出格式

11个整数,表示xx出现的次数。

代码

/*
* @Author: Gehrychiang
* @Date:   2019-08-21 22:14:17
* @Last Modified by:   Gehrychiang
* @Last Modified time: 2019-08-21 22:46:57
* @Email:gehrychiang@ailiyun.com
* @Website:www.yilantingfeng.site
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
	int n;
	char x;
	cin>>n>>x;
	int count;
	for (int i = 1; i <=n ; ++i)
	{
		char tmp[1000000];
		sprintf(tmp,"%d",i);
		for (int i = 0; tmp[i]; ++i)
		{
			if (tmp[i]==x)
			{
				count=count+1;
			}
		}
	}
	cout<<count<<endl;
	return 0;
}

发表回复

您的电子邮箱地址不会被公开。

你好 No.62993