简体   繁体   中英

Printing max and min value , check program

I've been trying to write a program to print the min and max value of 5 integer type variables, using no arrays and only swapping using this technique

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

now this is what I came up with ( although I know it's possible with only 6 swaps however I can't figure it out):

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);

Now this should work, but I also needed to write a code to make sure the program works fine, so I could check all the combinations with 0's and 1's in them. ie 2^5=32 combinations. and if one of them fails print it. I've tried to do this :

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);
                    }
                }
            }
        }
    }

but this doesn't work because once there is a "1" in any variable a gets it and then I'm not gonna be able to run the combinations properly.. ****basically this is a tester for the code within the loop, so it should test 0000-11111 and print something like verified if all works well, or print the combination for which it got the wrong result.. however I can't see how it's gonna do that .**** any suggestions? no arrays , swapping only... thanks ahead

I would use Math.min(int, int) and Math.max(int, int) and a long chain of invocations. Like,

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);

Your "swapping piece of code" makes no sense. Here is another implementation

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;

Why don't you store the a,b,c,d,e values in the innermost loop and reassign it back to the variables again after your swapping piece of code? Like so,

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;
                    }
                }
            }
        }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM