Java经典编程题50道之四十三

一个偶数总能表示为两个素数之和。

public class Example43 {
    public static void main(String[] args) {
        f();
    }

public static boolean fun(int a) {
        boolean flag = false;
        if (a == 3) {
            flag = true;
            return (flag);
        }
        for (int i = 2; i <= Math.sqrt(a); i++) {
            if (a % i == 0) {
                flag = false;
                break;
            } else
                flag = true;
        }
        return (flag);
    }

public static void f() {
        Scanner s = new Scanner(System.in);
        int n, i;
        do {
            System.out.print("请输入一个大于等于6的偶数:");
            n = s.nextInt();
        } while (n < 6 || n % 2 != 0);
        for (i = 2; i <= n / 2; i++) {
            if (fun(i) && fun(n - i)) {
                int j = n - i;
                System.out.println(n + " = " + i + " + " + j);
            }
        }
    }
}

时间: 2024-07-30 01:04:04

Java经典编程题50道之四十三的相关文章

Java经典编程题50道之四十六

编程实现两个字符串的连接. public class Example46 {    public static void main(String[] args) {        addString("hello"," world!");    }    public static void addString(String s1,String s2){        String s3=s1+s2;        System.out.println("

Java经典编程题50道之四十七

读取7个数(1~50)的整数值,每读取一个值,程序打印出该值个数的*. public class Example47 {    public static void main(String[] args) {        int[] a = { 10, 7, 6, 15, 4, 3, 20 };        display(a);    } public static void display(int[] a) {        System.out.print("读取的整数有:"

Java经典编程题50道之四十

将几个字符串排序(按英文字母的顺序). public class Example40 {    public static void main(String[] args) {        String[] s={"math","english","java","java web","rose"};        stringSort(s);    } public static void stringS

Java经典编程题50道之四十八

某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 每位数字都加上5,然后用和除以10的余数代替该数字, 再将第一位和第四位交换,第二位和第三位交换. public class Example48 {    public static void main(String[] args) {        f(2345);    } public static void f(int num) {        int[] c = new int[4];      

Java经典编程题50道之四十九

计算某字符串中子串出现的次数. public class Example49 {    public static void main(String[] args) {        String s1 = "adcdcjncfb";        String s2 = "";        count(s1, s2);    } public static void count(String str1, String str2) {        int cou

Java经典编程题50道之三十三

打印出杨辉三角形(要求打印出10行如下图)11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1 public class Example33 { public static void main(String[] args) {        yanghui(10);    } public static void yanghui(int n) {        int[][] a = new int[n][n];        for (int i = 0; i < n;

Java经典编程题50道之四十二

809*??=800*??+9*??+1,其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,以及809*??后的结果. public class Example42 {    public static void main(String[] args) {        f();    } public static void f() {        int a = 809, b, i;        for (i = 10; i < 13; i++)

Java经典编程题50道之四十五

判断一个整数能被几个9整除. public class Example45 {    public static void main(String[] args) {        f(729);    } public static void f(int n) {        int tmp = n;        int count = 0;        for (int i = 0; tmp % 9 == 0;) {            tmp = tmp / 9;         

Java经典编程题50道之四十四

求0~7所能组成的奇数个数.分析:组成1位数是4个,组成2位数是7*4个,组成3位数是7*8*4个,组成4位数是7*8*8*4个…… public class Example44 {    public static void main(String[] args) {        f();    } public static void f() {        int sum = 4;        int j;        System.out.println("组成1位数是 &quo