大数

本文介绍了一种使用Java大数类BigInteger进行复杂数学运算的方法,特别是在处理超大整数运算的问题上。通过两个实例,一是计算组合数解决多组输入问题,二是判断大数是否为完全平方数来决定电子竞技游戏的选择,展示了BigInteger的强大功能。文章还提供了代码示例,包括大数的四则运算、比较、取绝对值、幂运算以及进制转换等。

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

  http://acm.hdu.edu.cn/showproblem.php?pid=1261

import java.util.Scanner;
import java.math.BigInteger;
class Main
{
	public static void main(String[] args)
	{
		Scanner cin=new Scanner(System.in);
		int n,s;
		int[] num=new int[30];
		BigInteger a,b;
		while(cin.hasNext()){     //实现多组输入
			n=cin.nextInt();
			if(n==0) break;
			s=0;
			for(int i=0;i<n;i++){
				num[i]=cin.nextInt();
				s+=num[i];
				}
			//System.out.println(s);
			a=BigInteger.valueOf(1);
			for(int i=2;i<=s;i++)
				a=a.multiply(BigInteger.valueOf(i));
			for(int i=0;i<n;i++){
				b=BigInteger.valueOf(1);
				for(int j=2;j<=num[i];j++)
					b=b.multiply(BigInteger.valueOf(j));
				a=a.divide(b);
			}
			System.out.println(a);
		}
	}

}
//java 大数判断an和sn-1是否是完全平方数
import java.util.Scanner;
import java.math.BigInteger;
class Main
{
    public static boolean iss(BigInteger n)
    {
        BigInteger l=BigInteger.ZERO;
        BigInteger r=n;
        while(l.compareTo(r)<=0)
        {
            BigInteger mid=l.add(r).shiftRight(1);    //二分法
            BigInteger s=mid.multiply(mid);
            int ans;
            ans=s.compareTo(n);
            if(ans==0)
                return true;
            else if(ans>0)
                r=mid.subtract(BigInteger.ONE);
            else
                l=mid.add(BigInteger.ONE);
                
        }
        return false;
    }
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        while(t--!=0)
        {
            BigInteger a=cin.nextBigInteger();
            BigInteger b=a.multiply(a.subtract(BigInteger.ONE)).shiftRight(1);
            Boolean n=iss(a);
            Boolean m=iss(b);
            if(n&&m) 
                System.out.println("Arena of Valor");
            else if(n&&!m)
                System.out.println("Hearth Stone");
            else if(!n&&m)
                System.out.println("Clash Royale");
            else
                System.out.println("League of Legends");
        }
    }
}

 

问题 J: Participate in E-sports

时间限制: 2 Sec  内存限制: 128 MB
提交: 177  解决: 48
[提交] [状态] [命题人:admin]

题目描述

Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know which one to choose, so they use a way to make decisions.
They have several boxes of candies, and there are i candies in the i^th box, each candy is wrapped in a piece of candy paper. Jessie opens the candy boxes in turn from the first box. Every time a box is opened, Jessie will take out all the candies inside, finish it, and hand all the candy papers to Justin.
When Jessie takes out the candies in the N^th box and hasn't eaten yet, if the amount of candies in Jessie's hand and the amount of candy papers in Justin's hand are both perfect square numbers, they will choose Arena of Valor. If only the amount of candies in Jessie's hand is a perfect square number, they will choose Hearth Stone. If only the amount of candy papers in Justin's hand is a perfect square number, they will choose Clash Royale. Otherwise they will choose League of Legends.
Now tell you the value of N, please judge which game they will choose.

 

 

输入

The first line contains an integer T(1≤T≤800), which is the number of test cases. 
Each test case contains one line with a single integer: N(1≤N≤10^200).

 

 

输出

For each test case, output one line containing the answer.

 

样例输入

复制样例数据

4
1
2
3
4

样例输出

Arena of Valor
Clash Royale
League of Legends
Hearth Stone

 大数的基本运算  【转载至https://www.cnblogs.com/qie-wei/p/10160143.html

import java.math.BigInteger;
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		BigInteger a,b;
		while(in.hasNext())
		{
			a = in.nextBigInteger();
			b = in.nextBigInteger();
			//四则运算
			System.out.println(a.add(b));
			System.out.println(a.subtract(b));
			System.out.println(a.multiply(b));
			System.out.println(a.divide(b));
			System.out.println(a.remainder(b));
			//大数比较
			if(a.compareTo(b)==0)
				System.out.println("a==b");
			else if(a.compareTo(b)>0) 
				System.out.println("a>b");
			else {
				System.out.println("a<b");
			}
			//绝对值
			System.out.println(a.abs());
			//幂
			int fun = 10;
			System.out.println(a.pow(fun));
			//大数x进制的字符串表示
			int x = 8;
			System.out.println(a.toString(x));
			//大数返回十进制的字符串表示
			System.out.println(a.toString());
		}
	}
}

toPlainString() //转为普遍计数法输出

//给一个字符串1.238761976E-10
//如何得到0.0000000001238761976这个字符串呢?
BigDecimal bd = new BigDecimal("1.238761976E-10");  
System.out.println(bd.toPlainString());

stripTrailingZeros() //去掉末尾0

//给一个字符串1245600.000
//如何得到1245600这个字符串?
BigDecimal bd=new BigDecimal("1245600.000");
System.out.println(bd.stripTrailingZeros());

hdu1063

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

public class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        while(in.hasNext())
        {
            BigDecimal ans = BigDecimal.valueOf(1);
            BigDecimal a = in.nextBigDecimal();
            Integer n = in.nextInt();
            while(n-->0)
                ans = ans.multiply(a);
                String str = ans.stripTrailingZeros().toPlainString().toString();
                if(str.charAt(0)=='0')
                    str = str.substring(1);
                System.out.println(str);    
        }
        in.close();
    }
}

hdu 1753

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

public class test1 {
	public static void main(String[] args) {
	
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			BigDecimal a = in.nextBigDecimal();
			BigDecimal b = in.nextBigDecimal();
				a=a.add(b);
				String str = a.stripTrailingZeros().toPlainString().toString();
				if(str.charAt(0)=='0')
					str = str.substring(1);
				System.out.println(str);	
		}
		in.close();
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值