zoukankan      html  css  js  c++  java
  • [编程题]股票交易日

    链接:https://www.nowcoder.com/questionTerminal/3e8c66829a7949d887334edaa5952c28
    来源:牛客网

    在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。请采用实践复杂度低的方法实现。

    给定价格序列prices及它的长度n,请返回最大收益。保证长度小于等于500。

    测试样例:
    [10,22,5,75,65,80],6
    代码:
    /*
    6 
    10 22 5 75 65 80
    */
    #include<iostream>
    using namespace std;
     int maxProfit(int prices[], int n)
     {
            // write code here
            int max=0;
            for(int i=1;i<=n-3;i++)
            {
                cout<<"这是第"<<i<<""<<endl;
                //A[i]为第一次卖出的股票价格
                 int lmax=prices[1]-prices[0];
                 int rmax=prices[i+2]-prices[i+1];
               cout<<"rmax=prices"<<i+2<<"-"<<"prices"<<i+1<<"   "<<prices[i+2]-prices[i+1]<<endl;
                 for(int j=0;j<=i;j++)
                {
                   for(int jj=j;jj<=i;jj++)
                   {
                       cout<<"L"<<"prices:"<<jj<<"-"<<"prices:"<<j<<"="<<prices[jj]-prices[j]<<endl;
                         if(lmax<prices[jj]-prices[j])
                         {
                         lmax=prices[jj]-prices[j];
                             cout<<"LMAX="<<lmax<<endl<<endl;
                         }   
                   }
                }
               
                cout<<"================分割线============="<<endl;
                for(int m=i+1;m<=n-1;m++)
                {
                    for(int mm=m;mm<=n-1;mm++)
                    {
                        cout<<"R"<<"prices:"<<mm<<"-"<<"prices:"<<m<<"="<<prices[mm]-prices[m]<<endl;
                        if(rmax<prices[mm]-prices[m])
                        {
                            rmax=prices[mm]-prices[m];
                            cout<<"RMAX="<<rmax<<endl<<endl;
                        }
                    }
                }
    
    
                cout<<"lmax="<<lmax<<"    "<<"rmax="<<rmax<<endl;
            cout<<max<<"     "<<lmax+rmax<<endl<<endl<<endl<<endl;
               if(max<lmax+rmax)
               {
                  max=lmax+rmax;
               }
            }
            return max;
     }
    int main()
    {
        int prices[1001];
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>prices[i];
    cout<<"The maxprofit is"<<maxProfit(prices,n)<<endl;
        return 0;
    }
    View Code
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    玉米不怕累,宇春最珍贵
    幸福
    谷歌位置搜索 蹩脚结合jquery.ui.gmap
    Resharp Format XML config
    正则表达取得 image src 中值
    一个Gif处理的类库
    使用nuGet管理自己的包
    正则表达式语言 快速参考
    动态加入JS及加入CSS
    检测是一个时间串或一个数字串是否连续
  • 原文地址:https://www.cnblogs.com/gcter/p/7286648.html
Copyright © 2011-2022 走看看