It's okay to brush LetCode - divisible lucky number

divisible lucky number

topic description

Assume that the lucky number is a positive integer containing only 4 or 7, such as 7, 47. Determine whether a positive integer is divisible by a lucky number. If yes, output YES, otherwise output N0. Negative numbers, 0 or other special cases are not considered. The case of outflow or exceeding the integer range is not considered.

Example 1:

输入:47
输出:YES
解释:47能被幸运数47整除 。

Brute force solution :

From 1 to input n, traverse all the factors of n, and judge whether it is a lucky number one by one, output YES if yes, otherwise output NO. (The violent solution is too simple, and the implementation method is not given here.)

Lucky number traversal method :

We need to find a way to list all the lucky numbers, and judge whether the input can be divisible by the lucky number by the remainder between the input and these lucky numbers. If the lucky number is evenly divisible, output NO.

get lucky number

Now, let's assume that there is such a problem: Suppose the lucky number is a positive integer that only contains 4 or 7, such as 7, 47. We need to get the Kth largest lucky number through the given value K. For example, if 1 is given, it will be 4, and if 8 is given, it will be 447. Now we need to solve this problem.

We assume that there is a tree, as shown below.
insert image description here
Then, when a K value is given, what is the number we require, it is mapped into the following steps

1: Which layer is this number in the tree, that is, h=int(log2(K+1)+1)
2: How many numbers are there in total above the hth layer, that is, num=pow(2, h- 1) - 1, note that this contains meaningless primary nodes.
3: Which number is the number in the h layer, starting from 0, for example, 444 is the 0th number in the 4th layer, that is, idx=K-num

At this point, suppose we want to get the 8th number (447), that is, h=int(log2(8+1)+1)=4; num=
pow(2,4-1)-1=7;idx= 8-7=1;
that is, the first point of the 4th floor.
The fourth layer, we know that this number must be a three-digit number, 1 can be converted into binary 001,
0 corresponds to 4, 1 corresponds to 7, then 001 is 447. It was solved.

The procedure is as follows:

# 翻转整数值 例:123->321
int reverseNum(int num) {
    
    
	int result = 0, temp;
	while (true) {
    
    
		if (num != 0) {
    
    
			temp = num % 10;
			result = result * 10 + temp;
			num = num / 10;
		}
		else {
    
    
			break;
		}
	}
	return result;
}

# 从小到大获取第K大的幸运数
int getLuckyNum(int K) {
    
    
	int h, num;
	# 求得层数
	h = log2(K + 1) + 1;
	# 求得前几层的总共节点数
	num = pow(2, h - 1) - 1;
	# 求得是第h层的第几个,从0开始
	int idx = K - num;
	int ans = 0;
	for (int i = 0; i < h - 1; i++) {
    
    
		# 与最低位取并集,为1就是7,为0就是4
		if ((idx & 1) == 0) {
    
    
			ans = ans * 10 + 4;
		}
		else {
    
    
			ans = ans * 10 + 7;
		}
		idx = idx >> 1;
	}
	# 翻转数值,获得最终的幸运数
	ans = reverseNum(ans);
	return ans;
}

Lucky number traversal method

int reverseNum(int num) {
    
    
	int result = 0, temp;
	while (true) {
    
    
		if (num != 0) {
    
    
			temp = num % 10;
			result = result * 10 + temp;
			num = num / 10;
		}
		else {
    
    
			break;
		}
	}
	return result;
}

int getLuckyNum(int K) {
    
    
	int h, num;
	h = log2(K + 1) + 1;
	num = pow(2, h - 1) - 1;
	int idx = K - num;
	int ans = 0;
	for (int i = 0; i < h - 1; i++) {
    
    
		if ((idx & 1) == 0) {
    
    
			ans = ans * 10 + 4;
		}
		else {
    
    
			ans = ans * 10 + 7;
		}
		idx = idx >> 1;
	}
	ans = reverseNum(ans);
	return ans;
}

int main( )
{
    
    
    int n, ans;
    # 输入数值
    scanf("%d", &n);
    for (int i = 1; i<=n; i++){
    
    
     	# 遍历幸运数
        ans = getLuckyNum(i);
        # 如果幸运数比输入还大,则该数不能被任意幸运数整除。
        if (ans > n){
    
    
            printf("NO");
            break;
        }
        else{
    
    
        	# 如果输入能被幸运数整除,则输出YES。
            if (n % ans == 0){
    
    
                printf("YES");
                break;
            }
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/REstrat/article/details/128499319