高精度题目JAVA写法基础篇,妈妈再也不用担心我的高精度

UVA-424 - Integer Inquiry

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=365

没啥好说的,代码如下:

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String [] args){
		Scanner scan=new Scanner(System.in);
		BigInteger sum=BigInteger.valueOf(0);
		while(scan.hasNext())
		{
			BigInteger a=scan.nextBigInteger();
			if(a.equals(BigInteger.ZERO))
				break;
			else
			{
				sum=sum.add(a);
			}
		}
		System.out.println(sum);
	}
}
	

UVA-10106 - Product



没啥好说的,简单乘法,代码如下:

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String [] args){
		Scanner scan=new Scanner(System.in);
		while(scan.hasNext())
		{
			BigInteger a=scan.nextBigInteger();
			BigInteger b=scan.nextBigInteger();
			BigInteger sum=a.multiply(b);
			System.out.println(sum);
		}
	}
}
	

UVA-465 - Overflow

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=406

貌似看别人代码有更好的做法,俺这是比较笨的人的做法= =!

额,题目的坑在于输出的时候会有前导0!!!

代码如下:

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String [] args){
		Scanner scan=new Scanner(System.in);
		while(scan.hasNext())
		{
			String woca1=scan.next();
			BigInteger a=new BigInteger(woca1);
			String haha=scan.next();
			char hehe=haha.charAt(0);
			String woca2=scan.next();
			BigInteger b=new BigInteger(woca2);
			System.out.print(woca1);
			System.out.print(" "+hehe+" ");
			System.out.println(woca2);
			if(a.compareTo(BigInteger.valueOf(2147483647))>0)
				System.out.println("first number too big");
			if(b.compareTo(BigInteger.valueOf(2147483647))>0)
				System.out.println("second number too big");
			if(hehe=='+')
			{
				BigInteger ans=a.add(b);
				if(ans.compareTo(BigInteger.valueOf(2147483647))>0)
					System.out.println("result too big");
			}
			else if(hehe=='*')
			{
				BigInteger ans=a.multiply(b);
				if(ans.compareTo(BigInteger.valueOf(2147483647))>0)
				{
					System.out.println("result too big");
				}
			}
		}
	}
}
	

UVA-748 - Exponentiation

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=689

依稀记得还是POJ的1001。。。

没啥好说的,注意前导0.和后面无用的0

PS:JAVA太强大了= =!

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String [] args){
		Scanner scan=new Scanner(System.in);
		while(scan.hasNext())
		{
			BigDecimal a;
			a=scan.nextBigDecimal();
			int n;
			n=scan.nextInt();
			a=a.pow(n);
			a=a.stripTrailingZeros();
			String str=a.toPlainString();
			if(str.startsWith("0."))
				str=str.substring(1);
			System.out.println(str);
		}
	}
}


UVA-10494 - If We Were a Child Again

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1435

额。。。。依旧不懂为啥java没有读入char 的机制,要先读string才能读char!

代码如下:

import java.util.*;
import java.math.*;

public class Main{
	public static void main(String [] args){
		Scanner scan=new Scanner(System.in);
		while(scan.hasNext())
		{
			BigInteger a=scan.nextBigInteger();
			String temp=scan.next();
			//next()的用法要牢记,java按照空格读入就靠它了
			//查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息。
			char op=temp.charAt(0);
			BigInteger b=scan.nextBigInteger();
			//Java有趣的是dicideAndRemainder返回的是一个商加余数的数组,而非就是一个余数= =#
			BigInteger []c=a.divideAndRemainder(b);
			if(op=='/')
				System.out.println(a.divide(b));
			else if(op=='%')
				System.out.println(c[1]);
		}
	}
}
	



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值