201412-3

学到了没有确定输入数目,但使用回车后,使用ctrl+z,再回车就可结束输入。这里输入的是字符串,输入除规定以外的就break。
只有30分,真不知道哪里错了

#include <bits/stdc++.h>
using namespace std;
struct Record
{
    int type;//buy 为0,sell 为 1
    long long num;//股数
    float money;//成交价
    bool cancel;
    Record(int typee,long long numm,float moneyy=0.0,bool cancell=false):type(typee),num(numm),money(moneyy),cancel(cancell){}
};
vector <Record> records;
bool cmp(int a,int b)
{
    return records[a].money>records[b].money;
}
int main()
{
  int i=0;
  vector <int> buy,sell;
  string s;
  long long buys=0,sells=0;
  long long t=0,maxx=0;
  int ans=0;
  long long b;
  float a;
  int j=0;
  long long sum1=0,sum2=0;
  while(cin>>s)
  {
      if(s=="buy")
      {
          cin>>a>>b;
          records.push_back(Record (0,b,a));
      }
      else if(s=="sell")
      {
          cin>>a>>b;

           records.push_back(Record (1,b,a));
      }
      else if(s=="cancel")
      {
          cin>>b;
          records[b-1].cancel=true;
      }
      else//这个可以省略
        break;
  }
  for(i=0;i<records.size();i++)
  {
      if(records[i].cancel==false&&records[i].type==0)
        buy.push_back(i);
      else if(records[i].cancel==false&&records[i].type==1)
       {
        sell.push_back(i);
        sells=sells+records[i].num;
       }
  }
  sort(buy.begin(),buy.end(),cmp);//数量从大到小排序
  sort(sell.begin(),sell.end(),cmp);
  for(i=0;i<buy.size();i++)
  {
      buys=buys+records[buy[i]].num;
      for(;j<sell.size();j++)
      {
          if(records[sell[j]].money>records[buy[i]].money)
            sells=sells-records[sell[j]].num;
         else
            break;
      }
      t=min(buys,sells);
      if(t>maxx)   {maxx=t; ans=i;}
  }
  printf("%.2f %lld",records[buy[ans]].money,maxx);
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值