找到成对的数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找到成对的数字相关的知识,希望对你有一定的参考价值。
Write a code that will find the sum of any pairs of numbers
import java.io.*; import java.util.*; public class FindPair { public static Vector<MyPair> v = new Vector<MyPair> (); { // System.out.println ("Hello world"); int target = -1; if (args.length != 0) { } else { target = 5; } //int []array = {0,1,2,3,4,5}; int []array = new int[100]; int size = PopulateArrayFromFile(array); PopulateArrayFromFile(array); // for(int x=0; x < size; x++) // { //System.out.print (array[x]+ "... "); // } FindPairs(array,size, target); // System.out.println ("Size: " + v.size()); for(int n=0; n < size;n++ ) { } while (e.hasMoreElements()) { MyPair p = (MyPair)e.nextElement(); } } public static int PopulateArrayFromFile (int []arr) { int size = -1; try { String l; while ((l = br.readLine())!= null) { // System.out.println ("l: " + l); vFileContents.add(l); } size = vFileContents.size(); int count = 0; { // System.out.println ("x: " + x); // System.out.println ("arr: " + arr[count]); count++; } //System.out.println ("here size: " + arr.length); // for (int n=0; n < size; n++) // { // System.out.println ("here2: " + arr[n]); // } { io.printStackTrace (); } return size; } public static void FindPairs(int []array, int size, int target) { for (int i =0; i < size; i++) { for (int j = 1; j < size; j++) { int sum = array[i] + array[j]; if (sum == target) { //System.out.println ("Adding elem: " + array[i] + " " + array[j]); v.addElement(new MyPair(array[i],array[j])); } } } } } class MyPair { int x = 0; int y = 0; public MyPair (int x, int y) { this.x = x; this.y = y; } }
以上是关于找到成对的数字的主要内容,如果未能解决你的问题,请参考以下文章