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