[soj1014]Specialized Four-Dig(10进制,12进制和16进制各位相加相等)

来源:互联网 发布:分布式数据库架构 编辑:程序博客网 时间:2024/06/05 01:08

题目描述:

输出所有的10进制4位数
并且,这个数,要满足10进制和12进制还有16进制各位数上相加是相等的。
那么这个数就是我们要的,所有,就把这个数给输出来。

AC代码如下:

// Problem#: 1014// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>using namespace std;int ans=0;bool check12(int x) {    int ans12 = 0;    while (x){        ans12 += (x % 12);        x /= 12;    }    if (ans12 == ans) {        return true;    } else {        return false;    }}bool check16(int x) {    int ans16 = 0;    while (x){        ans16 += (x % 16);        x /= 16;    }    if (ans16 == ans) {        return true;    } else {        return false;    }}void getAns(int x) {    ans = 0;    while (x) {        ans +=  x % 10;        x /= 10;    }}int main(){    for (int i = 2992; i <= 9999; ++i) {        getAns(i);        if (check12(i) && check16(i)) {            cout << i<< endl;        }    }}