杭电OJ | 1197 A hard puzzle 2031 进制转换

1197 A hard puzzle

Problem Description

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.

Output

Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.

Sample Output

2992

2993

......

要点:进制转换问题

#include <stdio.h>
#include <math.h>
int sum(int start, int base);
int main(){
	int start;
	for(start = 2991; start<=9999; start++){
		int dec = sum(start,10);
		int duo = sum(start,12);
		int hex = sum(start,16);
		if(duo==dec&&hex==dec){
			printf("%d\n",start);
		}	
	}
	return 0;
}
int sum(int start, int base){
	int i,remain;
	int sum = 0;
	for(i=0;start>0;i++){
		remain = start%base;
		sum = sum+remain;
		start = start/base;
	}
	return sum;	
}

2031 进制转换

Problem Description    输入一个十进制数N,将它转换成R进制数输出。

Input         输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。

Output     为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(如10用A表示)。

Sample Input

7 2

23 12

-4 3

Sample Output

111

1B

-11

#include <stdio.h>
int main() {
    int num, base;
    while (scanf("%d %d", &num, &base) != EOF) {
        int quo = num;
        int flag = 1;
        if (quo < 0) {
            quo = -quo;
            flag = 0;
        }
        int remain, j;
        int i = 0;
        char output[50];
        while (quo != 0) {
            remain = quo % base;
            if (remain > 9) {
                if (remain == 10) {
                    output[i] = 'A';
                } else if (remain == 11) {
                    output[i] = 'B';
                } else if (remain == 12) {
                    output[i] = 'C';
                } else if (remain == 13) {
                    output[i] = 'D';
                } else if (remain == 14) {
                    output[i] = 'E';
                } else if (remain == 15) {
                    output[i] = 'F';
                }
            } else {
                output[i] = remain + '0';
            }
            quo = quo / base;
            i++;
        }
        if (flag == 0) {
            printf("-");
        }
        for (j = i - 1; j >= 0; j--) {
            printf("%c", output[j]);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值