题目链接:http://soj.sysu.edu.cn/1014
题目大意
找出所有在10进制、12进制、16进制表示下,各个数位上的数字的和相同的所有四位数。
代码
/**
* 1014.cpp-Specialized Four-Dig: http://soj.sysu.edu.cn/1014
* find all 4-digit numbers that the sum of the digits in demical, hexademical
* and duodemical are all same.
* Copyright (c) 2014 Junjie Huang@SYSU(SNO:13331087). All Rights Reserved.
**/
#include
using namespace std;
int main() {
for (int n = 2992; n < 10000; n++) {
int k, sum1 = 0, sum2 = 0, sum3 = 0;
k = n;
while (k) {
sum1 += k % 10;
k /= 10;
}
k = n;
while (k) {
sum2 += k % 12;
k /= 12;
}
k = n;
while (k) {
sum3 += k % 16;
k /= 16;
}
if (sum1 == sum2 && sum2 == sum3) cout << n << endl;
}
return 0;
}