如果给定否,我该如何打印。大于数组然后不存在这样的对 [英] How do I print if given no. Is greater than array then no such pair exists

查看:49
本文介绍了如果给定否,我该如何打印。大于数组然后不存在这样的对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序正在打印总和等于给定数量的所有对。如果不存在这样的对,那么我如何显示它不会这样做的消息。如果我把它放在else部分,那么相同的消息是打印arr.length次。任何人都可以帮我显示一条消息 - 没有这样的对存在如果没有。是否大于或小于给定数组的总和一次?任何帮助将不胜感激。



我尝试过:



the following program is printing all pairs whose sum is equal to a given no. If no such pairs exist then how do i display a message that it doesn't do so.If i put it in the else part then same message is printing arr.length times.Anyone can help me to display a message-"no such pairs exist" if the no. is greater or less than the sum of given array only once?Any help would be appreciated.

What I have tried:

public class pairsum {
	
	public static void arri(int arr[],int n)
	{  int first=0,second=0;
		for(int a=0;a<arr.length;a++)
		{	 first=arr[a];
			for(int b=a+1;b<arr.length;b++)
			{	second=arr[b];
				if(first+second==n)
					System.out.printf("(%d, %d)%n",first,second);		
			}
		}	
	}
	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int arr[]=new int[5];
		System.out.println("enter integer array\n");
		for(int i=0;i<arr.length;i++)
			arr[i]=s.nextInt();
		System.out.println("enter the no. to find sum\n");
		int n=s.nextInt();
		arri(arr,n);
			
	}
}

推荐答案

虽然编写的算法很差,但我不会谈论它。 :)



只是那样,你可以让函数打印出数组中的值与函数本身内部提供的值不相加,如果迭代已经完成运行。像这样,

Although the algorithm written is very poor, but I will not be talking about it. :)

Just the thing that, you can have the function print that the values in the array do not sum to the value provided, inside the function itself, if the iterations have finished running. Like this,
public static void arri(int arr[],int n)
{
    int first=0,second=0;
    for(int a=0;a<arr.length;a++) {
        first=arr[a];
        for(int b=a+1;b<arr.length;b++) {
            second=arr[b];
            if(first + second == n) {
                System.out.printf("(%d, %d)%n", first, second);
                return; // Return out of the function.
            }
        }
    }

    // Program reacher here, because the loop didn't find any combination. 
    System.out.println("no such pairs exist");
}



提示:您可以通过使用不同的方法来改进函数,例如,使用一个索引并沿着数组的开头,并求和它与数组中的最后一个元素,看看你是否得到它。这适用于排序数组。对于未分类的数字,您可以尝试保存您看到的数字,并在上一个列表中找到该数字。这将改进当前算法从O(N 2 )到O(N)。



在学术界,数据结构,算法和细化很重要,请不要跳过它们,否则找工作将比这个简单的问题更难。 :-)只是我的2美分。


Tip: You can improve the function by using a different approach, for example, use one index and follow along the start of the array, and sum it with the last element in the array to see if you got it. This would work with sorted arrays. In the case of an unsorted one, you can try saving the numbers you have seen and finding it you have that number available in the previous list. This will improve the current algorithm from O(N2) to O(N).

In academics, data structures, algorithms and refinement matter a lot, please do not skip them, otherwise getting a job will be more difficult than this simple problem. :-) Just my 2 cents.


引用:

如果给定否则我如何打印。大于数组然后不存在这样的对

How do I print if given no. Is greater than array then no such pair exists



答案隐藏在语句中。


The answer is hidden in the statement.

Quote:

以下程序打印所有总和等于给定的对。如果不存在这样的对,那么我如何显示它不这样做的消息

the following program is printing all pairs whose sum is equal to a given no. If no such pairs exist then how do i display a message that it doesn't do so



你什么时候才能知道找不到对?

- 检查结束后。

怎么知道有没有对?

- 设置一个计数器,并在找到它们时计算它们。


When can you know that no pair was found ?
- After the check is finished.
How to know that there was some pairs or not ?
- Set a counter and count them as you find them.

public static void arri(int arr[],int n)
{  int first=0,second=0;
    // set the pairs counter here
    for(int a=0;a<arr.length;a++)
    {    first=arr[a];
        for(int b=a+1;b<arr.length;b++)
        {   second=arr[b];
            if(first+second==n) // This is where you know that a pair is found
                System.out.printf("(%d, %d)%n",first,second);
        }
    }
    // end of pairs check is here
}


这篇关于如果给定否,我该如何打印。大于数组然后不存在这样的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆