POJ 1503 Integer Inquiry(大数相加)

本文探讨了处理大数运算的挑战,特别是在计算机科学竞赛中遇到的问题。通过对比不同的编程语言,展示了Java如何利用BigInteger类简化大数相加的操作。同时,提供了两种实现方式的代码示例,一种使用BigDecimal类,另一种采用自定义数组实现加法,后者在调试过程中遇到了多次错误,突出了理解数据类型重要性。
ComfyUI

ComfyUI是一款易于上手的工作流设计工具,具有以下特点:基于工作流节点设计,可视化工作流搭建,快速切换工作流,对显存占用小,速度快,支持多种插件,如ADetailer、Controlnet和AnimateDIFF等

一、Description

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 VeryLongInteger. 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.
二、题解
        这个题和之前2602有 Poj 2602 Superlong sums(大数相加) 异曲同工。但还是有要注意的部分,就是这里有前导的0,而且题目没说是否每个数的位数是否相同。还有数组的长度也比题目说明的要大,所以开数组的时候千万不要手下留情啊。这个题目我WA了还多次,可能是考虑的情况不够全面,对题目的要求没有完全弄明白。所以,虽然经过了几次的调试,几组测试数据都过了,但是还是没有AC。请大神指教。但当我一头雾水的时候呢,居然发现java有一种脑残解法,由于java 中自带的 BigInteger 类,这是个不可变的任意精度的整数。而且接受十进制的字符串,并能将 BigInteger 的十进制字符串表示形式转换为 BigInteger。所以,管它什么大数,都弱爆了。但是看来好东西虽然存在,但是了解它的原理还是必要的。
三、java代码
import java.util.*;   
import java.math.*;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
        BigDecimal bd1 = BigDecimal.valueOf(0);   
        BigDecimal bd2 = BigDecimal.valueOf(0);   
        String str;   
           
        while(cin.hasNext()){   
            str = cin.nextLine();   
            if(str.equals("0"))   
                break;   
            else{   
                bd2 = new BigDecimal(str);   
                bd1 = bd1.add(bd2);   
            }   
        }   
           
        System.out.println(bd1.toPlainString());   
    }   
}  

求错误代码!!
import java.io.IOException;
import java.util.Scanner;

  public class Main {
	  static byte[] c = new byte[1000];  
      public static void add(String s1){
    	  int i;
          for (i = 0; i <s1.length(); i++){  
              c[i] += s1.charAt(i) - 48;  
          }  
          int cf = 0;  
          for (i = s1.length() - 1; i >= 0; i--) {  
              c[i] += cf;  
              cf=c[i]/10;  
              c[i]=(byte) (c[i] %10);  
          }  
      }
	  public static void main(String[] args) throws IOException {     
       Scanner cin = new Scanner(System.in);
       String s[]=new String[120];
       int i=0,j=0,k;
       String ss,te;
       String te2;
       int max=0;
       while(!(ss=cin.next()).equals("0")){
    	   s[i]=ss;
    	   max=Math.max(s[i].length(), max);
    	   i++;
       }
       for(k=0;k<i;k++){
    	   te=s[k];
    	   te2 ="";
    	   while(s[k].length()<max){
    		   s[k]+="0";
    		   te2+="0";
    	   }
    	   s[k]=te2+te;
    	   add(s[k]);
       }
       while(c[j]==0){
	    	j++;
	    }
       for(k=j;k<max;k++){
    	    System.out.print(c[k]);
       }
       System.out.println();
    }  
 }     
  
  
  
  
  
  
  


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/AndyDai/p/4734147.html

您可能感兴趣的与本文相关的镜像

ComfyUI

ComfyUI

AI应用
ComfyUI

ComfyUI是一款易于上手的工作流设计工具,具有以下特点:基于工作流节点设计,可视化工作流搭建,快速切换工作流,对显存占用小,速度快,支持多种插件,如ADetailer、Controlnet和AnimateDIFF等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值