/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg3arraysum;
/**
*
* @author Sunshine
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a1[] = new int[]{2, -3, 7, 8};
int a2[] = new int[]{4, -6, 2, -1};
int a3[] = new int[]{-6, 4, 7, 0};
sort(a1);
sort(a2);
sort(a3);
int sum;
for(int i=0;i<a1.length;i++){
int j=0,k=a3.length-1;
while(j<a2.length && k>0){
sum=a1[i]+a2[j]+a3[k];
if(sum>0)
--k;
else if(sum<0)
++j;
else if(sum==0)
System.out.println(a1[i]+"+"+a2[j]+"+"+a3[k]+"=0");
}
}
}
public static void sort(int[] a) {
int j;
boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < a.length - 1; i++) {
if(a[i]>a[i+1]){
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
flag = true;
}
}
}
}
}
No comments:
Post a Comment