蓝桥杯打卡day01(3.4)

文章目录

      • 数列求值
      • 质数
      • 饮料换购
      • 巧克力

数列求值

由于到20190324的数太大,会导致爆int,因此采用同余公式
( a + b + c + . . . . + n ) % m o d = a % m o d + b % m o d + . . . . + n % m o d (a + b + c + .... + n) \% mod = a \% mod + b \% mod + .... + n \% mod (a+b+c+....+n)%mod=a%mod+b%mod+....+n%mod

package day01;

public class 数列求值 {
	static int a = 1,b = 1,c = 1;
	public static void main(String[] args) {
		for(int i = 4; i <= 20190324; i++) {
			change(a,b,c);
		}
		
		System.out.println(c % 10000);
	}
	
	
	static void change(int a1,int b1,int c1) {
		int s = (a1 + b1 + c1) % 10000;
		a =  b1;
		b = c1;
		c = s;
	}
	
}

质数


public class 质数 {
	public static void main(String[] args) {
		int cnt = 0;
		for(long i = 2; ; i ++) {
			if(isPrime(i))cnt++;
			if(cnt == 2019) {
				System.out.println(i);
				break;
			}
		}
	}
	
	static boolean isPrime(long n) {
		for(long i = 2; i * i <= n; i ++) {
			if(n % i == 0)return false;
		}
		return true;
	}
}

饮料换购


import java.util.Scanner;

public class 换饮料 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int res = n;
		while(n >= 3) {
			n -= 3;
			res += 1;
			n += 1;
		}
		System.out.println(res);
	}
}

巧克力

参考题解:https://www.lanqiao.cn/questions/302038/

import java.util.*
public class 巧克力 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
//        吃巧克力天数
        int x = sc.nextInt();
        int n = sc.nextInt();
        //价格,保质期,数量
        int[][] kinds = new int[n][3];

        for (int i = 0; i < n; i++) {
            kinds[i] =  new int[]{sc.nextInt(),sc.nextInt(),sc.nextInt()};
        }

        //先按保质期排序,再按价格排序
        //保质期从高到低,价格从高到低
        Arrays.sort(kinds, new Comparator() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[1] != o2[1])return o2[1] - o1[1];
                return o2[0] - o1[0];
            }
        });
        //优先队列
        PriorityQueue list = new PriorityQueue();
        Point point;
        int index = 0;
        long res = 0;

        while(x > 0){
            //所有满足条件的且保质期大于x的种类
            while(index < n && kinds[index][1] >= x){
                //小根堆 :价格小的在上面
                list.add(new Point(kinds[index][0],kinds[index][1],kinds[index][2]));
                index++;
            }

            //要求时长还有,但已经没有巧克力了
            if(list.size() == 0){
                res = -1;
                break;
            }else {
                point = list.peek();
                res += point.x;
                //天数减一
                x--;
                //该种类掐巧克力减一
                point.z--;
                if(point.z == 0){
                    list.remove(point);
                }
            }
        }
        System.out.println(res);


    }

  static   class Point implements Comparable {
        //价格,保质期, 数量,
        public int x,y,z;

        public Point(int px,int py,int pz){
            x = px;
            y = py;
            z = pz;
        }
        @Override
        public int compareTo(Point o) {
            if (x != o.x) return x - o.x;
            else return y - o.y;
        }
    }
}

你可能感兴趣的:(蓝桥杯,java,职场和发展)