大数java

一、大明A+B

话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。  

Input

本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。

Output

请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。

Sample Input

1.1 2.9
1.1111111111 2.3444323343
1 1.1

Sample Output

4
3.4555434454
2.1
import java.util.Scanner;
import java.math.BigDecimal;
public class Main
{
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		BigDecimal a,b;//定义浮点类型的大数;
		while(cin.hasNext())
		{
		    a = cin.nextBigDecimal();
		    b = cin.nextBigDecimal();
		    System.out.println(a.add(b).stripTrailingZeros().toPlainString());
                   //stripTrailingZeros() //去掉小数点后的末尾0;
                   //toPlainString() //转为普遍计数法输出;(例如从科学记数法表示的转化为普通计数)
		}
	}
}

二、A + B Problem Too

This problem is also a A + B problem,but it has a little difference,you should determine does (a+b) could be divided with 86.For example ,if (A+B)=98,you should output no for result.

Input

Each line will contain two integers A and B. Process to end of file.

Output

For each case, if(A+B)%86=0,output yes in one line,else output no in one line.

Sample Input

1 1
8600 8600

Sample Output

no
yes
import java.util.Scanner;
import java.math.BigInteger;
public class Main
{

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		BigInteger a, b, d, e;//定义整形类的大数;
		while(cin.hasNext())
		{
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			BigInteger c = a.add(b);//c=a+b;
			d=BigInteger.valueOf(86);//赋值;
			e=BigInteger.valueOf(0);
			if(c.remainder(d)==e)//如果c%d==e;
			{
				System.out.println("yes");
			}
			else
				System.out.println("no");
		}
	}

}

三、ACMer

There are at least P% and at most Q% students of HDU are ACMers, now I want to know how many students HDU have at least?

Input

The input contains multiple test cases.
The first line has one integer,represent the number of test cases.
The following N lines each line contains two numbers P and Q(P < Q),which accurate up to 2 decimal places.

Output

For each test case, output the minumal number of students in HDU.

Sample Input

1
13.00 14.10

Sample Output

15

一个超时的程序...

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Main {

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		BigDecimal a, b, d, f, e, c, x, flag1, flag2;
		int T = cin.nextInt();
		for(int i=0; i<T; i++)
		{
			a = cin.nextBigDecimal();
			b = cin.nextBigDecimal();
			d = BigDecimal.valueOf(1);
			x = BigDecimal.valueOf(100);
			BigDecimal m = new BigDecimal(1);
			BigDecimal n = new BigDecimal(0.2);
			for(c = d; ;)
			{
				c=c.add(d);
				e = (c.multiply(a)).divide(x);
				f = (c.multiply(b)).divide(x);
				flag1 = e.setScale(0,BigDecimal.ROUND_UP);//最小比率向上取整;
				flag2 = f.setScale(0,BigDecimal.ROUND_DOWN);//最大比率向下取整;
				if(flag1==flag2)//当最小比率向上取整与最大比率向下取整相等时,为最少人数;
				{
					System.out.println(c);
					break;
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42569807/article/details/88854742