pat1022:D进制的A+B

https://www.patest.cn/contests/pat-b-practise/1022
#include "stdio.h"

int main()
{
	int a, b, d, i, s, flag = 0;
	char num[32] = {0};
	scanf("%d %d %d", &a, &b, &d);
	s = a + b;
	if(0 == s)
		printf("0");
	for(i = 0; i < 32; ++i)
		if(s > 0)
		{
			num[i] = s % d + '0';
			s = s / d;
		}
	for(i = 31; i >= 0; --i)
		if((num[i] > 0) || (1 == flag))
		{
			printf("%c", num[i]);
			flag = 1;
		}
	printf("\n");

	return 0;
}

你可能感兴趣的:(c,pat)