HDU 又一版 A+B

题目:输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。

http://acm.hdu.edu.cn/showproblem.php?pid=1877

// LANG: C++
#include 
#include 
#include 
//http://acm.hdu.edu.cn/showproblem.php?pid=1877
//输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
//进制转换,base进制数
void work(unsigned int num, int base)
{
	char str[40] = {0};
	int i = 0;
	// 判断结果是否为0
	if (!num)
	{
		printf("0\n");
		return ;
	}

	while (num)
	{
		str[i++] = num % base + '0';
		num /= base;
	}
	--i;
	while (i >= 0) putchar(str[i--]);
	putchar('\n');
}

//多进制转换,num=源数据,base进制数,buffer输出数据
void work2(unsigned int num, int base,char* buffer)
{
	char str[40] = {0};
	int i = 0;
	int j=0;
	// 判断结果是否为0
	if (!num)
	{
		printf("0\n");
		return ;
	}

	while (num)
	{
		str[i++] = num % base + '0';
		num /= base;
	}
	--i;
	while (i >= 0)
	{

		buffer[j++]=str[i--];
	}
	//printf("%s\n",buffer);
}
// 2 1 7
int main(int argc, char **argv)
{
	unsigned int base, left, right;
	while (1)
	{
		scanf ("%u", &base);
		if (!base) break;
		scanf("%u %u", &left, &right);

		left += right;
		char* buffer;
		buffer=(char*)malloc(sizeof(char*)*1024);
		memset(buffer,0,sizeof(char*)*1024);

		work2(left, base,buffer);
		printf("%s\n",buffer);
	}

	return 0;
}



你可能感兴趣的:(ACM竞赛编程)