首页 > 网易互娱笔试

网易互娱笔试

1.自动售货机  为何自己本地都通过 提交却是0????
int main()
{
    int N,M;
    cin>>N>>M;//货物槽数量 操作人数
    
    vector<vector<int> >v;//货物槽
    vector<int>price(N+1,0);
    for(int i=1;i<=N;i++)
      cin>>price[i];
    //初始化货物槽
    for(int i=0;i<=N;i++)
    {
        vector<int>temp(900,price[i]);
        v.push_back(temp);
    }
    //操作
    for(int i=0;i<M;i++)
    {
        vector<string>op;//操作
        int op_count;
        cin>>op_count;
        string str1;
        getline(cin,str1);
        string str;
        vector<string>temp;
        for(int j=0;j<op_count;j++)
        {
            getline(cin,str);
            op.push_back(str);
        }
        int left=0,right=0,res=0;
        //遍历一个人的操作
        for(int index=0;index<op.size();index++)
        {
            vector<string>word;
            int cur=0;
            string ss="";
            while(cur<op[index].size())
            {
                if(op[index][cur]!=' ') ss.push_back(op[index][cur]);
                else 
                {
                  word.push_back(ss);
                  ss="";
                }
                cur++;
            }
            word.push_back(ss);
            if(word.size()==3)
            {
                int temp_index=word[2][0]-'0';
                if(word[0]=="left" && word[1]=="take") 
                {
                    left=v[temp_index][v[temp_index].size()-1];//取栈顶
                    v[temp_index].pop_back();
                }
                if(word[0]=="right" && word[1]=="take")
                { 
                    right=v[temp_index][v[temp_index].size()-1];
                    v[temp_index].pop_back();
                }
                if(word[0]=="left" && word[1]=="return") 
                {
                    v[temp_index].push_back(left);
                    left=0;
                }
                if(word[0]=="right" && word[1]=="return") 
                {
                    v[temp_index].push_back(right);
                    right=0;
                }
            }
            else if(word.size()==2)
            {
                if(word[0]=="left" && word[1]=="keep")
                {
                    res=res+left;
                    left=0;
                }
                if(word[0]=="right" && word[1]=="keep")
                {
                    res=res+right;
                    right=0;
                }
            }
        }
        cout<<res+left+right<<endl;  
    }
}


全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐