UVa 424 Integer Inquiry
之前杭电上也做过a + b的高精度的题,不过这道题的区别是有多组数据。
之前做的时候开了3个字符数组a,b,c,在计算的时候还要比较a,b长度,短的那个还要加'0',还设置了一个add来存放进位。
现在看来这种算法确实很繁琐。
而这次只用了两个字符数组,一个放加数,一个放和。
相比之前程序更短小了,而且可读性也提高了。
果然办法都是逼出来的。
没有了add,在判断进位的时候就看那一位的ASCII码是否>'9',然后进位。
尤其需要注意的一点是可能会出现连续进位的情况,比如99999 + 1。解决的办法就是用循环来控制。
Integer Inquiry |
One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3 to go from 0 to 333 and he explored taking various sums of thosenumbers.
``Thissupercomputer is great,'' remarked Chip. ``I only wish Timothy were here to seethese results.'' (Chip moved to a new apartment, once one became available onthe third floor of the Lemon Sky apartments on Third Street.)
Input
The input willconsist of at most 100 lines of text, each of which contains a singleVeryLongInteger. Each VeryLongInteger will be 100 or fewer characters inlength, and will only contain digits (no VeryLongInteger will be negative).
The final inputline will contain a single zero on a line by itself.
Output
Your programshould output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
AC代码:
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
char Linteger[maxn];
char Resault[maxn]; void Reverse(char s[], int l); int main(void)
{
#ifdef LOCAL
freopen("424in.txt", "r", stdin);
#endif int i;
memset(Resault, '', sizeof(Resault));//将结果全部初始化为'0'
while(gets(Linteger) && Linteger[] != '')
{
int l = strlen(Linteger);
Reverse(Linteger, l);
for(i = ; i < l; ++i)
{
Resault[i] = Resault[i] + Linteger[i] - '';
if(Resault[i] > '')
{
int j = i;
while(Resault[j] > '')//考虑连续进位的情况
{
Resault[j] -= ;
++j;
++Resault[j];
}
}
}
}
for(i = ; i >= ; --i)//输出时忽略前导0
if(Resault[i] != '')
break;
for(; i >= ; --i)
cout << Resault[i];
cout << endl;
return ;
}
void Reverse(char s[], int l)//用来反转数组,从个位开始加起
{
int i;
char t;
for(i = ; i < l / ; ++i)
{
t = s[i];
s[i] = s[l - i -];
s[l - i -] = t;
}
}
代码君
UVa 424 Integer Inquiry的更多相关文章
- UVa 424 Integer Inquiry 【大数相加】
解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
- hdu1047 Integer Inquiry
/* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- hdu 1047 Integer Inquiry
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...
- Poj 1503 Integer Inquiry
1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS Memory Limit: 1000 ...
- hdoj 1047 Integer Inquiry
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- 深入理解javacript之prototype
对于javascript这样一种前端语言,个人觉得,要真正的理解其oop, 就必须要彻底搞清楚javascript的对象,原型链,作用域,闭包,以及this所引用的对象等概念.这些对弄明白了,应该就可 ...
- NET 查找程序集路径(CLR关于Assembly的搜索路径的过程)
最近在回顾.Net应用程序的执行环境,这里做一个很小的总结,方面以后需要的时候进行查找: CLR必须可以找到正确的Assembly,Net提供了Assembly搜索算法,可以根据.config文件(类 ...
- Selenium获取input输入框中值的三种方法
第一种用jQuery的val方法: js = "return $('input').val();" driver.execute_script(js) 第二种用jQuery的att ...
- C# 和Jsonp的一个小demo 用jQuery与JSONP轻松解决跨域访问的问题
客服端: 在A项目下面 建立一个 JsonpClient.aspx页面,代码如下: <%@ Page Language="C#" AutoEventWireup=& ...
- $headers = $this->input->request_headers();返回请求头(header)数组
请查看:CI中的输入类部分 建议用第一个 $headers = $this->input->request_headers() $this->input->request_he ...
- OAuth 2 的简单理解
什么是 OAuth 2.0 根据 oauth.net 的描述,我们可以将它简述为以下内容:OAuth 2.0 是 OAuth 1.0 框架协议的升级版本,简化了多种平台上身份及授权认证的流程. 具体的 ...
- 妙味课堂——HTML+CSS(第三课)
常见标签我已经在上一篇文章中提及,我们做前端设计时,主要也是用这些标签(最常用的). 然而有一个问题,就是有的标签都有自己的默认样式.试通过如下代码来说明: <!DOCTYPE html> ...
- 在Jmeter中使用自定义编写的Java测试代码
我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","Java ...
- tvm install
一.系统需求:1.可以访问互联网2.关闭防火墙和selinux 二.安装步骤(进入软件包所在目录):1.rpm -ivh daemontools-0.76-1.el6.x86_64.rpm2.yum ...
- oracle 密码忘记 找回密码
生活中,容易忘记Oracle数据库system用户的密码,怎么办呢,小生带你一步步重新登上Oracle ,及时你密码忘记了. 1.打开cmd窗口,输入 sqlplus / as sysdba 2.运行 ...