Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- //the highest product of three(negative numbers included)
- using namespace std;
- int fhighest(int current, int highest, int i)
- {
- if (i == 0)
- highest = current;
- else
- highest = max(current, highest);
- return highest;
- }
- int flowest(int current, int lowest, int i)
- {
- if (i == 0)
- lowest = current;
- else
- lowest = min(current, lowest);
- return lowest;
- }
- int fhighest_product_of_two(int highest_product_of_two, int current, int highest, int lowest, int i)
- {
- if (i == 1)
- highest_product_of_two = max(current * highest, current * lowest);
- else
- if (i > 1)
- highest_product_of_two = max(highest_product_of_two, max(current * highest, current * lowest));
- return highest_product_of_two;
- }
- int flowest_product_of_two(int lowest_product_of_two,int current, int highest, int lowest, int i)
- {
- if (i == 1)
- lowest_product_of_two = min(current * highest, current * lowest);
- else
- if (i > 1)
- lowest_product_of_two = min(lowest_product_of_two, min(current * highest, current * lowest));
- return lowest_product_of_two;
- }
- int fhighest_product_of_three(int highest_product_of_three, int current, int highest_product_of_two, int lowest_product_of_two, int i)
- {
- if (i == 2)
- highest_product_of_three = max(current * highest_product_of_two, current * lowest_product_of_two);
- else
- if (i > 2)
- highest_product_of_three = max(highest_product_of_three, max(current * highest_product_of_two, current * lowest_product_of_two));
- return highest_product_of_three;
- }
- int main()
- {
- int current;
- int highest_product_of_three = 0;
- int highest_product_of_two = 0;
- int highest = 0;
- int lowest_product_of_two = 0;
- int lowest = 0;
- int i = 0;
- while(cin >> current)
- {
- highest_product_of_three = fhighest_product_of_three(highest_product_of_three, current, highest_product_of_two, lowest_product_of_two, i);
- highest_product_of_two = fhighest_product_of_two(highest_product_of_two, current, highest, lowest, i);
- lowest_product_of_two = flowest_product_of_two(lowest_product_of_two, current, highest, lowest, i);
- lowest = flowest(current, lowest, i);
- highest = fhighest(current, highest, i);
- i++;
- }
- cout << highest_product_of_three << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement