您的位置:首页 > 其它

HDOJ 1047 大数加法 水

2012-08-20 21:06 225 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047

虽然说这题水,但我也没一次过。第一次没有考虑到一个输入块只包含0的情况。

#include <iostream>
using namespace std;

const int LEN = 200;
//图方便,将这些都作为全局变量
char aLine[LEN];
int oneInput[LEN],res[LEN];

//将aLine以逆序转成oneInput
void toInt()
{
int len = strlen(aLine);
memset(oneInput,0,sizeof(oneInput));
for (int i = 0;i < len;i ++)
oneInput[i] = aLine[len - 1 - i] - '0';
}

void Add()
{
for (int i = 0;i < LEN - 1;i ++)
res[i] += oneInput[i];
for (int i = 0;i < LEN - 1;i ++)
{
if (res[i] >= 10)
res[i + 1] += res[i] / 10;
res[i] %= 10;
}
}

int main ()
{
int blockNum;
scanf("%d",&blockNum);
for (int i = 1;i <= blockNum;i ++)
{
memset(res,0,sizeof(res));
while (scanf("%s",aLine) != -1)
{
if (strlen(aLine) == 1 && aLine[0] == '0')
break;
toInt();
Add();
}
int pos = LEN - 1;
//在pos=1时停止,可以处理一个input block只有一个0的情况
for (;pos >= 1;pos --)
if (res[pos] != 0)
break;
for (;pos >= 0;pos --)
printf("%d",res[pos]);
if (i < blockNum)
printf("\n\n");
else
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: