String Builder (15 分)

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<bits/stdc++.h>
using namespace std;
int main(){
	int n,a,b,c;//12 2 5 3   --->16 1
	cin>>n>>a>>b>>c;
	int j=0,l=0,sum=0;
	for(int i=0;i<=n;i++){
		if(!i){
			if(a==0&&c==0)
				sum++;
			l++;
			continue;
		}
		j=i;
		vector<int> v;
		while(j){
			v.push_back(j%10);
			j/=10;
		}
		for(int j=v.size()-1;j>=0;j--){
			if(l>=a&&l<=b&&v[j]==c)
				sum++;
				
			l++;
			
		}
	}
	cout<<l<<" "<<sum;
	return 0;
	
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>