# 题目：输入一个已经按升序排序过的数组和一个数字，在数组中查找两个数，使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字，输出任意一对即可。

#include<iostream>
using namespace std;

int num[6] = {1,2,4,7,11,15};

int main()
{
int n = 6;
int sum;
scanf("%d", &sum);
int i = 0;
int j = n - 1;
int tmp;
bool solve = false;
while(i < j){
tmp = num[i] + num[j];
if(tmp == sum){
solve = true;
cout << num[i] << ' ' << num[j] << endl;
break;
}
else if(tmp < sum){
i ++;
}
else{
j --;
}
}
if(!solve){
cout << "there is not." << endl;
}
system("pause");
}

（无序数组）

void getsum(int num[], int n, int sum)
{
bool arr[high - low + 1];
memset(arr, false, sizeof(arr));

for(int i = 0; i < n; i ++){
arr[num[i] - low] = true;
}

int i = 0;
int j = high - low;
int tmp;
bool solve = false;
while(i < j){
while(i < j && !arr[i]){
i ++;
}
while(i < j && !arr[j]){
j --;
}
tmp = i + j;
if(tmp == sum){
solve  = true;
cout << i << ' ' << j << endl;
i ++;
j --;
}
else if(tmp < sum){
i ++;
}
else{
j --;
}
}
if(!solve){
cout << "There are not two numbers" << endl;
}
}
09-15 453