java大数

题目

https://cn.vjudge.net/problem/FZU-2278

https://cn.vjudge.net/problem/FZU-2281


/*
大数的加减运算不同于普通整数的加减乘除运算
加—— a+b: a=a.add(b);
减—— a-b: a=a.subtract(b); 
乘—— a*b: a=a.multiply(b);
除—— a/b: a=a.divide(b);
求余—a%b: a=a.mod(b);
转换—a=b: b=BigInteger.valueOf(a);
比较 if (ans.compareTo(x) == 0)//比较
    System.out.println("相等");
System.out.println("a + b = "+ans_add); // 这里的‘+’ (第二个) 是连接的意思
*/
import java.util.*;
import java.math.*;

public class Main {
	public static void main(String args[]) {
		Scanner cin = new Scanner(System.in);

		int T; //声明int
		T = cin.nextInt();//读入int
		while((T--) != 0) 
		{
			int n;
			n = cin.nextInt();

			BigInteger p = new BigInteger("1"); //大数
			
			for(int i =  1;i <= n;i++)
			{
				Integer ii = new Integer(i);//转化为类
				BigInteger iii = new BigInteger(ii.toString());
				p = p.multiply(iii);
			}
			BigInteger ans = new BigInteger("0");
			for(int i = 1;i <= n;i++)
			{
				Integer ii = new Integer(i);
				BigInteger iii = new BigInteger(ii.toString());
				ans = ans.add(p.divide(iii));
			}
			System.out.print(ans);
			System.out.println(".0");
		}
	}




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

public class Main {
	public static void main(String args[]) {
		Scanner cin = new Scanner(System.in);

		int T; //声明int
		T = cin.nextInt();//读入int
		int ko = 1;
		while((T--) != 0) 
		{
			int n;
			n = cin.nextInt();
			BigInteger m;
			m = cin.nextBigInteger();
			BigInteger a[] = new BigInteger[20005];
			int top = 1;
			int f = 0;
			for(int i = 1;i <= n;i++)
			{
				a[top] = cin.nextBigInteger();
				if(i==1||a[i].equals(a[i-1]) == false)
				{
					top++;
					f = i;
				}
			}
			if(f == n) top--;
			a[0] = new BigInteger("100000000006");
			a[top+1] = new BigInteger("0");
			BigInteger num = new BigInteger("0");
			BigInteger modd = new BigInteger("1000000007");
			for(int i = 1;i <= top;i++)
			{
				if(a[i].compareTo(a[i-1])<0&&a[i].compareTo(a[i+1])<0)
				{
					num = m.divide(a[i]);
					m = m.mod(a[i]);
				}
				if(a[i].compareTo(a[i-1])>0&&a[i].compareTo(a[i+1])>0)
				{
					m = m.add(num.multiply(a[i]));
					num = new BigInteger("0");
				}
			}
			m = m.mod(modd);
			System.out.print("Case #"+ko+": ");
			ko++;
			System.out.println(m);
		}
	}
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值