备战蓝桥杯 DAY_04

import java.util.Arrays;
//给你两个按 非递减顺序 排列的整数数组nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
//请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。
//注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。
//为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
public class test01 {
    public static void main(String[] args) {
        int m = 3;
        int n = 3;
        int[] nums1 = new int[m+n];
        int[] nums2 = new int[n];
        for (int i = 0; i < m; i++) {
            nums1[i] = i+1;
            nums2[i] = i+2;
            nums1[m+i] = nums2[i];
        }
        Arrays.sort(nums1);
        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i] + " ");//1 2 2 3 3 4
        }
    }
}
//给定数列1,1,1,3,5,9,17...从第四项开始,每项都是前三项的和
//求20190327项的最后四位数
public class test03 {
    public static void main(String[] args) {
        // 由于数字过于庞大,不能直接输出20190327项的最后四位数
        int a = 1;
        int b = 1;
        int c = 1;
        int count=0;
        for(int i = 4; i <= 20190324;i++){
            count = (a+b+c)%10000;
            a = b;
            b = c;
            c = count;
        }
        System.out.println(count);//4659
    }
}
//我们知道第一个质数是2、第二个质数是3、第三个质数是5……
//请你计算第 2019个质数是多少?
public class test04 {
    public static void main(String[] args) {
        // 方法一:(超时,共用3s)
//        int count = 3;
//        for (int i = 6; i < Integer.MAX_VALUE; i++) {
//            for (int j = 2; j < i; j++) {
//                if(i % j == 0){
//                    break;
//                }
//                if (i % j != 0 && j == i-1){
//                    count++;
//                    if (count == 2019){
//                        System.out.println(i);//17569
//                    }
//                }
//
//            }
//        }
        // 方法二:
        int n = 2;
        int counts = 0;
        while(true){
            if(flag(n)){
                counts++;
            }
            if(counts == 2019){
                System.out.println(n);//17569
                break;
            }
            n++;
        }


    }
    //判断是否为质数
    public static boolean flag(int n){
        for(int i = 2;i <= Math.sqrt(n);i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
}
//乐羊羊饮料厂正在举办一次促销优惠活动。
//乐羊羊 C 型饮料,凭 3 个瓶盖可以再换一瓶 C 型饮料,
//并且可以一直循环下去(但不允许暂借或赊账)
//请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,
//那么,对于他初始买入的 n 瓶饮料,最后他一共能喝到多少瓶饮料。
public class test05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//100
        int count = n;
        while (n > 3){
            count += n/3;
            n = n/3 + n%3;
        }
        System.out.println(count);//149
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值