ACM (UVa) : 11057


#include
#include

using namespace std;

int main ()
{
int length;
int a [10050];

while ( cin >> length ) {

int i, j; // iterator
for ( i = 0; i < length; i++ ) cin >> a [i];

sort ( a, a + length);

int number;
cin >> number;

i = 0;
j = length – 1;
int first;

while ( i < j ) { while ( j > i ) {
if ( a [i] + a [j] == number ) {
first = a [i];
j–;
break;
}

else if ( a [i] + a [j] < number ) break; j--; } i++; } cout << "Peter should buy books whose prices are " << first << " and " << number - first << ".\n" << endl; } return 0; } [/sourcecode]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s