【洛谷 1980】计数问题

题目描述

试计算在区间 11 到 nn的所有整数中,数字x(0 ≤ x ≤ 9)x(0x9)共出现了多少次?例如,在 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出现的次数。

输入输出样例

输入 #1
11 1
输出 #1
4

说明/提示

对于 100\%100%的数据,1≤ n ≤ 1,000,000,0 ≤ x ≤ 91n1,000,000,0x9。

题解:切红题真的很快乐,5分钟一题,红题他不香吗?!

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
int n,x,ans;
int main(){
    //freopen("1980.in","r",stdin);
    //freopen("1980.out","w",stdout);
    scanf("%d %d",&n,&x);
    for(int i=1;i<=n;i++){
        int c=i;
        while(c>0){
            ans+=(c%10==x);
            c/=10;
        }
    }
    cout<<ans;
    return 0;
}
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11851164.html