Java 返回构成和的数组元素
我编写了下面的代码,以查看数组中是否有2个数字占一个和。我不知道如何捕捉构成该总和的元素。有什么想法吗 示例={1,11,4,7,8,10}总和=21计数=2 此代码返回true或false,但不捕获构成总和的数字。我该怎么做Java 返回构成和的数组元素,java,Java,我编写了下面的代码,以查看数组中是否有2个数字占一个和。我不知道如何捕捉构成该总和的元素。有什么想法吗 示例={1,11,4,7,8,10}总和=21计数=2 此代码返回true或false,但不捕获构成总和的数字。我该怎么做 public static boolean isSum(int[] a,int val,int count,int index){ if(count == 0 && val ==0){ return true; }
public static boolean isSum(int[] a,int val,int count,int index){
if(count == 0 && val ==0){
return true;
}
if(index>=a.length)
return false;
else{
return isSum(a,val-a[index],count-1,index+1)||isSum(a,val,count,index+1);
}
}
我感谢下面列出的所有漂亮的解决方案。在早上的时候,他四处奔波,找到了一种优雅的方法来解决这个问题,可以计算出任意数量的元素的总和。我只是想在这里分享解决方案供您评论
public class IsSum {
static ArrayList<Integer> intArray;
public static void main(String[] args) {
// TODO code application logic here
int[] a = {1,44, 4, 7, 8, 10};
intArray = new ArrayList<Integer>();
if (isSum(a,54,2, 0)) {
System.out.println("Is Present");
}
Iterator<Integer> arrayIter = intArray.iterator();
while (arrayIter.hasNext()) {
System.out.println(arrayIter.next());
}
}
public static boolean isSum(int[] a, int val, int count, int index) {
if (count == 0 && val == 0) {
return true;
}
if (index >= a.length) {
return false;
} else {
if (isSum(a, val - a[index], count - 1, index + 1)) {
intArray.add(a[index]);
return true;
} else {
return isSum(a, val, count, index + 1);
}
}
}
公共类问题{
静态阵列列表;
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
int[]a={1,44,4,7,8,10};
intArray=newarraylist();
如果(isSum(a,54,2,0)){
System.out.println(“存在”);
}
迭代器arrayIter=intArray.Iterator();
while(arrayIter.hasNext()){
System.out.println(arrayIter.next());
}
}
公共静态布尔ISUM(int[]a,int val,int count,int index){
if(count==0&&val==0){
返回true;
}
如果(索引>=a.length){
返回false;
}否则{
如果(isSum(a,val-a[索引],计数-1,索引+1)){
添加(a[索引]);
返回true;
}否则{
返回问题(a、val、计数、索引+1);
}
}
}
}修补程序有点难看-但它可以工作,如果有这两个元素-结果将返回数组中这些元素的索引:
public static int[] isSum(int[] a,int val,int count,int index, int[] arr){
int[] res = new int[2];
if(count == 0 && val ==0){
return arr;
}
else if(index >=a.length || count == 0) {
return res;
}
else{
res[0] = arr[0];
res[1] = arr[1];
if(count==1){
arr[1] = index;
}
else{
arr[0] = index;
}
int[] s1 = isSum(a,val-a[index],count-1,index+1, arr);
int[] s2 = isSum(a,val,count,index+1, res);
res = (s1[1] != 0 ? s1 : s2);
}
return res;
}
public static void main(String...args){
int[] a = {1,11,4,7,8,10};
int[] s = new int[2];
int [] res = isSum(a, 21, 2, 0, s);
System.out.println("result: "+(res[1] != 0));
if((res[1] > 0)){
System.out.print(res[0]+" "+res[1]);
}
}
输出
result: true
1 5
另一种(更优雅的)方式:
公共静态int[]isSum(int[]a,int val){
int[]res=新的int[2];
对于(inti=0;i,必须将返回类型更改为(例如)数组,然后
您必须捕获返回的结果,而不是直接返回它,如果是真的,则通过给定的索引捕获值,然后在数组中返回它们,如果是假的,则返回空数组……测试返回数组的值,您将知道它是真是假,如果是真的,还将提供您需要的值
更新:
public static int[] isSum(int[] a, int val, int count, int index) {
int[] results = new int[2];
results[0] = -1;
results[1] = -1;
if (count == 0 && val == 0) {
results[0] = 0;
results[1] = 0;
return results;
}
if (index >= a.length)
return results;
else {
if (isSum(a, val - a[index], count - 1, index + 1) == results) {
if (isSum(a, val, count, index + 1) == results) {
return results;
} else {
results[0] = val;
results[1] = a[index + 1];
return results;
}
} else {
results[0] = val - a[index];
results[1] = a[index + 1];
return results;
}
}
}
/**
*类用于保存结果详细信息。
*/
私有静态类结果{
布尔ISUM;
int[][]对;
国际noOfPairs;
结果(int值){
pairs=新整数[值][2];
}
}
公共静态结果isSum(int[]数组、int val、int count){
结果结果=新结果(计数);
int指数=0;
for(int i=0;i
公共静态布尔ISUM(int[]arr,int val){
int a,b=0;
int c=0,d=0;
布尔bol=假;
对于(a=0;a)你所说的“两个数字的总和”是什么意思?你能举个例子吗?我不明白你的意思。我添加了一个例子。很抱歉,你想要的是构成和的数字吗?即如果和=21,那么它必须返回10和11。这就是你想要的吗?如果我在签名中传递计数,它将不会这样做。示例-a[]={1,3,4,10}Signature-isSum(a,15,2,0)@SindhujaNarasimhan您确实通过了签名中的计数-但是您没有添加检查以确保它不大于2(在这种情况下,您应该返回false
oc)将上面发布的示例更改为:example={1,12,4,7,8,10}Sum-21。它仍然会返回true,因为:4+7+10=21为什么它符合bug的条件?如果我坚持用这种方式写签名,我可以传递任何计数值,并返回计算该计数的元素。如果不太麻烦的话,你能在代码中显示示例吗?谢谢!
public static int[] isSum(int[] a, int val, int count, int index) {
int[] results = new int[2];
results[0] = -1;
results[1] = -1;
if (count == 0 && val == 0) {
results[0] = 0;
results[1] = 0;
return results;
}
if (index >= a.length)
return results;
else {
if (isSum(a, val - a[index], count - 1, index + 1) == results) {
if (isSum(a, val, count, index + 1) == results) {
return results;
} else {
results[0] = val;
results[1] = a[index + 1];
return results;
}
} else {
results[0] = val - a[index];
results[1] = a[index + 1];
return results;
}
}
}
/**
* Class used to hold the result details.
*/
private static class Result {
boolean isSum;
int[][] pairs;
int noOfPairs;
Result(int value) {
pairs = new int[value][2];
}
}
public static Result isSum(int[] array, int val, int count) {
Result result = new Result(count);
int index = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
//check if pair values add to given sum
if (array[i] + array[j] == val) {
int[] temp = new int[2];
temp[0] = array[i];
temp[1] = array[j];
result.pairs[index++] = temp;
result.noOfPairs++;
count--;
if (count == 0) {
//we got required no of pairs..now exit
result.isSum = true;
return result;
}
}
}
}
return result;
}
public static boolean isSum(int[] arr,int val){
int a,b=0;
int c=0,d=0;
boolean bol=false;
for(a=0;a<arr.length;a++)
{
b=a+1;
while(b<arr.length)
{
System.out.println(a+" "+b);
if(arr[a]+arr[b]==val)
{
System.out.println(arr[a]+arr[b]);
bol=true;
c=a;
d=b;
break;
}
else
b++;
}
}
System.err.println(c+" "+d);
return bol ;
}
}