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

腾讯云智三道算法题

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);
//    }
}
http://www.xdnf.cn/news/2385.html

相关文章:

  • 侵水防触电的原理是什么? 侵水防触电算先进技术吗?-优雅草卓伊凡
  • 【Redis——通用命令】
  • 写时拷贝讲解
  • SQL:MySQL 函数
  • Eigen库入门
  • 博客文章格式更新2.0
  • N维漂洛界的定义和参数方程
  • 算法设计课作业
  • 【概念】什么是 JWT Token?
  • JAVA多线程(8.0)
  • matlab实现稀疏低秩去噪
  • day7 python针对心脏病数据集预处理
  • Java ThreadLocal与内存泄漏
  • 黑马Java基础笔记-4
  • 青少年CTF-贪吃蛇
  • YOLOv11改进:RevColV1可逆列目标检测网络(特征解耦助力小目标检测)
  • 写入cache时数据格式错误产生的ERRO导致整个测试框架无法运行
  • 大模型时代的语言格局演变:为什么是 JavaScript?
  • PyTorch数据加载与预处理
  • 模板引擎语法-过滤器
  • TeaCache原理及代码
  • 泛型进阶之通配符
  • import tree # pip install dm_tree ModuleNotFoundError: No module named ‘tree‘
  • 如何导出1寸分辨率为300及以上的照片?
  • 常见cmd命令
  • 基于PyTorch的图像识别主要依赖于深度学习模型(尤其是卷积神经网络,CNN)对图像特征进行自动学习和分类
  • tigase源码学习杂记-IO处理的线程模型
  • Python-MCPServerStdio开发
  • python输出
  • 防火墙规则配置错误导致的网络问题排查