学到了没有确定输入数目,但使用回车后,使用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;
}