备战蓝桥杯 DAY_05

//给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
//你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
public class test01 {
    public static void main(String[] args) {
        int [] nums = {8,1,2,1,2,5,5,6,6};
        int ans = nums[0];
        for (int i = 1;i< nums.length;i++){
            ans ^= nums[i];//循环比较,相等ans就取0,不相等ans就取nums[i]
        }

        System.out.println(ans);//8
    }
}
//给定一个正整数 n,找到并返回 n 的二进制表示中两个 相邻 1 之间的 最长距离 。如果不存在两个相邻的 1,返回 0 。
//如果只有 0 将两个 1 分隔开(可能不存在 0 ),则认为这两个 1 彼此 相邻 。
//两个 1 之间的距离是它们的二进制表示中位置的绝对差。例如,"1001" 中的两个 1 的距离为 3 。
public class test02 {
    public static void main(String[] args) {
        int n = 22;
        //方法一(超时)
//        int a = 0;
//        int b = 0;
//        int [] arr = new int[n];
//        int []sum = new int[n];
//
//        //十进制转二进制
//        for (int i = n;i > 0;i /= 2){
//            arr[a] = i % 2 ;
//            a++;
//        }
//
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print(arr[i]);
//        }
//        System.out.println();
//
//        //将所有相邻的1的距离写入数组
//        for (int i = 0; i < arr.length; i++) {
//            for (int j = i+1; j < arr.length; j++) {
//                if (arr[i] == 1 && arr[j] == 1){
//                    sum[b] = j-i;
//                    b++;
//                    break;
//                }
//            }
//
//        }
//        //找到并输出最大距离
//        int max = sum[0];
//        for (int i = 0; i < sum.length; i++) {
//            if (sum[i] > max){
//                max = sum[i];
//            }
//        }
//        System.out.println(max);//2

        // 方法二
        int p = -1;
        int i = 0;
        int max = 0;
        while(n != 0){
            int r = (n & 1);
            if(r == 1){
                if(p == -1){
                    p = i;
                }else{
                    max = Math.max(max,i - p);
                    p = i;
                }
            }
            n = n >> 1;
            i++;
        }
        System.out.println(max);//2
    }
}
//请你找到最小的整数X 同时满足:
//X 是2019 的整 倍 数;
//X 的每一位数字都是奇数。
public class test03 {
    public static void main(String[] args) {
        int X = 2019;
        while(true){
            if (isOK(X)){
                System.out.println(X);//139311
                break;
            }
            X += 2019;
        }
    }
    public static boolean isOK(int n){
        while (n > 0){
            int num = n % 10;
            if (num == 0 || num == 2 || num == 4 || num == 6 || num == 8 ){
                return false;
            }
            n /= 10;
        }
        return true;
    }
}
//学习了约数后,小明对约数很好奇,他发现一个正整数t,总是可以找到含有t个约数的整数.
//S1 = 1, S2 = 2, S3 = 4, S4 = 6,...
//小明想知道当t=100时,St时多少?即S100
public class test04 {
    public static void main(String[] args) {
//        //方法一:暴力算法,超时,用时3s
//        for (int i = 0;;i++) {
//            int n = 0;
//            for (int j = 1; j <= i; j++) {
//                if (i % j == 0) {
//                    n++;
//                    if (n == 100) {
//                        System.out.println(i);//45360
//                        return;
//                    }
//                }
//            }
//        }
        //方法二:质因子筛法(看不懂)
        int s = 100;
        long i;
        for (i = 1; i <= Long.MAX_VALUE; i++) {
            long temp = i;
            int countFactor = 1; //乘法初始值
            for (long j = 2; j <= temp / j; j++) {
                int count = 0;
                while (temp % j == 0) {
                    count++;
                    temp /= j;
                }
                if (count > 0) countFactor = countFactor * (count + 1);
            }
            if (temp > 1) countFactor = countFactor * 2;
            if (countFactor == s) break;
        }
        System.out.println(i);//45360
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值