uva 465 Overflow

本文介绍使用Java解决大数运算的问题,通过引入BigDecimal类进行高精度计算,并处理输入数据的有效性和运算结果的合理性。

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

又是一道大数的题目,可以直接用Java解决,用C++处理起来比较麻烦

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		Scanner str_scan;
		String s = "";
		String temp;
		BigDecimal num1, num2, max, res;
		max = new BigDecimal(Integer.MAX_VALUE);
		String oper;
		while(scanner.hasNextLine())
		{
			s = scanner.nextLine();
			str_scan = new Scanner(s);
			
			temp = str_scan.next();
			num1 = new BigDecimal(temp);
			oper = str_scan.next();
			temp = str_scan.next();
			num2 = new BigDecimal(temp);
			
			/*
			System.out.println("num="+num1);
			System.out.println("oper:"+oper);
			System.out.println("num2="+num2);
			*/
			
			System.out.println(s);
			if(num1.compareTo(max) > 0)
			{
				System.out.println("first number too big");
			}
			if(num2.compareTo(max) > 0)
			{
				System.out.println("second number too big");
			}
			
			if(oper.compareTo("+") == 0)
			{
				res = num1.add(num2);
			}
			else
			{
				res = num1.multiply(num2);
			}
			
			if(res.compareTo(max) > 0)
			{
				System.out.println("result too big");
			}
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值