hdu1047 java大整数相加

本文详细解析了HDOJ1047问题的求解思路,包括输入处理、BigInteger类的应用、以及输出格式的特殊要求。重点介绍了如何使用Scanner类读取输入,BigInteger类进行数值操作,并处理特殊的输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 import java.util.*;
 2 import java.io.*;
 3 import java.math.BigInteger;
 4 public class Main {
 5     public static void main(String[] args)
 6     {
 7         Scanner in=new Scanner(System.in);
 8         int n=in.nextInt();
 9         while (n-->0)
10         {
11             BigInteger ans=new BigInteger("0");
12             while (in.hasNext())
13             {
14                 BigInteger tmp=new BigInteger("0");
15                 tmp=in.nextBigInteger();
16                 if (!tmp.equals(BigInteger.valueOf(0)))
17                     ans=ans.add(tmp);
18                 else
19                 {
20                     System.out.println(ans);
21                     if (n!=0) System.out.println("");
22                     break;
23                 }
24             }
25         }
26     }
27 }

-------------多个整数和

1.n-->0

2.equals判断相等

3.区别直接new BigInteger和BigInteger.valueOf

4.输出回车

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047

转载于:https://www.cnblogs.com/xiao-xin/articles/4192756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值