现在的位置: 首页 > 综合 > 正文

[Java][hoj]Super Calculator

2018年03月17日 ⁄ 综合 ⁄ 共 1155字 ⁄ 字号 评论关闭
package jprac;

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

public class Main
{
	public static void main(String[] args)
	{
		BigInteger a,b;
		String op,sz1,sz2;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext())
		{
			//a = cin.nextBigInteger();
			//op = cin.next();
			//b = cin.nextBigInteger();
			sz1=cin.next();
			op=cin.next();
			sz2=cin.next();
			
			if(sz1.charAt(0)=='+')
				a=new BigInteger(sz1.substring(1));
			else
				a=new BigInteger(sz1);
			
			if(sz2.charAt(0)=='+')
				b=new BigInteger(sz2.substring(1));
			else
				b=new BigInteger(sz2);
			if(op.equals("+"))
				System.out.println(a.add(b));
			else if(op.equals("-"))
				System.out.println(a.subtract(b));
			else if(op.equals("*"))
				System.out.println(a.multiply(b));
			else if(op.equals("/"))
			{
				if(b.equals(BigInteger.ZERO)){
					System.out.println("Divided by zero.");
				}
				else{
					BigInteger res[] = a.divideAndRemainder(b);
					System.out.println(res[0] + " " + res[1]);
				}
			}
			else if(op.equals(">"))
			{ 
				if(a.compareTo(b)>0)
					System.out.println("true");
				else
					System.out.println("false");
			}
			else if(op.equals("<"))
			{
				if(a.compareTo(b)<0)
					System.out.println("true");
				else
					System.out.println("false");
			}
			else
			{
				if(a.equals(b))
					System.out.println("true");
				else
					System.out.println("false");
			}
		}
		
	}

}

熟悉了一下大数功能,但是这个new总让我心存疑虑。。。好吧,Java中不需要delete

为什么非要转成字符串呢。。。HOJ服务器上的环境版本较低。。。

抱歉!评论已关闭.