其他分享
首页 > 其他分享> > A - Specialized Four-Digit Numbers

A - Specialized Four-Digit Numbers

作者:互联网

Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.
For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 11728 + 8144 + 9*12 + 3, its duodecimal representation is 189312, and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program.
The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don’t want decimal numbers with fewer than four digits – excluding leading zeroes – so that 2992 is the first correct answer.)
题目的主要意思其实就是在十进制的四位数里找到将其转化为16进制和12进制,他们每个位置的位数的和都相等的数。

#include <iostream>
using namespace std;

int search(int base, int n){
	int sum=0;
	while(n){
		sum+=n%base;	//每个位置上的数 
		n/=base;
	}
	return sum;
}

int main(){
	for(int i=1000; i<=9999; i++){
		int a=search(10,i);
		if(a==search(16,i)&&a==search(12,i)){	//判断和是否相等 
			cout<<i<<endl;
		}
	}
} 

标签:digits,Digit,int,2991,sum,Four,base,Specialized,its
来源: https://blog.csdn.net/qq_46092927/article/details/112774828