JAVA大数+常用函数

推荐:java中的BigInteger

test 1
题目描述:输入两个非常大的实数A和B,判断A是否等于B;

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

public class Main {
    public static void main(String[] args) {
        BigDecimal a, b;
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) { //相当于 !=EOF
            a = cin.nextBigDecimal();
            b = cin.nextBigDecimal();
            if(a.compareTo(b) != 0) System.out.println("NO"); //大数比较函数
            else System.out.println("YES");
        }
    }
}

test 2
题目描述:大数求和;

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

public class Main {
	public static void main(String[] args) {
		BigInteger a, b;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			System.out.println(a.add(b)); //相加函数
		}
	}
}

test 3
题目描述:大数阶乘;

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

public class Main {
	public static void main(String[] args) {
		int a, size = 5100;
		BigInteger f[] = new BigInteger[size];
		f[1] = BigInteger.valueOf(1);
		for(int i = 2; i < size; ++i) {
			f[i] = f[i - 1].multiply(BigInteger.valueOf(i)); //强制类型转换
		}
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			a = cin.nextInt();
			System.out.println(f[a]);
		}
	}
}

test 4
题意描述:斐波那契大数;

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

public class Main {
	public static void main(String[] args) {
		int T, a, size = 1010;
		BigInteger f[] = new BigInteger[size];
		f[1] = f[2] = BigInteger.valueOf(1);
		for(int i = 3; i < size; ++i) {
			f[i] = f[i - 1].add(f[i - 2]);
		}
		Scanner cin = new Scanner(System.in);
		T = cin.nextInt();
		while(T-- > 0) {
			a = cin.nextInt();
			System.out.println(f[a]);
		}
	}
}

test 5
题意描述:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1);

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

import org.omg.Messaging.SyncScopeHelper;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int T = cin.nextInt();
		while(T-- > 0) {
			BigInteger n, A, B, MOD = new BigInteger("9973");
			n = cin.nextBigInteger();
			B = cin.nextBigInteger();
			for(int i = 0; ; ++i) {
				BigInteger ans = B.multiply(BigInteger.valueOf(i));
				if(ans.mod(MOD).compareTo(n) == 0) { //ans.mod(MOD) == n,不合法
					System.out.println(i % 9973);
					break;
				}
			}
		}
	}
}

PS:常用函数

import java.util.Scanner;

public class Main {
	public double f(double x) {
		return x * 6.5573;
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			Main ans = new Main();
			double x = cin.nextDouble();
			System.out.println(String.format("%.2f", ans.f(x))); //保留两位小数
		}
	}
}

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

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			String ch = cin.next();
			char b;
			if(ch.charAt(0) <= 'Z') b = ch.charAt(0);//取字符串某一位
			else b= (char)((int)ch.charAt(0) - 32); //类型转换
			//int a = cin.nextInt();
			//String a = cin.next();
			System.out.println(b);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值