どのように私は、その合計指定された入力数に等しい配列で私のユーザの入力数をペアにすることができますか?

ジョシュアラゴス:

ここではこれまでのところ、我々はGUIでそれを行うように求めていた私のハードワークコードです。私はその合計指定された入力数に等しい配列で私のユーザの入力数をペアの隣に何をすべきかのアプローチで私を助けてください?

package josh;


import javax.swing.JOptionPane;

public class Sample {


    public static void main (String args[]) {

        int colInput,rowInput;


        String user_col = JOptionPane.showInputDialog("Enter Column Of Array:");
        colInput =  Integer.parseInt(user_col);


        String user_row = JOptionPane.showInputDialog("Enter Row Of Array:");
        rowInput = Integer.parseInt(user_row);



        int user_value;


        for (int i = 0; i < colInput; i++) {
            for (int j= 0; j < rowInput; j++) {

            String values = JOptionPane.showInputDialog(null, "Enter Value "+(i+1));
             user_value = Integer.parseInt(values);

            final Integer [] value_arr = new Integer [user_value];




                }

            }


        }
}

ここではサンプル入力と出力のです

ここではサンプル入力と出力のです

TrishulシンChoudharyさん:

私はあなたが使用できることを基本的なロジックを与えるだろう。

// value_arr[] is the array where you have all the elements.
// sum is the number to be paired

Arrays.sort(value_arr);
int i=0,j=value_arr.length-1;

// now value_arr is sorted. we'll use two pointers i & j to move.. i moves in from left side towards centre and j moves from right side towards centre

while(i<j){
     if(value_arr[i]+value_arr[j]>sum){
         j--;
     }
     else if(value_arr[i]+value_arr[j]<sum){
         i++;
     }
     else if(value_arr[i]+value_arr[j]==sum){
         System.out.println(value_arr[i]+"+"+value_arr[j]+"="+sum);
     }
}

それが助け場合、私に教えてください

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=27550&siteId=1