Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何测试一个集合是否包含集合中其他两个数字的总和_Java_Recursion - Fatal编程技术网

Java 如何测试一个集合是否包含集合中其他两个数字的总和

Java 如何测试一个集合是否包含集合中其他两个数字的总和,java,recursion,Java,Recursion,假设我在我的isSumAB中有数字7和43,如果a和b在我的isSumAB中,那么我当然可以有a+b。所以我想做的是用一个布尔递归函数来测试数组,如果它是真的或不是真的,那么我怎么能用一个布尔函数来测试int a和b呢 int[] numbers = {10, 51,137, 464, 589 ...}; for(int num: numbers){ System.out.println("isSumAB()-- is " + num + " Sum

假设我在我的
isSumAB
中有数字7和43,如果a和b在我的
isSumAB
中,那么我当然可以有
a+b
。所以我想做的是用一个布尔递归函数来测试数组,如果它是真的或不是真的,那么我怎么能用一个布尔函数来测试int a和b呢

int[] numbers = {10, 51,137, 464, 589 ...};

     for(int num: numbers){
                System.out.println("isSumAB()-- is " + num + " Sum of A and B in Java :" + isSumAB(num));
    }
这种方法有些道理

public boolean isSumAB(int a, int b){

   if ( a > b )
     return false
   else if 
        y + sum( x, y - 1 );
      return true 
      //something

}

我不明白。你想检查数组中是否有一个元素的值与其他两个整数的和相同吗?@SotiriosDelimanolis是的,先生!也很困惑。问题是这些数字中是否有一个是集合中其他两个数字的和,或者其中是否有一个是前两个数字的和,或者其中是否有一个是从其他地方获得的两个数字的和?
数字
数组是否按升序预排序?它们是否都是肯定的?无论你在寻找什么,递归几乎肯定是问题的错误解决方案,而且你使这个问题比它需要的困难了15倍。想想你将如何手动解决它,并实现它。@keshlam它是一个任意的数字是集合中其他两个数字的总和。没有负数,它们都是正数。好吧,那么只做两个嵌套循环扫描集合有什么错呢?
import  java.util.Iterator;
import  java.util.Map;
import  java.util.Set;
import  java.util.TreeMap;
/**
   <P>{@code java FindElementsThatAreSumsOfOthers}</P>
 **/
public class FindElementsThatAreSumsOfOthers  {
   public static final void main(String[] igno_red)  {
      int[] ai = new int[]{10, 51, 137, 464, 589, 61, 452};

      //All numbers in a map, key is array-value, value is array-index
      Map<Integer,Integer> mpValIdxAll = new TreeMap<Integer,Integer>();

      for(int i = 0; i < ai.length; i++)  {
         mpValIdxAll.put(ai[i], i);
      }

      //Only those elements in the array that are *sums* of other elements
      //Key is array-index of sum, value is SumInfo object
      Map<Integer,SumInfo> mpValIdxSums = new TreeMap<Integer,SumInfo>();

      for(int i = 0; i < ai.length; i++)  {
         //j + 1: So we don't test the same combination twice.
         for(int j = i + 1; j < ai.length; j++)  {
            int iSum = ai[i] + ai[j];

            if(mpValIdxAll.containsKey(iSum))  {
               //The all-map contains the sum, so add it to the sum-map
               mpValIdxSums.put(mpValIdxAll.get(iSum), new SumInfo(ai[i], i, ai[j], j));
            }
         }
      }

      Set<Integer> stSumIdxs = mpValIdxSums.keySet();
      Iterator<Integer> itrSumIdxs = stSumIdxs.iterator();
      while(itrSumIdxs.hasNext())  {
         int iIdxSum = itrSumIdxs.next();
         SumInfo si = mpValIdxSums.get(iIdxSum);
         System.out.println(ai[iIdxSum] + " (element " + iIdxSum + ")  is the sum of elements " + si.iA + " (idx=" + si.iIdxA + ") and " + si.iB + " (idx=" + si.iIdxB + ")");
      }
   }
}
//The two elements that are a sum of another element
class SumInfo  {
   public final int iA;
   public final int iIdxA;
   public final int iB;
   public final int iIdxB;
   public SumInfo(int i_addendA, int i_ndexA, int i_addendB, int i_ndexB)  {
      iA = i_addendA;
      iIdxA = i_ndexA;
      iB = i_addendB;
      iIdxB = i_ndexB;
   }
}
[C:\java_code\]java FindElementsThatAreSumsOfOthers
589 (element 4)  is the sum of elements 137 (idx=2) and 452 (idx=6)
61 (element 5)  is the sum of elements 10 (idx=0) and 51 (idx=1)