Sunday, 18 November 2018

uva problem 11057 Exact Sum solution

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int arr[10005];
  6.    int n,m,mi,d;
  7.    while(cin>>n){
  8.         mi=1000001;
  9.         int x,y;
  10.    for(int i=0;i<n;i++){
  11.     cin>>arr[i];
  12.    }
  13.    cin>>m;
  14.     for(int i=0;i<n-1;i++){
  15.         for(int j=i+1;j<n;j++){
  16.             if((arr[i]+arr[j])==m){
  17.                     d=(arr[i]>arr[j]?arr[i]-arr[j]:arr[j]-arr[i]);
  18.             if(d<mi){
  19.                 mi=d;
  20.                 x=arr[i];
  21.                 y=arr[j];
  22.             }
  23.             }
  24.         }
  25.     }
  26.     if(x>y){
  27.         int t;
  28.         t=x;
  29.         x=y;
  30.         y=t;
  31.     }
  32.     cout<<"Peter should buy books whose prices are "<<x<<" and "<<y<<".\n\n";
  33.    }
  34.  
  35.     return 0;
  36. }

No comments:

Post a Comment

variable declaration of c program

#include <stdbool.h> #include<bits/stdc++.h> #include <stdio.h> #include <string.h> #include <std...