UVA Problem 11057 (Exact Sum) Solution

Problem Solving, UVa

#include <bits/stdc++.h>

using namespace std;



int main()

{

    int v[10010];

    //vector<int>v;

    int i,j,n,k,max,price1,a,price2,m,diff,tmp;



    while(cin>>n)

    {

        for(i=0;i<n;i++)

            cin>>v[i];

            //v.push_back(i);

        cin>>m;



        max=1000000;

        for(i=0;i<n-1;i++)

            for(j=i+1;j<n;j++)

            {

                if((v[i]+v[j])== m)

                {

                    diff=abs(v[i]-v[j]);

                        if(diff<max)

                        {

                            max=diff;

                            price1=v[i];

                            price2=v[j];

                        }

                }

            }

        if(price1>price2)

        {

            tmp=price1;

            price1=price2;

            price2=tmp;

        }

        printf("Peter should buy books whose prices are %d and %d.\n\n",price1,price2);

    }

    return 0;

}

 

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...