One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of
powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
“This supercomputer is great,” remarked Chip. “I only wish Timothy were here to see these results.”
(Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky
apartments on Third Street.)
Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInte-
ger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no
VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670

#include <bits/stdc++.h>
using namespace std; string add(string a, string b) {
string c;
int p1 = a.size(), p2 = b.size();
int p = max(p1, p2), l = 0;
while (1) {
int k = (int)a[p1-1]-48+(int)b[p2-1]-48+l;
char m = (char)48+k%10;
c += m;
l = k/10;
p1 --, p2 --;
if (p1 == 0 || p2 == 0) break;
}
if (p1 == 0 && p2 == 0) {
if (l!=0) {
char m = (char)48+l;
c += m;
}
}
else if (p1 != 0 && p2 == 0) {
for (int i = p1; i>0; i--) {
int k = (int)a[i-1]-48+l;
char m = (char)48+k%10;
c += m;
l = k/10;
}
if (l!=0) {
char m = (char)48+l;
c += m;
}
}
else if (p1 == 0 && p2 != 0) {
for (int i = p2; i>0; i--) {
int k = (int)b[i-1]-48+l;
char m = (char)48+k%10;
c += m;
l = k/10;
}
if (l!=0) {
char m = (char)48+l;
c += m;
}
}
string q;
for (int i = 0; i<c.size(); i++) q += c[c.size()-i-1];
return q;
} int main() {
string s, a;
a = "", s="0";
int flag = 0;
//cout <<add("9345", "900") <<endl;
//cout << s << endl;
while (cin >> a) {
if (a[0] == '0') break; s = add(s, a);
// cout << s <<endl;
}
cout <<s <<endl;
return 0;
}

UVA424高精度加法的更多相关文章

  1. NEFU 2016省赛演练一 F题 (高精度加法)

    Function1 Problem:F Time Limit:1000ms Memory Limit:65535K Description You know that huicpc0838 has b ...

  2. java算法 蓝桥杯 高精度加法

    问题描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩展了,比如用数组或 ...

  3. 用c++实现高精度加法

    c++实习高精度加法 最近遇到一个c++实现高精度加法的问题,高精度问题往往十复杂但发现其中的规律后发现并没有那么复杂,这里我实现了一个整数的高精度加法,主要需要注意以下几点: 1:将所需输入的数据以 ...

  4. 高精度加法——经典题 洛谷p1601

    题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...

  5. 高精度加法--C++

    高精度加法--C++ 仿照竖式加法,在第一步计算的时候将进位保留,第一步计算完再处理进位.(见代码注释) 和乘法是类似的. #include <iostream> #include < ...

  6. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  8. leetcode 67. Add Binary (高精度加法)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. leetcode 66. Plus One(高精度加法)

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

随机推荐

  1. iOS SDAutoLayout图文混排-共享

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #526eda } span.s1 { color: #4dbf5 ...

  2. 如何高效撤销Git管理的文件在各种状态下的更改

    一.背景 企业中我们一般采用分布式版本管理工具git来进行版本管理,在团队协作的过程中,我们难免会遇到误操作,需要撤销更改的情况,那么我们怎么高效的进行撤销修改呢?对于还未提交到暂存区的代码怎么高效撤 ...

  3. React Native绑定微信分享/登录/支付(演示+实现步骤+注意事项)

    React Native(以下简称RN)绑定微信分享/微信登录/微信支付的实现演示+源码+注意事项!微信的调用大同小异,本文实现了微信的分享功能,其他功能可以在链接文档里面找到具体的方法. 本文分文三 ...

  4. CSS篇(上)

    紧接着HTML篇的CSS篇开啦,老铁们快来围观... 1.介绍一下标准的CSS盒子模型?低版本IE的盒子模型有什么不同? 1>有两种:IE盒子模型      W3C盒子模型 2>盒模型:内 ...

  5. .net中如何实现多线程

    l线程肯定也是要执行一段代码的.所以要产生一个线程,必须先为该线程写一个方法,这个方法中的代码就是该线程运行所要执行的代码.(找个人来做一件事情) l线程启动时,通过委托调用该方法. (委托的好处) ...

  6. Visual Representation of SQL Joins

    原文:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins   从视图上介绍了7种不同类型的JOIN ...

  7. Effective Java 第三版——15. 使类和成员的可访问性最小化

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. 接口返回数据Json格式处理

    有这样一个页面 , 用来显示用户的账户记录数据,并且需要显示每个月的 收入 支出合计 ,在分页的时候涉及到一些问题,需要对返回的Json格式做处理,处理起来比较麻烦,后端返回的Json数据格式形式如下 ...

  9. C#调用windows api 实现打印机控制

    using System; using System.Text; using System.Runtime.InteropServices; using System.Security; using ...

  10. sql server中的分页数据查询

    1.引言 今天在工作中遇到一个需要进行sql server分页数据查询的问题,但是分页数据查询的sql却忘记了,最终通过查询资料解决了该问题.现在把解决方法记下,以备查阅. 在这里需要感谢博客园的Ql ...