1132 Cut Integer(JAVA)

1132 Cut Integer(20 分)

Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 × 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.
Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20). Then N lines follow, each gives an integer Z (10 ≤ Z <2​31​​). It is guaranteed that the number of digits of Z is an even number.
Output Specification:

For each case, print a single line Yes if it is such a number, or No if not.
Sample Input:

3
167334
2333
12345678

Sample Output:

Yes
No
No
题解:水题,但是有坑~
注意两点:1.0不能做除数,所以如果A,B有一个为零就要输出No。
2.substring(0,k),这个字符串中不包括k地址里的字符。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n-- > 0) {
            String s = sc.next();
            int k = s.length() / 2;
            String sa = s.substring(0, k);
            String sb = s.substring(k);
            Integer a = Integer.parseInt(sa);
            Integer b = Integer.parseInt(sb);
            if(a == 0 || b == 0)
                System.out.println("No");
            else {
                Integer c = a * b;
                Integer ss = Integer.parseInt(s);
                if(ss % c == 0)
                    System.out.println("Yes");
                else
                    System.out.println("No");
                }
            }
        sc.close();
    }
}
您似乎只给出了“给出java”这部分内容,您的问题是希望了解 Java 的某个特性、语法还是与 Java 相关的具体应用呢?如果是以刚才的铁皮切割问题为例,下面是一个基于动态规划思想用 Java 实现的解决方案。 --- ### Java 动态规划实现 我们将按照之前的思路,通过动态规划的方式解决这个问题。以下是完整的代码实现: ```java import java.util.*; public class MaxWaterCapacity { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 输入铁皮高度数组 System.out.println("请输入铁皮各单元的高度:"); String input = scanner.nextLine(); String[] heightStrArray = input.split(" "); int n = heightStrArray.length; int[] heights = new int[n]; for (int i = 0; i < n; ++i){ heights[i] = Integer.parseInt(heightStrArray[i]); } double maxVolume = getMaxVolume(heights); System.out.printf("最大水容量为: %.6f\n", maxVolume); } private static double getMaxVolume(int[] heights) { int n = heights.length; // dp 数组初始化 double[] dp = new double[n + 1]; // 构建前缀最小区间高度辅助数组用于加速计算 f(i,j) int[][] minHeightOfRange = new int[n][n]; for(int left=0;left<n;left++) { int localMinHeight=Integer.MAX_VALUE; for(int right=left;right<n;right++){ if(localMinHeight>heights[right]){ localMinHeight=heights[right]; } minHeightOfRange[left][right]=localMinHeight; } } // 开始填表过程类似于背包问题变种形式的操作流程 for(int len=1;len<=n;len++) for(int lbound=0;lbound+len-1<n;lbound++) { int rbound=lbound+len-1 ; double optionWithoutCutting=minHeightOfRange[lbound][rbound]*Math.sqrt(len); double optionWithCutting=-Double.MAX_VALUE; if(lbound<rbound && len>=2){ for(int cut=lbound;cut<rbound;cut++){ optionWithCutting=Math.max(optionWithCutting,dp[cut-lbound+1]+getMaxVolumeRecursively(minHeightOfRange,cut+1,rbound)); } } dp[len]=(optionWithoutCutting > optionWithCutting)? optionWithoutCutting : optionWithCutting; } return dp[n]; } private static double getMaxVolumeRecursively(int [][]precomputedHeights,int startIdx,int endIdx){ double res=(endIdx-startIdx)*Math.sqrt(endIdx-startIdx+1)*precomputedHeights[startIdx][endIdx]; return res; } } ``` --- ### 说明 此程序首先读取用户输入的一系列数字代表每单位长度上不同点的高度信息构成数组`heights`,随后调用核心函数 `getMaxVolume()` 来求解最大水量,并输出结果。 为了简化计算过程中的频繁查找操作,先预处理出任意区间内的最小值矩阵存储起来方便后续引用提高性能表现。 此外需要注意的是由于涉及浮点运算所以保留适当有效小数位数保证精确度同时避免不必要的舍入误差累积干扰最终判断结论准确性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值