ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

A - Specialized Four-Digit Numbers

2021-01-18 13:32:26  阅读:183  来源: 互联网

标签:digits Digit int 2991 sum Four base Specialized its


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有