【CCF-201912-1】——Report the number

Insert picture description here
Insert picture description here

Note that only if it is not a multiple of 7, nor does it contain 7, the sum will increase automatically until it is greater than the value of n. If one of the two situations of 7 is met, it will be skipped. The counting of A, B, C, D can be done through an array. When judging, pay attention to four cycles, so you can judge by taking the remainder of 4.
#include <iostream>
using namespace std;

bool ishave(int n){
    
    
	if(n==0)
		return false;
	if(n%7==0)
		return true;
	while(n!=0){
    
    
		if(n%10==7)
			return true;
		n /= 10;
	}
	return false;
}

int main(){
    
    
	int n;
	cin >> n;
	int sum = 1,cnt[4] = {
    
    0};
	for(int i = 1;sum<=n;i++){
    
    
		if(!ishave(i))
			sum++;
		else if(i%4==1&&ishave(i)){
    
    
			cnt[0]++;
		}
		else if(i%4==2&&ishave(i)){
    
    
			cnt[1]++;
		}
		else if(i%4==3&&ishave(i)){
    
    
			cnt[2]++;
		}
		else if(i%4==0&&ishave(i)){
    
    
			cnt[3]++;
		}
	}
	for(int i = 0;i<4;i++){
    
    
		cout << cnt[i] << endl;
	}
	return 0; 
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108597248