Question B of the Second National College Student Algorithm Design and Programming Challenge: Digital dp-dp

Description

Little x is very interested in digits dp. Now he is solving a problem, asking to solve all the numbers in the closed interval [x,y] that meet the following properties:

  1. The absolute value of the difference between adjacent digits cannot exceed 7.
  2. And the absolute value of the difference between the lowest bit and the highest bit is greater than 2.
    Now, x=13930 and y=457439 are given. Please tell little x the number of digits that meet the requirements.

Thinking analysis

Both x and y are not very large, just use brute force search.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int solve(int x) {
    
    
	int a[6];
	int ind = 0;
	while(x) {
    
    
		int j = x % 10;
		x /= 10;
		a[ind++] = j;
	}
	if(abs(a[ind - 1] - a[0]) <= 2) {
    
    
		return 0;
	}
	for(int i = 1; i < ind; i++) {
    
    
		if(abs(a[i] - a[i - 1]) > 7) {
    
    
			return 0;
		}
	}
	return 1;
}

int main() {
    
    
	int res = 0;
	for(int i = 13930; i <= 457439; i++) {
    
    
		if(solve(i)) {
    
    
			res++;
		}
	}
	cout << res;
	return 0;
}

Guess you like

Origin blog.csdn.net/Cyril_KI/article/details/110558486