Hankson of "inverse problem"

, Entitled Analysis
1. Problem: Unknown provided a positive integer x satisfying:
(. 1) and x is the greatest common divisor of a0 A1;
(2) is the least common multiple of x, and b0 b1.
Hankson of "inverse problem" is determined to meet the conditions of positive integers x.
2. Analysis of Process
(1) according to claim two, results of x is determined conditions;
(2) because the number x is about b1, so that x <= b1, y = b1 / x; y meet the conditions may be reduced number of cycles, the number of conditions to determine compliance range;
(3) the number of each entry 4, these four numbers stored in the array, n sets of input data, i.e., processing n times;
two, the analysis program
1. procedural composition
1.1 two classes:
(. 1) the Solve categories: determined to meet the requirements of x, and outputs the number x and the value of x
(2) Improve categories: test class
1.2 three methods
1.2.1 int Max (int a, int b ) {}
function: greatest common divisor of two numbers

//求两个数的最大公约数(辗转相除法)
    public  static int Max(int a,int b){
        if(a%b == 0){
            return b;
        }else{
            return Max(b,a%b);
        }
    }

1.2.2 void improve () {}
function: determining the number meet condition number, and outputs

/**
     *
     * @param: []
     * @return: void
     * @Description:判断符合条件数的个数,并输出
     */
    public static void improve(){

        int[] arrays = new int[4];
        int count = 0;
        for(int i = 0;i < 4;i++){
            System.out.print("请输入第"+(i+1)+"个数:");
            int number = new Scanner(System.in).nextInt();
            //将输入的数字存到数组中
            arrays[i] = number;
        }
        //x->(1-sqrt(array[3]))
        for(int x = 1;x*x <= arrays[3];x++){
            if(arrays[3]%x == 0){
                //判断条件:x、arrays[0]的最大公约数是arrays[1];
                //          x和arrays[2]的最小公倍数是arrays[3];
                if(Max(arrays[0],x) == arrays[1]&&((arrays[2]*x)/Max(arrays[2],x)) == arrays[3]){
                    count++;
                    System.out.print(x+" ");
                }
                int y = arrays[3]/x;
                if(x == y){
                    continue;
                }
                if(Max(arrays[0],y) == arrays[1]&&((arrays[2]*y)/Max(arrays[2],y)) == arrays[3]){
                    count++;
                    System.out.print(y+" ");
                }
            }
        }
        System.out.println("满足条件的x有:"+count+"个");
    }

1.2.3 void main (String [] args ) {}
function: a main method, test Improve () method, and outputs the result

public static void main(String[] args) {
        int k = 1;
        while(k == 1){
            System.out.println("请输入数据组数:");
            int group = new Scanner(System.in).nextInt();
            //循环输入组数次
            for (int i = 0; i < group; i++) {
                System.out.println("第" + (i + 1) + "组:");
                new Solve().improve();
            }
            System.out.println("继续选择1,退出选择0");
            k = new Scanner(System.in).nextInt();
        }
    }

1.3 source code:

package www.program.code;

import java.util.Scanner;

class Solve{
    //求两个数的最大公约数
    public  static int Max(int a,int b){
        if(a%b == 0){
            return b;
        }else{
            return Max(b,a%b);
        }
    }

    /**
     *
     * @param: []
     * @return: void
     * @Description:判断符合条件数的个数,并输出
     */
    public static void improve(){

        int[] arrays = new int[4];
        int count = 0;
        for(int i = 0;i < 4;i++){
            System.out.print("请输入第"+(i+1)+"个数:");
            int number = new Scanner(System.in).nextInt();
            //将输入的数字存到数组中
            arrays[i] = number;
        }
        //x->(1-sqrt(array[3]))
        for(int x = 1;x*x <= arrays[3];x++){
            if(arrays[3]%x == 0){
                //判断条件:x、arrays[0]的最大公约数是arrays[1];
                //          x和arrays[2]的最小公倍数是arrays[3];
                if(Max(arrays[0],x) == arrays[1]&&((arrays[2]*x)/Max(arrays[2],x)) == arrays[3]){
                    count++;
                    System.out.print(x+" ");
                }
                int y = arrays[3]/x;
                if(x == y){
                    continue;
                }
                if(Max(arrays[0],y) == arrays[1]&&((arrays[2]*y)/Max(arrays[2],y)) == arrays[3]){
                    count++;
                    System.out.print(y+" ");
                }
            }
        }
        System.out.println("满足条件的x有:"+count+"个");
    }
}

/**
 * @Author:Star
 * @Date:Created in 13:45 2019/3/21
 * @Description:解决Hankson问题:
 * 设某未知正整数x满足:
 * 1、  x和a0的最大公约数是a1;
 * 2、  x和b0的最小公倍数是b1。
 * Hankson的“逆问题”就是求出满足条件的正整数x
 */
public class Improve {
    public static void main(String[] args) {
        int k = 1;
        while(k == 1){
            System.out.println("请输入数据组数:");
            int group = new Scanner(System.in).nextInt();
            //循环输入组数次
            for (int i = 0; i < group; i++) {
                System.out.println("第" + (i + 1) + "组:");
                new Solve().improve();
            }
            System.out.println("继续选择1,退出选择0");
            k = new Scanner(System.in).nextInt();
        }
    }
}

2. Flowchart
Here Insert Picture Description
Third, debugging
1. Enter 0 an error occurred
Here Insert Picture Description
2. 0 input data were processed
Here Insert Picture Description
Fourth, the test
1. Enter the data illegal
Here Insert Picture Description
6.3.5 Operating results
Here Insert Picture Description
VI Summary
raise questions 1. reverse thinking, can also the evaluation methods known to place, the condition is satisfied the counter +1; final output value to the counter.
2. For the reverse process of solving a problem, previously had any contact with, so for me to write good code in some difficulty, the program adopted a more simple substitution method to solve the problem, but also a good time training opportunities, but the code written is still very bad, follow-up will continue to think, I hope we can find some good places to do, encourage each other.

Guess you like

Origin blog.csdn.net/weixin_44369212/article/details/88752192