fool puzzle four
Time Limit:
1000 ms | Memory Limit: 65535 KB
Difficulty:
3
- describe
-
These days, the idiot has been researching stocks. After research, he finally found the stock pattern of xxx company. What is even more gratifying is that the idiot calculated the daily stock price of this company, in order to prevent others from discovering his secrets. He decided to buy at most one share of the company, and now the idiot has listed the price. (This is no longer a fool's problem, he has solved it hehe). I just want to make things difficult for you. From the stock price list, can you calculate the maximum amount of money a fool can make per share?
- enter
-
The first line is an n, which means n days (less than 100000)
The second line gives the price of each share for n days - output
- How much money can be earned per share
- sample input
-
4 947 267 359 771 7 669 735 322 794 397 565 181
- Sample output
-
504 472
1 /* * 2 Analysis: 3 Ⅰ, traverse 4 from left to right Ⅱ, find the smallest number before pos 5 Ⅲ, find max (A [pos] - my_min, ans); 6 7 Core code: 8 while (n - -) { 9 scanf ("%d", &num); 10 if (num < my_min) my_min = num; 11 else { 12 ans = max (ans, num - my_min); 13 } 14 } 15 * */
C/C++ code implementation (AC):
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int n; 6 7 int main () { 8 9 while (~scanf ("%d", &n)) { 10 int num, ans = 0, my_min; 11 scanf ("%d", &my_min); 12 -- n; 13 while (n --) { 14 scanf ("%d", &num); 15 if (num < my_min) { 16 my_min = num; 17 } else { 18 ans = max (ans, num - my_min); 19 } 20 } 21 printf ("%d\n", ans); 22 } 23 }