String Builder

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, nab 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

结尾无空行

 

代码如下

 1 #include<stdio.h>
 2 #include<vector>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,a,b,c;
 7     scanf("%d %d %d %d",&n,&a,&b,&c);
 8     int j=0,l=0,sum=0;
 9     for(int i=0;i<=n;i++)
10     {
11         if(!i)//特判0是不是满足条件
12         {
13             if(a==0&&c==0)
14                 sum++;
15             l++;
16             continue;
17         }
18         j=i;
19         vector<int> v;
20         while(j)//将数分成1个个0-9的数
21         {
22             v.push_back(j%10);
23             j/=10;
24         }
25         for(int j=v.size()-1; j>=0; j--)//记录c在a-b位置出现的次数
26         {
27             if(l>=a&&l<=b&&v[j]==c)
28                 sum++;
29             l++;
30         }
31     }
32     printf("%d %d",l,sum);//输出长度和总数
33     return 0;
34 }

emmm,比赛时,吃了英语的亏

这篇文章以互相学习为主,有什么错的还望告知,谢谢啦

posted on 2021-11-26 15:48  LonelinessKid  阅读(164)  评论(0)    收藏  举报

导航