天天看點

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.

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;
  
}      

繼續閱讀