Title Description
Calculate the range of 1 to all integers n, the digital x (0 ≤ x ≤ 9) were appeared many times? For example, in the 1 to 1 1, i.e. in 1,2,3,4,5,6,7,8,9,10,11 , the number 1 appears four times.
Input Format
Two integers n-, X , separated by a space between.
Output Format
1 integer representing the number of times x appears.
Sample input and output
Entry
11 1
Export
4
Description / Tips
For . 1 0 0 % of the data, . 1 ≤ n- ≤ . 1 , 0 0 0 , 0 0 0 , 0 ≤ X ≤ . 9.
Code:
#include<cstdio>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
int main(){
int x,n;
scanf("%d %d",&n,&x);
int sum=0;
stringstream ss;
string s;
for(int i=1;i<=n;i++){
ss<<i;
}
s=ss.str();
printf("%d",count(s.begin(), s.end(), x + '0'));
}
Encounter problems:
-
[Error] stray '\ 243'in program Cause: The compiler encounters illegal characters unrecognized when compiling the source file
- [Error] 'to_string' was not declared in this scope the wrong reasons: to_string is c ++ 11 inside, but no dev c ++ 11 environment, we only need to find the Tools -> compiler option to add -std = c + +11
- stringstream use
-
#include<cstdio> #include<iostream> #include<string> #include<sstream> //stringstream 所需的头文件 using namespace std; int main(){ string s="35"; string tem; stringstream ss; int a; ss<<s;//将 s 读入字符串流中 ss>>a;//把字符串转为数值 tem=ss.str();//stringstream 型不能直接输出,也不支持迭代器,需要利用 stringstream 类的成员函数str()才能把它当成普通字符串来用 }