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;
}

 

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