hdu Integer Inquiry 解题报告
题目链接: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 解题报告的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
- hdu 1896.Stones 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...
- 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] ...
- BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...
- BestCoder27 1002.Taking Bus(hdu 5163) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163 题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di ( ...
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...
随机推荐
- Silverlight 离线安装包
直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十五:IT人屈辱之道-十倍奉還
現代人普遍火氣都很大,與人爭論時只要有一點點感到屈辱,便會開始大聲反擊,甚至於暴力相向.至於企業中的人事相鬥,則是典型的來個明爭暗鬥,直到成為老闆眼中的紅人,在逐漸掌握了權力之後再來個內部大清洗,不久 ...
- openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
以前学习过用Scoket 建立聊天,简单的建立聊天是没问题的,但如果要实现多人复杂的聊天,后台服务器代码就比较复杂,对于我这新手来讲就比较难了.后来在网上看到用openfire做服务器,利用强大的Sm ...
- const mutable
在C++中,由const修饰的成员函数的函数体内部,是不能够对成员变量进行修改的.这个特性被用来保证某些成员函数在实现过程中,避免由于程序员大意而对数据进行了错误的修改:同时也说明此成员函数是非修改性 ...
- Python3.2官方文档翻译--作用域和命名空间实例
6.2.1 作用域和命名空间实例 以下的实例主要用来示范怎样引用不同的作用域和命名空间,keywordglobal和nonlocalru怎样影响变量绑定. 实例执行结果是: After local a ...
- 利用 apache ab 测试服务器性能
安装步骤:https://blog.csdn.net/ahaaaaa/article/details/51514175 在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bin ...
- SubmittingPatches, SubmitChecklist and CodingStyle
How to Get Your Change Into the Linux Kernel or Care And Operation Of Your Linus Torvalds For a pers ...
- 使用 Kingfisher 处理网络图片的读取与缓存
Kingfisher 是一个读取网络图片和处理本地缓存的开源库,由 onevcat 开发.提到图片缓存库,那么熟悉 Objective-C 开发的同学,可能会想起 SDWebImage. 没错,Kin ...
- caffe搭建--ubuntu标准平台的搭建
http://caffe.berkeleyvision.org/install_apt.html Ubuntu Installation General dependencies sudo apt-g ...
- android启动另一应用
http://www.2cto.com/kf/201203/122910.html Android SDK中有这样一个API: public abstract Intent getLaunchInte ...