Advertisement
kokokozhina

tproger91

Dec 22nd, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. //the highest product of three(negative numbers included)
  4. using namespace std;
  5.  
  6. int fhighest(int current, int highest, int i)
  7. {
  8.     if (i == 0)
  9.         highest = current;
  10.     else
  11.         highest = max(current, highest);
  12.     return highest;
  13. }
  14.  
  15. int flowest(int current, int lowest, int i)
  16. {
  17.     if (i == 0)
  18.         lowest = current;
  19.     else
  20.         lowest = min(current, lowest);
  21.     return lowest;
  22. }
  23.  
  24. int fhighest_product_of_two(int highest_product_of_two, int current, int highest, int lowest, int i)
  25. {
  26.     if (i == 1)
  27.         highest_product_of_two = max(current * highest, current * lowest);
  28.     else
  29.         if (i > 1)
  30.             highest_product_of_two = max(highest_product_of_two, max(current * highest, current * lowest));
  31.     return highest_product_of_two;
  32. }
  33.  
  34. int flowest_product_of_two(int lowest_product_of_two,int current, int highest, int lowest, int i)
  35. {
  36.     if (i == 1)
  37.         lowest_product_of_two = min(current * highest, current * lowest);
  38.     else
  39.         if (i > 1)
  40.             lowest_product_of_two = min(lowest_product_of_two, min(current * highest, current * lowest));
  41.     return lowest_product_of_two;
  42. }
  43.  
  44. int fhighest_product_of_three(int highest_product_of_three, int current, int highest_product_of_two, int lowest_product_of_two, int i)
  45. {
  46.     if (i == 2)
  47.         highest_product_of_three = max(current * highest_product_of_two, current * lowest_product_of_two);
  48.     else
  49.         if (i > 2)
  50.             highest_product_of_three = max(highest_product_of_three, max(current * highest_product_of_two, current * lowest_product_of_two));
  51.     return highest_product_of_three;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     int current;
  58.     int highest_product_of_three = 0;
  59.     int highest_product_of_two = 0;
  60.     int highest = 0;
  61.     int lowest_product_of_two = 0;
  62.     int lowest = 0;
  63.     int i = 0;
  64.     while(cin >> current)
  65.     {
  66.         highest_product_of_three = fhighest_product_of_three(highest_product_of_three, current, highest_product_of_two, lowest_product_of_two, i);
  67.         highest_product_of_two = fhighest_product_of_two(highest_product_of_two, current, highest, lowest, i);
  68.         lowest_product_of_two = flowest_product_of_two(lowest_product_of_two, current, highest, lowest, i);
  69.         lowest = flowest(current, lowest, i);
  70.         highest = fhighest(current, highest, i);
  71.         i++;
  72.     }
  73.  
  74.     cout << highest_product_of_three << endl;
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement