yzl_rex

对于算法,我只是一个草民!

zoj 1874 Primary Arithmetic
#include "stdio.h"
#include "iostream"
#include "string.h"
using namespace std;

int main()
{
	char input1[20], input2[20];//对输入的字符的保存
	int num1[20], num2[20];//由字符转换为数字的保存
	int len1, len2, i, len, carry, temp, c;
	while (scanf("%s%s", input1, input2))
	{
		carry = c = 0;
		memset(num1, 0, sizeof(num1));
		memset(num2, 0, sizeof(num2));
		if (strcmp(input1, "0") == 0 && strcmp(input2, "0") == 0) break;
		len1 = strlen(input1), len2 = strlen(input2);

		for (i = 0; i < len1; i++)
			num1[len1-1-i] = input1[i] - 48;
		for (i = 0; i < len2; i++)
			num2[len2-i-1] = input2[i] - 48;
		len = len1 > len2 ? len1 : len2;

		for (i = 0; i < len; i++)
		{
			temp = num1[i] + num2[i] + c;
			if (temp >= 10)
			{
				carry++;
				c = temp / 10;
			}
		}
		if (carry == 1)
			printf("%d carry operation.\n", carry);
		else if (carry >1)
			printf("%d carry operations.\n", carry);
		else if (carry == 0)
			printf("No carry operation.\n");
	}
}

阅读更多
文章标签: input include c
个人分类: ZOJ
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

zoj 1874 Primary Arithmetic

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭