Sword refers to the offer series source code-and for s two numbers

Source: Internet
Author: User

Topic 1352: And for the two digital time limit for S: 2 seconds Memory limit: 32 trillion special Subject: No submission: 2651 Resolution: 668 Title Description: Enter an array of ascending sort and a number s, look for two numbers in the array, and yes their and exactly s, if there are many pairs of numbers and equals S, The product of the output two number is the smallest one. Input: Each test case consists of two lines: The first line contains an integer n and k,n represents the number of elements in the array, and K represents the sum of two numbers. where 1 <= n <= 10^6,k is an int the second row contains n integers, and each array is of type int. Output: corresponding to each test case, output two number, small first output. Output "1-1" sample input if not found: 6 151 2 4 7 11 15 Sample output: 4 11

#include <iostream> #include <stdio.h>using namespace Std;int main () {    int n,sum;    while (scanf ("%d%d", &n,&sum)!=eof) {        int* data = new Int[n];        for (int i=0;i<n;i++) {            scanf ("%d", &data[i]);        }        int* begin = Data;        int* end = data+n-1;        BOOL found = false;        while (begin<end) {            int s = *begin+*end;            if (s==sum) {                found = true;                break;            } else if (s<sum) {                begin++;            } else{                end--;            }        }        if (found) {            printf ("%d%d\n", *begin,*end);        } else{            printf (" -1-1\n");        }    }    return 0;}

Sword refers to the offer series source code-and for s two numbers

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.