[Algorithm C ++] checks whether the sum of two numbers in an array is equal to a certain number.

Source: Internet
Author: User

[Algorithm C ++] checks whether the sum of two numbers in an array is equal to a certain number.

Problem:
Checks whether the sum of two numbers in the array is equal to a certain number.

Solution 1: first sort the array and then traverse from the two ends
After the array is sorted, the minimum value is obtained from the left end and the maximum value is obtained from the right end,
Determine the sum of the two and the size of the target:
1. If it is equal to, two numbers are output;
2. If the value is greater than 2nd, move the right side to continue the judgment;
3. When the value is smaller than 2nd, the left end is moved to. Continue to judge.

# Include
  
   
# Include
   
    
# Include using namespace std; void fun1 (int a [], int length, int target) {// sort the array by sort (a, a + length ); // left is the minimum value, right is the maximum value int left = 0, right = length-1; while (left <right) {int tmp = a [left] + a [right]; if (tmp = target) {cout <a [left] <a [right] <endl; return;} else if (tmp> target) {// and greater than the target, right is reduced, and right --;} else {// and is smaller than the target, left is increased, so as to increase and left ++ ;}}cout <none <endl ;}int main () {int a [] = {1, 3, 2, 7, 6, 9, 8, 0, 5, 4}; int target = 0; while (cin> target) {fun1 (a, 10, target );}}
   
  

Although the above method is simple, it has some drawbacks and cannot output all two numbers equal to the target value.

Solution 2:
Brute force solution: record the sum of each number and other number in a two-dimensional array and traverse it. In this way, we can record all and equal to the value pairs of the target value, as shown below:
Assume that the input array is 2 3 4 5 1.
The following matrix is available:

  2 3 4 5 1
2 - 5 6 7 3
3 5 - 7 8 4
4 6 7 - 9 5
5 7 8 9 - 6
1 3 4 5 6 -

When the target value is 7, there are two groups:
(5, 2) and (3, 4)
Considering symmetry, n [I] [j] = n [j] [I], I! = J,
Therefore, we only need the value pair of I> j.

void fun2(int a[], int length, int target) {    int** n = new int*[length];    for (int i = 0; i < length; i++) {        n[i] = new int[length];    }    for (int i = 0; i < length; i++) {        for (int j = length - 1; j > i; j--) {            n[i][j] = n[j][i] = a[i] + a[j];        }    }    for (int i = 0; i < length; i++) {        for (int j = length - 1; j > i; j--) {            if (n[i][j] == target) {                cout << a[i] <<   << a[j] << endl;            }        }    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.