P1980

题目描述

试计算在区间 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出现的次数。

 

输入输出样例

输入样例#1: 复制

11 1

输出样例#1: 复制

4

 

应用:sstream的使用

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<math.h>
#include<string.h>
#include<cmath>
#include<string>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 5555;
const int inf = 0x7f7f7f7f;
typedef long long ll;
typedef unsigned long long ull;
ll n;
char x;
string s;
int main()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        stringstream ss;
        ss << i;
        s += ss.str();
    }
    ll ans = 0;
    ll size = s.size();
    for (int i = 0; i<size; i++) {
        if (s[i] == x)ans++;
    }
    cout << ans << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章