高精度运算------C++

(加减乘除)

例:ZOJ2001  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1001

The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.

Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.

Sample Input

3
24 1
4358 754
305 794

Sample Output

34
1998
1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
const int maxn=1005;
using namespace std; int main()
{
int n;
//freopen("Atext.in","r",stdin);
cin >> n;
while(n--){
string a,b;
int flag=0,n,m,len,i;
int num1[maxn]={0},num2[maxn]={0},ans[maxn]={0};
cin >> a >> b;
n=a.size();
for(int i=0;i<n;i++){
num1[i]=a[i]-'0';
}
m=b.size();
for(int i=0;i<m;i++){
num2[i]=b[i]-'0';
}
len=n>m? n:m;
for(i=0;i<len;i++){
ans[i]+=num1[i]+num2[i]; //反向数之和
if(ans[i]>9){
ans[i]-=10;
ans[i+1]++;
}
}
/*for(int i=0;i<len/2;i++){
swap(ans[i],ans[len-i-1]); //将和逆序
}*/
if(ans[len]!=0)len++;
for(i=0;i<len;i++){
if(ans[i]!=0||flag==1){
cout << ans[i];
flag=1;
}
}
cout << endl;
}
return 0;
}

高精度------C++的更多相关文章

  1. CSharpGL(28)得到高精度可定制字形贴图的极简方法

    CSharpGL(28)得到高精度可定制字形贴图的极简方法 回顾 以前我用SharpFont实现了解析TTF文件从而获取字形贴图的功能,并最终实现了用OpenGL渲染文字. 使用SharpFont,美 ...

  2. 递推+高精度 UVA 10497 Sweet Child Makes Trouble(可爱的孩子惹麻烦)

    题目链接 题意: n个物品全部乱序排列(都不在原来的位置)的方案数. 思路: dp[i]表示i个物品都乱序排序的方案数,所以状态转移方程.考虑i-1个物品乱序,放入第i个物品一定要和i-1个的其中一个 ...

  3. [Template]高精度模板

    重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...

  4. Code[VS] 3123 高精度练习之超大整数乘法

    FFT 做 高精度乘法 #include <bits/stdc++.h> ); struct complex { double a, b; inline complex( , ) { a ...

  5. Java 高精度数字

    BigInteger // 高精度整数 BigDecimal //高精度小数  小数位数不受限制

  6. c++减法高精度算法

    c++高精度算法,对于新手来说还是一大挑战,只要克服它,你就开启了编程的新篇章,算法. 我发的这个代码并不是很好,占用内存很多而且运行时间很长(不超过0.02秒),但是很好理解,很适合新手 高精算法的 ...

  7. c++加法高精度算法

    c++高精度算法,对于新手来说还是一大挑战,只要克服它,你就开启了编程的新篇章,算法. 我发的这个代码并不是很好,占用内存很多而且运行时间很长(不超过1秒),但是很好理解,很适合新手 高精算法的本质就 ...

  8. [转]使用Stopwatch类实现高精度计时

    对一段代码计时同查通常有三种方法.最简单就是用DateTime.Now来进行比较了,不过其精度只有3.3毫秒,可以通过DllImport导入QueryPerformanceFrequency和Quer ...

  9. .net平台下获取高精度时间类

    原文:http://www.it118.org/specials/321869dd-98cb-431b-b6d2-82d973cd739d/d940a15c-0d36-4369-8de0-b07cf3 ...

  10. [vijos P1040] 高精度乘法

    如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...

随机推荐

  1. MFS分布式存储特性及组件说明

    1.MFS MooseFS是一个具有冗余容错功能的分布式网络文件系统,它将数据分别存放在多个物理服务器或单独磁盘或分区上,确保一份数据有多个备份副本,然而对于访问MFS的客户端或者用户来说,整个分布式 ...

  2. PointGNN未修改之前实验结果 ---car

    10个epoch中1-4:

  3. 还不知道如何在java中终止一个线程?快来,一文给你揭秘

    目录 简介 Thread.stop被禁用之谜 怎么才能安全? 捕获异常之后的处理 总结 简介 工作中我们经常会用到线程,一般情况下我们让线程执行就完事了,那么你们有没有想过如何去终止一个正在运行的线程 ...

  4. 一篇博客带你学会MyBatis

    概念 MyBatis是一款持久层框架,用于简化JDBC操作(JavaEE三层架构:表现层(用来做页面的代码),业务层(业务逻辑代码),持久层(对数据库操作的代码))(框架就是一个半成品软件,是一套可重 ...

  5. Java并发小结01

    Java并发小结01 主要参考自<实战Java高并发程序设计>. 需要知道的概念 - 同步与异步 - 并发与并行 - 临界区 - 阻塞与非阻塞 - 死锁.饥饿.活锁 同步与异步 同步:同步 ...

  6. 你可能需要的 6 个 React 开发小技巧

    ​ 这是一个可怕的问题,在React中,我们经常会编写条件语句来显示不同的视图,比如这个简单的例子. const App = () => { return ( <> { loadin ...

  7. Java面试——Nginx

    一. 二.Nginx 的优点 [1]速度更快:这表现在两个方面:一方面,在正常情况下,单次请求会得到更快的响应:另一方面,在高峰期(如有数以万计的并发请求),Nginx 可以比其他 Web服务器更快地 ...

  8. Java面试——JVM知识

    一.什么情况下会发生栈内存溢出 [1]线程请求的栈深度大于虚拟机所允许的深度,将抛出 StackOverflowError 异常.递归的调用一个简单的方法,不断累积就会抛出 StackOverflow ...

  9. new 的原理是什么?通过 new 的方式创建对象和通过字面量 创建有什么区别?

    涉及面试题: new 的原理是什么?通过 new 的方式创建对象和通过字面量 创建有什么区别? 在调用 new 的过程中会发生四件事情 新生成了一个对象: 链接到原型: 绑定 this : 返回新对象 ...

  10. 面对AI的兴起,从人类发展到个人发展,普通人应当如何抉择?

    这一周被各种 AI 卷的不行,从 ChatGPT 4.0 上线到百度文心一言发布会,再到微软的 Microsoft 365 Copilot. 网上有很多人.公众号吐嘈百度,而晓衡接触到的圈子还有一些不 ...