You are going to read four numbers: n, a, b and c, like this:
12 2 5 3
First, n is used to build up a string from 0 to n, like this:
0123456789101112
is a string build up for n=12.
Then, in all the digits from index a to index b, count the appearence of c.
For the string above, 2 5 is:
2345
Thus the appearence of 3 is 1.
Input Format:
Four positive numbers, n, a, b and c, where a<b<n<10000, and 0<=c<=9..
Output Format:
One number represnets the length of the generated string. One number represents the apprence of c. There is a space between the two numbers.
Sample Input:
12 2 5 3
结尾无空行
Sample Output:
16 1
结尾无空行
代码如下
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
int n,a,b,c;
scanf("%d %d %d %d",&n,&a,&b,&c);
int j=0,l=0,sum=0;
for(int i=0;i<=n;i++)
{
if(!i)//特判0是不是满足条件
{
if(a==0&&c==0)
sum++;
l++;
continue;
}
j=i;
vector<int> v;
while(j)//将数分成1个个0-9的数
{
v.push_back(j%10);
j/=10;
}
for(int j=v.size()-1; j>=0; j--)//记录c在a-b位置出现的次数
{
if(l>=a&&l<=b&&v[j]==c)
sum++;
l++;
}
}
printf("%d %d",l,sum);//输出长度和总数
return 0;
}
emmm,比赛时,吃了英语的亏
这篇文章以互相学习为主,有什么错的还望告知,谢谢啦