Digit Count
时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Dr. Orooji’s children have played Tetris but are not willing to help Dr. O with a related problem.
Dr. O’s children don’t realize that Dr. O is lucky to have access to 100+ great problem solvers and
great programmers today!
Given a range (in the form of two integers) and a digit (0-9), you are to count how many
occurrences of the digit there are in the given range.
输入描述:
There is only one input line; it provides the range and the digit. Each integer for the range will be between 1000 and 9999 (inclusive) and the digit will be between 0 and 9 (inclusive). Assume the first integer for the range is not greater than the second integer for the range.
输出描述:
Print the number of occurrences of the digit in the given range.
示例1
输入
复制
1000 1000 0
输出
复制
3
示例2
输入
复制
1000 1001 0
输出
复制
5
示例3
输入
复制
8996 9004 5
输出
复制
0
示例4
输入
复制
9800 9900 5
输出
复制
20
#include<iostream>
#include<set>
#include<cstring>
int solve(int x,int k){
int sum=0;
int a,b,c,d;
a=x/1000;
if(a==k) sum++;
b=(x-a*1000)/100;
if(b==k) sum++;
d=x%10;
if(d==k) sum++;
c=(x%100-d)/10;
if(c==k) sum++;
return sum;
}
using namespace std;
int main(){
int a,b;
int k;
cin>>a>>b>>k;
int res=0;
for(int i=a;i<=b;i++){
res+=solve(i, k);
}
cout<<res;
}