我的PAT系列文章更新重心已移至Github,歡迎來(lái)看PAT題解的小伙伴請(qǐng)到Github Pages瀏覽最新內(nèi)容。此處文章目前已更新至與Github Pages同步。歡迎star我的repo。
題目
輸入兩個(gè)非負(fù) 10 進(jìn)制整數(shù) 和
(
),輸出
的
(
)進(jìn)制數(shù)。
輸入格式:
輸入在一行中依次給出 3 個(gè)整數(shù) 、
和
。
輸出格式:
輸出 的
進(jìn)制數(shù)。
輸入樣例:
123 456 8
輸出樣例:
1103
思路
A、B和A+B的范圍在32位整型的范圍內(nèi),因此用int就好不需要擔(dān)心。
進(jìn)制的轉(zhuǎn)換也是很簡(jiǎn)單的,為了不使用數(shù)組,可以從最高位開始輸出,這樣就要先判斷D進(jìn)制下的位數(shù)。
代碼
最新代碼@github,歡迎交流
#include <stdio.h>
int main()
{
int A, B, D, Sum;
scanf("%d %d %d", &A, &B, &D);
Sum = A + B;
/* calculate the bits of Sum */
int power = 1;
/* use Sum / D >= power to avoid using long int */
while(Sum / D >= power) power *= D;
/* calculate D-base number. print them on-the-fly */
for(; power > 0; Sum %= power, power /= D)
printf("%d", Sum / power);
return 0;
}