题解 | #买卖股票的最好时机(二)#

买卖股票的最好时机(二)

https://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281

#include <stdio.h>
#include <stdlib.h>
int num[100005];
int max(int x,int y)
{
    if(x>y) return x;
    else return y;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
    }
    int a=0,max1=0;
    for(int x=0;x<n;x++)
    {
        if(num[x]<num[a]){
            a=x;
        }
        else if(num[x]>num[a])
        {
            max1=max1+num[x]-num[a];
            a=x;
        }
    }
    printf("%d",max1);
    return 0;
}

全部评论

相关推荐

码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务