高精度加法
来源:互联网 发布:微信小程序瀑布流源码 编辑:程序博客网 时间:2024/05/16 08:20
<span style="font-size:18px;">/* * * 输入两个整数a和b,输出这两个整数的和。a和b都不超过100位。 */import java.util.Scanner;public class Main {private static Scanner sc;public static void main(String[] args) {sc = new Scanner(System.in);String str1 = sc.next(); //存储加数String str2 = sc.next(); //存储被加数int r = 0; //保存进位int n = 0; //结果数组的长度//因为在运算过程中可能存在进位,故此把n设置成比这两个数中位数大的还要大1if (str1.length() > str2.length())n = str1.length() + 1;elsen = str2.length() + 1;char[] a = str1.toCharArray();char[] b = str2.toCharArray();int[] a1 = new int[n];int[] b1 = new int[n];int[] c = new int[n];//把字符数组转换成int型的,并且按个十百千万.....这样的顺序存储for (int i = 0; i < str1.length(); i++) {a1[i] = (int) a[str1.length() - i - 1] - 48;}for (int i = 0; i < str2.length(); i++) {b1[i] = (int) b[str2.length() - i - 1] - 48;}//计算,并将结果保存到c数组中for (int i = 0; i < n; i++) {int t = a1[i] + b1[i] + r;r = t / 10;c[i] = t % 10;}//检测最高位是否为0,如果是0,则不输出最高位,否则,从后向前遍历整个数组if (c[n-1] == 0) {for (int i = n - 2; i >= 0; i--) {System.out.print(c[i]);}} else {for (int i = n-1 ; i >= 0; i--) {System.out.print(c[i]);}}}}</span>//由于本人能力有限,所写程序不够完美,望各位大神指点一二,小弟不胜感激。
0 0
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度 加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- Mysql如何查询连续的时间次数
- Legacy Animation Can’t Animate Material Property
- MYSQL添加远程用户或允许远程访问三种方法
- hdoj Moving Tables 1050 (DP) 坑!坑!坑!
- usb can广成牌品牌提供专业的usb can分析仪
- 高精度加法
- 实战解析Android架构设计原则
- android QQ 登录
- 二插树相关知识
- C# Keywords
- RenderTexture 不能正常显示
- codeforces 597A
- 年终排名战白热化 领跑基金高招迭出
- Ubuntu下卸载mysql