题目链接: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. eclipse不会自动编译的问题解决

    注意:非必要的时候,重新下载eclipse安装是最有效的解决方法. 以下为尝试的步骤: 1.看看project->Build Automatically有没有勾上?如果没有,勾上以后,clean ...

  2. iOS开发 编码规范

    转至   http://www.cnblogs.com/celestial/archive/2012/06/30/2571417.html 编码规范 一.文档结构管理 1.建立Libraries文件夹 ...

  3. 一个Netfilter nf_conntrack流表查找的优化-为conntrack添加一个per cpu cache

    独悲须要忍受.快乐须要分享对Linux协议栈多次perf的结果,我无法忍受conntrack的性能,然而它的功能是如此强大,以至于我无法对其割舍,我想自己实现一个高速流表.可是我不得不抛弃依赖于con ...

  4. Naive Bayesian文本分类器

    贝叶斯学习方法中有用性非常高的一种为朴素贝叶斯学习期,常被称为朴素贝叶斯分类器. 在某些领域中与神经网络和决策树学习相当.尽管朴素贝叶斯分类器忽略单词间的依赖关系.即如果全部单词是条件独立的,但朴素贝 ...

  5. 江湖问题研究-- intent传递有没有限制大小,是多少?

    出门一步,便是江湖.江湖上有很多流言. 比方这条: intent传递是有限制大小的,详细在40KB左右. 当然也有传言说是1M左右. 数百头母驴为何半夜慘叫? 小卖部安全套为何屡遭黑手? 女生宿舍内裤 ...

  6. IOS_DatePicker_PickerView_SegmentControl_键盘处理

    H:/0712/01_UIController_MJViewController.m // MJViewController.m // 01-总结复习 // Created by apple on 1 ...

  7. MySQL 下优化SQL语句的一些经验

    http://java-guru.iteye.com/blog/143377

  8. Net is as typeof 运行运算符详解 net 自定义泛型那点事

    Net is as typeof 运行运算符详解   概述 在了解运行运算符的前提我们需要了解什么是RTTI ,在任何一门面向对象的语言中,都有RTTI这个概念(即 运行时). RTTI(Run-Ti ...

  9. Java集合01----ArrayList的遍历方式及应用

                                                 Java集合01----ArrayList的遍历方式及应用 前面已经学习了ArrayList的源代码,为了学以 ...

  10. 轻松搞定RabbitMQ(二)——工作队列之消息分发机制

    转自 http://blog.csdn.net/xiaoxian8023/article/details/48681987 上一篇博文中简单介绍了一下RabbitMQ的基础知识,并写了一个经典语言入门 ...