(C语言版)Aizu ALDS1_1_D --- Maximum Profit 最大利润求法

Maximum Profit
You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen.

Write a program which reads values of a currency Rt at a certain time t (t=0,1,2,…n−1), and reports the maximum value of Rj−Ri where j>i .

Input
The first line contains an integer n. In the following n lines, Rt (t=0,1,2,…n−1) are given in order.

Output
Print the maximum value in a line.

Constraints
2≤n≤200,000
1≤Rt≤109
Sample Input 1
6
5
3
1
3
4
3
Sample Output 1
3

用c语言写的 代码如下

 #include<stdio.h>

int main(int argc, char const *argv[])
{
    /* code */
    // 录入七个数据 6 5 3 1 3 4 3
	int n,str[200005]; //数组长度一定要很大,题目有要求
	scanf("%d",&n);
	for(int i=0; i<n;i++) {
		scanf("%d",&str[i]);
	}
	/*
	1.定义最小值,最大差值
	2.找出最小值与最大差值相减
	*/
	int minNum = str[0];
	int maxNum = str[1] - str[0]; 
	
	for(int j=1;j<n;j++) {
	 
		if((str[j] - minNum) > maxNum) maxNum = str[j] - minNum;
		if(str[j] < minNum) minNum = str[j];
		
	}
	printf("%d\n",maxNum);
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值