题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047

题目意思:就是求大整数加法。有多个案例,每个案例之间要输出一个空格。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
char t[maxn];
int s[maxn], ans[maxn]; int main()
{
int l1, l2, n, i, j, l, len, num;
scanf("%d", &n);
while (n--)
{
num = ;
memset(ans, , sizeof(ans));
while (cin >> t && strcmp(t, ""))
{
memset(s, , sizeof(s));
l1 = strlen(t);
for (i = ; i < l1; i++)
s[i] = t[l1-i-] - '';
num++;
if (num == )
{
for (i = ; i < l1; i++)
ans[i] = s[i];
l2 = l1;
}
else
{
len = max(l1, l2); // l1:s[i] l2:ans[i]
l = min(l1, l2);
int sum, c = ;
for (i = ; i < len; i++)
{
if (i < l)
{
sum = s[i] + ans[i] + c;
ans[i] = sum % ;
c = (sum > ? : );
}
else
{
if (l1 == len)
sum = s[i] + c;
else
sum = ans[i] + c;
ans[i] = sum % ;
c = (sum > ? : );
}
}
if (c == && i == len)
{
ans[len] = ;
l2 = len + ;
}
else
l2 = len; // 关键之处!!!!之前不记得更新错了好多次!!!
}
}
if (!num)
printf("");
else
{
for (i = maxn-; i >= ; i--)
{
if (ans[i])
break;
}
for ( ; i >= ; i--)
printf("%d", ans[i]);
}
printf("\n");
if (n)
cout << endl;
}
return ;
}

  忽略格式问题,借鉴了别人的写法

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e5 + ;
char t[maxn];
int num[maxn]; void add(char s[])
{
int z = ;
int len = strlen(s);
for (int i = len-; i >= ; i--)
{
num[z] += (s[i] - '');
num[z+] += num[z] / ;
num[z] %= ;
z++;
}
} int main()
{
int i, ok = ;
memset(num, , sizeof(num));
while (scanf("%s", t) != EOF && strcmp(t, ""))
add(t);
for (i = maxn; i >= ; i--)
{
if (num[i])
{
ok = ;
break;
}
}
for ( ; i >= && ok; i--)
printf("%d", num[i]);
if (!ok)
printf("");
printf("\n");
return ;
}

hdu Integer Inquiry 解题报告的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  2. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  3. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  4. hdu 1896.Stones 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...

  5. Valentine's Day Round 1001.Ferries Wheel(hdu 5174)解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1] ...

  6. BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...

  7. BestCoder27 1002.Taking Bus(hdu 5163) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163 题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di ( ...

  8. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  9. BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...

随机推荐

  1. Codeforces Round #295 (Div. 1) C. Pluses everywhere

    昨天ZZD大神邀请我做一道题,说这题很有趣啊. 哇,然后我被虐了. Orz ZZD 题目大意: 你有一个长度为n的'0-9'串,你要在其中加入k个'+'号,每种方案就会形成一个算式,算式算出来的值记做 ...

  2. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  3. MVC项目引用备注

    新建空的WEB应用 MVC Nuget引用:Microsoft.AspNet.MvcMicrosoft.AspNet.Web.Optimization 可选删除 Microsoft.CodeDom.P ...

  4. GDB调试动态链接库

    http://cyukang.com/2012/06/25/gdb-with-libso.html http://cyukang.com/2011/05/06/valgrind.html

  5. serialVersionUID的作用以及如何用idea自动生成实体类的serialVersionUID

    转载:http://blog.csdn.net/liuzongl2012/article/details/45168585 serialVersionUID的作用: 通过判断实体类的serialVer ...

  6. 日志打印longging模块(控制台和文件同时输出)

    在把日志写入文件的同时在控制台输出 示例代码如下: #coding=utf-8 import logging import time import os dir = os.path.dirname(o ...

  7. FastDFS的配置、部署与API使用解读(5)FastDFS配置详解之Tracker配置(转)

    本文是 tracker.conf 配置文件的详细介绍. 1 基本配置 disable #func:配置是否生效 #valu:true.false disable=false bind_addr #fu ...

  8. HDU 3564 Another LIS splay(水

    题意: 给定一个空序列 插入n个数(依次插入 1.2.3.4··n) 以下n个数表示i插在哪个位置. 每插入一个数后输出这个序列的lis 然后... 由于每次插入的数都是当前序列最大的数 所以不会影响 ...

  9. uboot下载地址

    非常奇怪百度搜索都搜不到一个好的uboot下载的说明,仅此标记 HOME http://www.denx.de/wiki/U-Boot/SourceCode sourcecode http://www ...

  10. (全然背包)小P寻宝记——好基友一起走

    题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是相当开心啊.回来就告诉了他的好基友小鑫.于是他们又结伴去伊利哇呀国寻宝. 这次小P的寻宝之路可没有那么的轻松,他们走到了一个森林,小鑫一 ...