FZU - 2281 Trades(JAVA大数,非常方便)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37129433/article/details/81877082

Trades
题 意:一个城镇有一列n个商店,每个商店都有一个价格ai,可以以那个商店的价格卖出
或者卖卖出物品。现在你身上有m单位的钱。问你最后你最多能有多少钱。

数据范围:
1<=n<=2000
0<=m<=1000000000
1<=ci<=1000000000

样例输入:

2
3 1
1 2 3
4 1
1 2 1 2

样例输出:

Case #1: 3
Case #2: 4

思 路:还是一样的思路,波谷买入,波峰卖出,这次比上次多了一个开始就有钱,其实开始有钱也没有什么用。代码还是没变。
收 获:java写大数 真的方便的多。
java大数的一些常规操作。
减法a.substract(b)
加法 a.add(b)
乘法 a.multiply(b)
除法 a.divide(b)
取模 a.mod(b)
比较a.compile(b) 大于返回1 小于返回-1 等于返回0

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int T,Kase=0;
        int n,m;
        int []a = new int[2005];
        T  = input.nextInt();
        BigInteger MOD = BigInteger.valueOf(1000000007);
        for(int xx=0;xx<T;xx++){
            n=input.nextInt();
            m=input.nextInt();
            for(int i=0;i<n;i++){
               a[i] =  input.nextInt();
            }
            int sg = 0;
            BigInteger k = new BigInteger(String.valueOf(m));
            BigInteger time=BigInteger.valueOf(0),sum=BigInteger.valueOf(0),ans = BigInteger.valueOf(0);
            for(int i=1;i<n;i++){
                if(a[i-1] == a[i]) continue;
                else if(a[i]>a[i-1]){
                    if(sg == 0){
                        time = k.divide(BigInteger.valueOf(a[i-1]));
                    }
                    BigInteger temp2 = time.multiply(BigInteger.valueOf(a[i]-a[i-1]));
                    sum = sum.add(temp2);
                    sg = 1;
                }else {
                    sg = 0;
                    k = sum.add(k);
                    sum = BigInteger.valueOf(0);
                }
            }
            ans = sum.add(k);
            ans = ans.mod(MOD);
            Kase = Kase + 1;
            System.out.println("Case #"+Kase+": "+ans);
        }
    }
}
/*
2
3 1
1 1000000000 1000000
*/

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/81877082