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 要么为一个正 ...
随机推荐
- linux几种常见的文件内容查找和替换命令
作为一个以前没怎么接触过linux的小白,开始使用linux,各种不习惯,这周遇到一个文件内容测查找和替换的需求.学习了以下几种实现方式: 1.vi命令下的查找和替换 1.1 vi下的查找 /patt ...
- ES6新语法学习
参考: 1.http://es6.ruanyifeng.com/#docs/let#let-命令 2.https://reactjs.org/tutorial/tutorial.html 3.http ...
- Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?
什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...
- NN优化方法对照:梯度下降、随机梯度下降和批量梯度下降
1.前言 这几种方法呢都是在求最优解中常常出现的方法,主要是应用迭代的思想来逼近.在梯度下降算法中.都是环绕下面这个式子展开: 当中在上面的式子中hθ(x)代表.输入为x的时候的其当时θ參数下的输出值 ...
- centos 升级内核失败回救
在升级 centos6.3上使用, yum -y update ... 灾难出现了!!! 解决方法: 1. 在机器启动的时候, 按F1, 会出现选择内核,选一个原来的. 2. vim /etc/gr ...
- 基于cucumber接口测试框架的扩展——测试框架总结之cucumber
主要功能: 1.通过fiddler抓取请求,导出xml文件. 2.解析xml文件至excel,或者手工填写excel数据. 3.根据excel中的URL中地址生成的接口集合和feature内容模板生成 ...
- C++简单介绍
一.怎样用C++的源文件产生一个可运行程序 一个C++程序由一个或者多个编译单元组成.每一个编译单元都是一个独立的源码文件.一般是一个带.cpp的文件,编译器每次编一个文件编译单元,生成一个以.obj ...
- JD笔试试题(凭记忆写的+人生感悟 try finally )
京东笔试:技术篇(一套卷.包含測试.算法,研发) 一:填空题(4分 * 15) 15 个 涉及的面很广的选择题,可是比較側重基础.包含数据结构的.c++类的,操作系统的,计算机网络的. 二:编程题(2 ...
- 几个关于tableView的问题解决方式整合
近期遇到关于tableView的问题的整合.部分比較白痴.仅仅是初学easy犯~ 1.关于tableView左边空余15像素的问题. 2.关于tableView多余切割线隐藏的问题: 3.关于tabl ...
- 【Sprint3冲刺之前】TDzhushou软件项目测试计划书
TDzhushou软件测试计划文档 文档编号:2014-5-8 产品版本:1.1 产品名称:TDzhushou 文 档 作 者: 解凤娇 日期:2014-5-4 软件测试计划 目录 第一章 ...