UVA problem id :1428- Closest Sums

/*url:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1428*/ #include <bits/stdc++.h> using namespace std; int main() { int n,r,i=0,j=0,k=0,t,min,result=1,q,x,c=0; std::vector<int>vect ; std::vector<int>sum ; cin>>n; while(n) { c++; cout<<"Case "<<c<<":"<<endl; for(i=0;i<n;i++){ cin>>x; vect.push_back(x); } for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++){ sum.push_back(vect[i]+vect[j]); } } cin>>q; min=INT_MAX; while(q--){ cin>>x; for(i=0;i<sum.size();i++){ t=abs(sum[i]-x); if(min>t){ min=t; result=sum[i]; } } cout<<"Closest sum to "<<x<<" is "<<result<<"."<<endl; min=INT_MAX; } cin>>n; vect.clear(); sum.clear(); } 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.