编程题练习
  FHVSBErvDkPx 2023年11月02日 35 0

package com.axu.exer9_15;

import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections;

/**

  • ClassName: Palindrome
  • Package: com.axu.exer9_15
  • Description:
  • 【一、最大回文乘积】
  • 回文数就是从前往后和从后往前读都一样的数。由两个2位数相乘得到的最大回文乘积是 9009 = 91 × 99。
  • 找出由两个3位数相乘得到的最大回文乘积。
  • @Author: 阿旭
  • @Create: 2023/9/15 - 18:24
  • @Version: v1.0 */ public class Palindrome { public static void main(String[] args) { int result = 0; int maxI = 0; int maxJ = 0; for (int i = 100; i < 1000; i++) { for (int j = 100; j < 1000; j++) { int product = i * j; if (isPal(i,j) && product > result) { result = product; maxI = i; maxJ = j; } } } System.out.println("最大回文乘积是 " + result + " = " + maxI + " × " + maxJ); } //判断乘积是否是回文数 public static boolean isPal(int a,int b){ int product = a * b; //将product转为字符串 String str = String.valueOf(product); String reverse = new StringBuffer(str).reverse().toString(); if (!str.equals(reverse)){ return false; } return true; } }

package com.axu.exer9_16;

/**

  • ClassName: AllDigitalPrime
  • Package: com.axu.exer9_16
  • Description:

  • @Author: 阿旭
  • @Create: 2023/9/17 - 8:52
  • @Version: v1.0 */ public class AllDigitalPrime { public static void main(String[] args) { for (int i = 987654321; i > 1; i-=2) { if (isQuanNum(i) && isPrime(i)){ System.out.println(i); break; } } } //全数判断 public static boolean isQuanNum(int n){ String str = String.valueOf(n); for (int i = 1; i <= str.length(); i++) { if (!str.contains(""+i)){ return false; } } return true; } //素数判断

public static boolean isPrime(int n){ if (n < 2){ return false; }else if (n == 2 && n == 3){ return true; }else{ for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } } return true; } }

package com.axu.exer9_16;

import java.util.HashSet;

/**

  • ClassName: a
  • Package: com.axu.exer9_16
  • Description:
  • 【一、全数字的乘积】
  • 如果一个n位数包含了1至n的所有数字恰好一次,我们称它为全数字的;例如,五位数15234就是1至5全数字的。
  • 7254是一个特殊的乘积,因为在等式39 × 186 = 7254中,被乘数、乘数和乘积恰好是1至9全数字的。
  • 找出所有被乘数、乘数和乘积恰好是1至9全数字的乘法等式,并求出这些等式中乘积的和。
  • 注意:有些乘积可能从多个乘法等式中得到,但在求和的时候只计算一次。

  • 分析:被乘数个数 + 乘数个数 + 乘积的个数 = 9
被乘数个数从一位数开始(最小为2)+ 乘数个数(只能为四位数最小为1000) + 乘积个数(四位数) = 9位
被乘数个数从二位数开始(最小为10)+ 乘数个数(只能为三位数最小为100) + 乘积个数(四位数) = 9位
被乘数个数从三位数开始(最小为100)+ 乘数个数(只能为二位数最小为10) + 乘积个数(四位数)= 9位

99 * 101 = 9999 个数相加为9
被乘数和乘数从2开始

定义方法:
返回值 int
参数 ()
长度不是9,break;
非1-9,break;
  • @Author: 阿旭
  • @Create: 2023/9/16 - 18:13
  • @Version: v1.0 */public class AllDigitalProduct { public static void main(String[] args) { product(); } //全数字乘积 public static void product(){ HashSet products = new HashSet<>(); int pro = 0; int i; int j = 0; for (i = 1; i < 10000; i++) { for (j = 1; j < 10000; j++) { pro = i * j; String strP = String.valueOf(pro); String strI = String.valueOf(i); String strJ = String.valueOf(j);
String strAll = strP.concat(strI).concat(strJ);

            if (isQuanNum(strAll)) {
                products.add(pro);
                System.out.println(i + "*" + j + "=" + pro);
            }
        }
    }
    long sum = 0;
    for (int product : products) {
        sum += product;
    }

    System.out.println("满足条件的乘积之和是:" + sum);

}
//是否是全数字
/*public static boolean isQuanNum(String strAll){
    return strAll.length() == 9 && strAll.chars().distinct().count() == 9 && !strAll.contains("0");
}*/

public static boolean isQuanNum(String strAll){
    if (strAll.length()!=9){
        return false;
    }
    for (int i = 1; i <= 9; i++) {
        if (!strAll.contains(""+i)){
            return false;
        }
    }
    return true;
}

}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
FHVSBErvDkPx