HDOJ 1047 大数加法 水
2012-08-20 21:06
225 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047
虽然说这题水,但我也没一次过。第一次没有考虑到一个输入块只包含0的情况。
虽然说这题水,但我也没一次过。第一次没有考虑到一个输入块只包含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; }
相关文章推荐
- POJ 1053 Integer Inquiry && HDOJ 1047 Integer Inquiry (大数加法)
- HDOJ1047大数相加
- [HDOJ] 1002 大数加法(坑爹格式
- 杭电OJ(HDOJ)1047题:Integer Inquiry(大数操作——加运算)
- HDOJ-1002 用JAVA写大数问题[大数加法]
- hdoj 1002 A + B Problem II(大数加法)
- hdoj1002(大数加法)
- HDOJ1002 A + B Problem II(大数加法)
- hdoj-1877,m进制大数加法
- hdoj 4523 切蛋糕 腾讯马拉松 大数加法比较
- HDOJ-1250 大数斐波那契问题[大数加法]
- HDOJ 1002:大数加法
- hdoj-1002 大数加法
- hduoj1047,Integer Inquiry,大数加法,跟poj某一题一样,再谈
- HDOJ1002大数加法
- 【大数问题】 HDOJ 1047 Integer Inquiry
- hdoj 1047 Integer Inquiry(几个大数连续加)
- HDU1047 - Integer Inquiry (大数加法)
- hdoj 1002 A + B Problem II【大数加法】
- HDOJ 1002 大数加法