UVA Problem Id : 11057 Exact Sum

// URL:https://uva.onlinejudge.org/external/110/11057.pdf #include <bits/stdc++.h> using namespace std; int main() { int N,M,i,j,k,price; while(cin>>N){ std::vector<int>v ; for(i=0;i<N;i++){ cin>>price; v.push_back(price); } cin>>M; int diff=-1,Book1=0,Book2=0; for(i=0;i<N;i++){ if(v[i]<M){ for(j=i+1;j<N;j++){ if(v[i]+v[j]==M){ if(diff<0|| abs(v[i]-v[j])<diff){ diff=abs(v[i]-v[j]); if(v[i]<=v[j]) { Book1=v[i]; Book2=v[j]; } else{ Book2=v[i]; Book1=v[j]; } } } } } } cout<<"Peter should buy books whose prices are "<<Book1<<" and "<<Book2<<"."<<endl<<endl; } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.