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. C++ 头文件系列(iostream)

    1. 简介 这个头文件非常特殊,它只声明了8个常用流对象. 2. 8个对象 2.1 窄字符对象(char) extern istream cin extern ostream cout extern ...

  2. android中Log类的封装

    1.为了方便的使用Log打印日志,以及后续方便撤销日志打印,所以对Log类进行封装是一件好事. package market.phone; import android.util.Log; /** * ...

  3. Jrebel简单的热部署一个web工程

    前言:博主最近在做Hybris开发,漫长的启动时间大大的拖累了项目的进度,而Jrebel的出现就是为了减少项目重启的时间或者说修改了代码后直接不用重启就可以看到修改的结果,但是Hybris的部署一直没 ...

  4. mentohust 使用

    可使用帮助命令 mentohust -h 或着查看 https://wenku.baidu.com/view/95c08019ff00bed5b9f31d1a.html

  5. Sum of AP series——AP系列之和

    A series with same common difference is known as arithmetic series. The first term of series is 'a' ...

  6. TXDragon的大火题合集

    还有三个题没写,弃疗了 Solution

  7. php按照中文首字母排序

    1> 网络上很多php的工具类可以将汉字转为拼音: 2> 将拼音进行排序即可 另一种则是类似mysql转码方式: 1 foreach ($array as $key=>$value) ...

  8. linux系统下phpstudy里的mysql使用方法

    linux作为一个优秀的服务器端管理系统,其实linux的桌面系统也用起来十分的nice.好吧,如何你在做开发的时候在linux下安装了lmap或者phpstudy,那么在第一次使用其自带的mysql ...

  9. tomcat 设置jvm 参数

    在catalina.bat中设置 正确的做法是设置成这样set JAVA_OPTS=%JAVA_OPTS% -Xms256m -Xmx256m,避免JAVA_OPTS参数覆盖

  10. KandQ:单例模式的七种写法及其相关问题解析

    设计模式中的单例模式可以有7种写法,这7种写法有各自的优点和缺点: 代码示例(java)及其分析如下: 一.懒汉式 public class Singleton { private static Si ...