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. 函数PYXX_READ_PAYROLL_RESULT的dump问题

    发现有两个HR的后台定时任务出现dump,日志表示,是PYXX_READ_PAYROLL_RESULT产生了类型冲突的异常CX_SY_DYN_CALL_ILLEGAL_TYPE. 日志标题部分: 类别 ...

  2. scala写算法-快排

    快排算法很经典,今天用scala的函数式思维来整理一下并实现: def qsort(list: List[Int]):List[Int]=list match { case Nil=>Nil c ...

  3. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

  4. headfirst设计模式(4)—工厂模式

    开篇 天天逛博客园,就是狠不下心来写篇博客,忙是一方面,但是说忙能有多忙呢,都有时间逛博客园,写篇博客的时间都没有?(这还真不好说) 每次想到写一篇新的设计模式,我总会问自己: 1,自己理解了吗? 2 ...

  5. CAS在Java类中的应用

    CAS 这个指令全称 compare and swap 即比较替换指令,在现代处理器新加入的指令.指导思想:基于乐观锁机制.比较一个变量在内存值中的值和变量的当前值(旧值).如果相等,则认为该变量没有 ...

  6. readAsDataURL(file) & readAsText(file, encoding)

      readAsDataURL(file)会把文件内容转换为data类型的URL: data:text/plain;base64,b3JkZXItaWQJb3JkZXItaXRlbS1p... 这种d ...

  7. 【转】nginx提示:500 Internal Server Error错误的解决方法

    本文转自:http://www.jb51.net/article/35675.htm 现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和 ...

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

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

  9. SQL奇技淫巧

    1.SQL行列转换 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94想变成(得到如下结果): 姓名 ...

  10. 关于md5的使用方法

    本周工作,学习中用到了,md5. 在我们需要用到md5密码的时候,可以使用: System.Web.Security.FormsAuthentication.HashPasswordForStorin ...