Java-以传统方式总结大整数

技术标签: 爪哇  数组  细绳  int

我已经嵌套了循环:

String s1 = "4412";
String s2 = "0123";

int k = 0, l = 0, i3 = 0;

for (int i = s1.length() - 1; i < s1.length(); i--) {

    for (int j = s2.length() - 1; j <= i; j--) {

        k = Integer.parseInt(Character.toString(s1.charAt(i)));
        l = Integer.parseInt(Character.toString(s2.charAt(j)));

        i3 = k + l;

        System.out.println(i3);

    }
}

当我执行此程序时,我将获得234作为输出。因为它在S1和S2中获得了最后一个元素值。S2继续这样重复循环:

1
321
2
321
.
.

因此2+3,2+2,2+1像这样。但是我期望的是4412+123 = 4535

谁能帮我吗。提前致谢

看答案

您使用反向字符串使用拆分,然后循环像这样抛出阵列:

String s1 = "4421";
String s2 = "321";
//reverse and split your string
String[] spl1 = new StringBuilder(s1).reverse().toString().split("");//[1,2,4,4]
String[] spl2 = new StringBuilder(s2).reverse().toString().split("");//[1,2,4,4]
String result = "";
int max = spl1.length > spl2.length ? spl1.length : spl2.length;
for (int i = 0; i < max; i++) {
    int k = spl1.length <= i ? 0 : Integer.parseInt(spl1[i]);
    int l = spl2.length <= i ? 0 : Integer.parseInt(spl2[i]);
    result += (k + l) + "";

}
System.out.println(result);//result 2474

这个想法是:

  1. 反向您的字符串12345-&gt;54321
  2. 分开弦[5,4,3,2,1]
  3. 在阵列之间找到最大
  4. 循环抛出您的数组并进行添加,如果输入不存在,则使用0, spl1.length <= i ? 0 : Integer.parseInt(spl1[i]);
  5. 添加到您的结果中。

编辑

让我们回到学校 :

Addition

您可以改用这个想法很简单:

String s1 = "5768956788678907689076890076544765433564376543564";
String s2 = "657687986578905438732902587349320254893";
String[] spl1 = new StringBuilder(s1).reverse().toString().split("");
String[] spl2 = new StringBuilder(s2).reverse().toString().split("");
String result = "";
int max = spl1.length > spl2.length ? spl1.length : spl2.length;
int rest = 0;
int sum;
for (int i = 0; i < max; i++) {
    int k = spl1.length <= i ? 0 : Integer.parseInt(spl1[i]);
    int l = spl2.length <= i ? 0 : Integer.parseInt(spl2[i]);

    sum = k + l + rest;
    if (sum > 9) {
        rest = 1;
        sum = sum - 10;
    } else {
        rest = 0;
    }
    result = (i + 1 == max ? sum + rest * 10 : sum) + result;

}
System.out.println(result);

智能推荐

ADSL共享上网方式大总结

为什么80%的码农都做不了架构师?>>>    3.邻里特殊组网方案 http://www.it.com.cn/f/network/052/28/81599_2.htm 邻里共享一条ADSL线路的人很多,虽然就是“双机共享上网”,但它有着特殊性,就是在Internet接入计算机关闭后(例如主人不在家,或要关闭计算机时),另一个用户就无法...

idea以utf-8方式启java

右上角 弹出对话框中找到你的启动方式 VM option中添加-Dfile.encoding=UTF-8字段,然后点击确定。 附:idea修改编码方式File->settings->Editor->File Encodings修改下面的各种编码 另外,如果发现输出还是乱码,看一下右下角编码是否正确...

在Ubuntu上以服务方式运行Java程序

女主宣言 最近小编在项目中遇到了一个问题,打包的jar文件需要作为服务来使用,但是如何才能保证让jar文件开机自启动呢。小编使用的是Ubuntu的系统,所以查阅了相关资料之后,整理了此篇文章,供大家参考使用。在本文中,我们将使用示例服务包装器,在Ubuntu系统中以服务的方式运行JAR文件。包括自动启动和日志提示。 PS:丰富的一线技术、多元化的表现形式,尽在“HULK一线技术杂谈&r...

以编程方式确定Java类的JDK编译版本

当需要确定使用哪个JDK版本来编译特定的Java .class文件时, 通常使用的方法是使用javap并在javap输出中查找列出的“主要版本”。 我在博客文章Autoboxing,Unboxing和NoSuchMethodError中引用了这种方法,但是在继续以编程方式实现此方法之前,请先在此进行详细说明。 以下代码段演示了如何对commons-configuration...

大整数加减法

大整数加法 大整数减法 本篇将以上两部分内容结合,使之能够真正计算大整数的加减法。 大整数加法介绍了两个正的大整数相加,大整数减法介绍了两个正的大整数相减。 加法 加法有以下情况,它们都可以转换为以上链接讲述的问题: 正 + 正 ,如 1 + 1 正 + 负,如 1 + (-1) = 1 - 1 负 + 正,如 -1 + 1 = 1 - 1 负 + 符,如 -1 + -1 = -(1 + 1) 减...

猜你喜欢

原型对象,原型链

函数都有prototype属性,它指向原型对象。 实例对象有__proto__属性,它指向对象原型 每一个原型对象都有constructor输赢,指向构造函数,每一个原型对象又具有__proto__属性,这个指向Object.prototype.在这里插入图片描述...

Node 调用 dubbo 服务的探索及实践

2.Dubbo简介 2.1 什么是dubbo Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。 2.2 流程图 Provider : 暴露服务的服务提供方。 Consumer : 调用远程服务的服务消费方。 Registry : 服务注册与发现的注册中心。 Monito...

mysql总结

mysql基础入门的总结     关于数据库:     数据库是软件开发人员要掌握的基本工具,软件的运行的过程就是操作数据的过程,数据库中的数据无非就是几个操作:增-删-查-改。         Mysql安装完成后,需要配置变量环境,找到配置路径path,然后把mysql安装目录bin文件导入就可以了。 然后运行cm...

adb及monkey常用命令

adb常用命令: 查看手机是否连接:adb devices   连接设备:adb connect 设备ip:端口号  若有连接多个设备需指明设备ip及端口号 安装APP:adb install [-r] 包名  -r表示覆盖安装,首次安装可省略 卸载APP:adb uninstall 包名 列出设备中所有应用包名:adb shell pm list packages ...

PC端浏览器如何设置无图模式

以谷歌浏览器为例,注意有些浏览器并不支持该功能。 1)打开自定义与控制 2)选择设置 3)查看左边状态栏,选择高级设置--》隐私设置和安全性 4)选择内容设置 5)图片 6)选择不显示任何图片,其中也可以只禁用某些网站图片,或者只开启自己想显示图片的网站...

问答精选

Correctly formatting GCM notifications?

I'm currently trying out the google cloud messaging service with its sample application "Guestbook." https://developers.google.com/cloud/samples/mbs/ I'm attempting to send notifications tha...

Are there any performance benefits of using Asynchronous functions over Synchronous in Node Js?

Now I came across an article that distinguishes between an Asynchronous function and Synchronous functions. From my understanding of the different examples and explanations, synchronous functions are ...

Python: Costing calculator output

Good day all I'm busy creating a small costing calculator for the signage department. I'm not getting the calculator to output the amount. Brief Description: You enter the height and width and then wh...

Flask-SQLAlchemy - model has no attribute 'foreign_keys'

I have 3 models created with Flask-SQLalchemy: User, Role, UserRole role.py: user.py: user_role.py: If I try (in the console) to get all users via User.query.all() I get AttributeError: 'NoneType' obj...

Seeding many PRNGs, then having to seed them again, what is a good quality approach?

I have many particles that follow an stochastic process in parallel. For each particle, there is a PRNG associated to it. The simulation must go through many repetitions to get average results. For ea...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答