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

时间:2017-05-10 12:40:27

标签: java arrays string int

我已嵌套for循环:

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

任何人都可以帮助我。提前致谢

2 个答案:

答案 0 :(得分:0)

你可以使用split with reverse String并循环抛出你的数组:

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. 在结果中添加添加内容。
  6. 修改

    Lets back to school

    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);
    

答案 1 :(得分:0)

试试这些代码

$("#btnProbar").click(function (e) {

    var TXT_URL = "http://localhost:44467/xml/monitor.xml";
    $.ajax({
        crossDomain: true,
        type: "GET",
        url: TXT_URL,
        dataType: "XML",
        success: function (xml) {
            $(xml).find('OBX').each(function () {
                var hl7_cabecera = $(this).find("OBX.3").text();
                var hl7_valor = $(this).find('OBX.5');
                var hl7_metrica = $(this).find('OBX.6');

                console.log(hl7_cabecera + ": "+hl7_valor +" " +hl7_metrica);
            });
        },
        error: function() {                                               
        alert("The XML File could not be processed correctly.");
    }
    });

});