打印最大值和最小值,检查程序

时间:2018-11-06 22:02:37

标签: java

我一直试图编写一个程序来打印5个整数类型变量的最小值和最大值,不使用数组,而仅使用此技术进行交换

if (x > y) {
    int tmp = x;
    x = y;
    y = tmp;
 }

现在这就是我的想法(尽管我知道只有6次交换是可能的,但是我不知道):

Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();

//storing max value in a all the way, and min value in b
if (a < b) {
    int tmp = a;
    a = b;
    b = tmp;

}
if (a < c) {
    int tmp = a;
    a = c;
    c = tmp;

}
if (a < d) {
    int tmp = a;
    a = d;
    d = tmp;

}
if (a < e) {
    int tmp = a;
    a = e;
    e = tmp;

}
if (c < b) {
    int tmp = b;
    b = c;
    c = tmp;

}
if (d < b) {
    int tmp = b;
    b = d;
    d = tmp;

}
if (e < b) {
    int tmp = b;
    b = e;
    e = tmp;

}
System.out.println(b +"\n" + a);

现在这应该可行,但是我还需要编写代码以确保程序正常运行,因此我可以检查其中包含0和1的所有组合。 即2 ^ 5 = 32个组合。如果其中之一失败,则将其打印出来。 我尝试这样做:

for (int a = 0; a <= 1; a++) {
        for(int b = 0; b <= 1; b++) {
            for(int c = 0; c <= 1; c++) {
                for(int d = 0; d <= 1; d++) {
                     for(int e = 0; e <= 1; e++) {
                        if (a < b){
                            int tmp = a;
                            a = b;
                            b = tmp;

                        }
                        if (a < c) {
                            int tmp = a;
                            a = c;
                            c = tmp;

                        }
                        if (a < d) {
                            int tmp = a;
                            a = d;
                            d = tmp;

                        }
                        if (a < e) {
                            int tmp = a;
                            a = e;
                            e = tmp;

                        }
                        if (c < b) {
                            int tmp = b;
                            b = c;
                            c = tmp;
                        }
                        if (d < b) {
                            int tmp = b;
                            b = d;
                            d = tmp;

                        }
                        if (e < b) {
                            int tmp = b;
                            b = e;
                            e = tmp;
                        }
                        System.out.println(b + "\n" + a);
                    }
                }
            }
        }
    }

但是这不起作用,因为一旦任何变量中有一个“ 1”,a就会得到它,然后我将无法正确运行组合。 ****基本上,这是循环中代码的测试器,因此它应该测试0000-11111并打印出经过验证的内容(如果一切正常),或者打印出错误结果的组合。。但是我可以看不出来怎么办。**** 有什么建议么?没有数组,仅交换... 谢谢你

2 个答案:

答案 0 :(得分:1)

我将使用Math.min(int, int) Math.max(int, int)和一长串调用。喜欢,

Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Math.min(Math.min(Math.min(Math.min(a, b), c), d), e);
int max = Math.max(Math.max(Math.max(Math.max(a, b), c), d), e);

您的“交换代码段”毫无意义。这是另一种实现方式

Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
min = (a < min) ? a : min;
min = (b < min) ? b : min;
min = (c < min) ? c : min;
min = (d < min) ? d : min;
min = (e < min) ? e : min;
max = (a > max) ? a : max;
max = (b > max) ? b : max;
max = (c > max) ? c : max;
max = (d > max) ? d : max;
max = (e > max) ? e : max;

答案 1 :(得分:1)

为什么不将a,b,c,d,e值存储在最里面的循环中,并在交换代码段后再次将其重新分配给变量?像这样

public static void main(String args[]) {
        for (int a=0 ;a<=1 ;a++ ){
        for(int b=0 ;b<=1 ;b++ ){
            for(int c=0 ;c<=1 ;c++ ){
                for(int d=0 ;d<=1 ;d++ ){
                     for(int e=0 ; e<=1 ;e++ ){
                         // Store the variable values
                         int ap = a;
                         int bp = b;
                         int cp =c;
                         int dp = d;
                         int ep = e;

                        // Swapping logic for finding min and max
                        // Due to swapping, the values of a,b,c,d,e may change
                        if ( a < b){
                            int tmp = a;
                            a=b;
                            b=tmp;

                        }
                        if ( a < c){
                            int tmp = a;
                            a=c;
                            c=tmp;

                        }
                        if ( a < d){
                            int tmp = a;
                            a=d;
                            d=tmp;

                        }
                        if ( a < e){
                            int tmp = a;
                            a=e;
                            e=tmp;

                        }
                        if ( c < b){
                            int tmp = b;
                            b=c;
                            c=tmp;

                        }
                        if ( d < b){
                            int tmp = b;
                            b=d;
                            d=tmp;

                        }
                        if ( e < b){
                            int tmp = b;
                            b=e;
                            e=tmp;

                        }
                        System.out.print(a+""+b+""+c+""+d+""+e+" ");
                        System.out.println(b + " " + a);

                        // Reassign the variable values
                        a = ap;
                        b = bp;
                        c=cp;
                        d=dp;
                        e=ep;
                    }
                }
            }
        }
    }
    }