当前位置: 首页 > news >正文

腾讯云智三道算法题

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;public class MyMain {//第一题:一个水果切成n块public static void getRes(int n, int l, int r){int min = -1;int max = -1;for (int i=l;i<=r;i++){if (i%n==0){min = i/n;break;}}for (int i=r;i>=l;i--){if (i%n==0){max=i/n;break;}}if (min==-1||max==-1){System.out.println(-1);}else{System.out.println(min+" "+max);}}//    //第二题-数组 ,初始长度
//    //9 1
//    //9 5 8 1 3 2 7 6 4
//    public static void getTwoRes(int[] nums, int len){
//        int n = nums.length;
//        Arrays.sort(nums);
//        if (nums[0]>len){
//            System.out.println(len);
//            return;
//        }
//        int index = 0;
//        while (index<n&&nums[index]<=len){
//            len++;
//            index++;
//        }
//        System.out.println(len);
//    }
//
//    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int N = scanner.nextInt();
//        int L = scanner.nextInt();
//        int[] nums = new int[N];
//        for (int i=0;i<N;i++){
//            nums[i]=scanner.nextInt();
//        }
//        getTwoRes(nums,L);
//    }//[1,2]//第三题 1234 000 001 010 011 101 110 111public static void getResThree(String s){int n = s.length();int num = (int) Math.pow(2,n-1);List<List<Integer>> res = new ArrayList<>();int result = 0;for (int i=0;i<num;i++){List<Integer> ls = new ArrayList<>();for (int j=0;j<n-1;j++){if (((i>>j)&1)==1){ls.add(1);}else{ls.add(0);}}res.add(ls);}for (int i=0;i<res.size();i++){StringBuilder stringBuilder = new StringBuilder(s);int count = 1;for (int j=0;j<res.get(i).size();j++){if (res.get(i).get(j)==1){stringBuilder.insert(count,"+");count+=2;}else{count+=1;}}String[] split = stringBuilder.toString().split("\\+");long mySum=0;for (int k=0;k<split.length;k++){mySum += Long.parseLong(split[k]);}if (isPrime(mySum)){result++;}}System.out.println(result);}public static boolean isPrime(long n){if (n==1){return false;}if (n==2){return true;}for (int i=2;i<n;i++){if (n%i==0){return false;}}return true;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);getResThree(scanner.nextLine());}
//    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int N = scanner.nextInt();
//        int L = scanner.nextInt();
//        int[] nums = new int[N];
//        for (int i=0;i<N;i++){
//            nums[i]=scanner.nextInt();
//        }
//        getTwoRes(nums,L);
//    }
}

相关文章:

  • chrony服务器(1)
  • Python赋能教育:构建智能考试评分系统的最佳实践
  • 上位机知识篇---时钟分频
  • Android学习总结之自定义View绘制源码理解
  • springboot入门-controller层
  • 多系统安装经验,移动硬盘,ubuntu grub修改/etc/fstab 移动硬盘需要改成nfts格式才能放steam游戏
  • YOLOv8改进新路径:Damo-YOLO与Dyhead融合的创新检测策略
  • 第三方测试机构如何保障软件质量并节省企业成本?
  • Xilinx FPGA支持的FLASH型号汇总
  • git 工具
  • 架构进阶:105页PPT学习数字化转型企业架构设计手册【附全文阅读】
  • ARM架构的微控制器总线矩阵仲裁策略
  • 【Android】四大组件之Activity
  • Java 中 ConcurrentHashMap 1.7 和 1.8 之间有哪些区别?
  • 【补题】Codeforces Global Round 20 F1. Array Shuffling
  • Unity-Shader详解-其一
  • LabVIEW 工业产线开发要点说明
  • 深入理解TransmittableThreadLocal:原理、使用与避坑指南
  • 职业教育新形态数字教材的建设与应用:重构教育生态的数字化革命
  • html初识
  • 乌称泽连斯基与特朗普进行简短会谈
  • 来论|这无非就是一只“纸老虎”:评特朗普政府“关税战”
  • 印媒称印巴在克什米尔控制线沿线发生小规模交火,巴方暂未回应
  • 过敏性鼻炎,不只是“打喷嚏”那么简单
  • 2025年一季度上海市国民经济运行情况
  • 明日出征!航天员详细信息来啦