UVa101-The Blocks Problem/STL-Vector的使用

取出指令共同点,减少重复代码

利用STL中不定长数组vector

vector<int> a;//<double>也可以,这里a是构建了一个不定长一维数组,a[maxn]也可以,构建一个二维数组

a.size() //a的长度

a.push_back(b);//向a的尾部添加元素b,应该是不能加一串只加单个元素

a.resize(h);//将a的第h-1后的元素删掉,in other words, 保留前h个元素(notice:数组从0开始,第h个元素其数组中下标是h-1)

a.pop_back();//删除最后一个元素

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn=30;
int n;
vector<int> pile[maxn];
void found(int a,int &p,int &h)
{
    for(p=0;p<n;p++)
        for(h=0;h<pile[p].size();h++)
            if(pile[p][h]==a)
                return;

}
void clear_above(int p,int h)
{
    for(int l=h+1;l<pile[p].size();l++)
        pile[pile[p][l]].push_back(pile[p][l]);
    pile[p].resize(h+1);
}
void move_above(int pa,int h,int pb)
{
    for(int l=h;l<pile[pa].size();l++)
        pile[pb].push_back(pile[pa][l]);
    pile[pa].resize(h);
}
void printf()
{
    for(int k=0;k<n;k++){
        printf("%d:",k);
        for(int l=0;l<pile[k].size();l++)
            printf(" %d",pile[k][l]);//printf("%s",pile[k])是不行的哦
            cout<<endl;
    }
}
int main()
{
    cin>>n;
    int a,b;
    string s1,s2;
    for(int i=0;i<n;i++)
        pile[i].push_back(i);
    while(cin>>s1){
        if(s1=="quit")
            break;
        cin>>a>>s2>>b;
        int pa,pb,ha,hb;
        found(a,pa,ha);
        found(b,pb,hb);
        if(pa==pb) continue;
        if(s1=="move") clear_above(pa,ha);
        if(s2=="onto") clear_above(pb,hb);
        move_above(pa,ha,pb);
    }
    printf();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
-------------------------------------------- C++ Call Stacks (More useful to developers): -------------------------------------------- Windows not support stack backtrace yet. ------------------------------------------ Python Call Stacks (More useful to users): ------------------------------------------ File "/data/yourenchun/share/projects/renzheng/liuyi/liuyi_env/Python-2.7.14/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1843, in append_op attrs=kwargs.get("attrs", None)) File "/data/yourenchun/share/projects/renzheng/liuyi/liuyi_env/Python-2.7.14/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/data/yourenchun/share/projects/renzheng/liuyi/liuyi_env/Python-2.7.14/lib/python2.7/site-packages/paddle/fluid/layers/nn.py", line 9597, in relu6 attrs={'threshold': threshold}) File "/data/yourenchun/share/projects/renzheng/slurm/dist_arcface/models/mnasnet.py", line 171, in conv_bn_layer return fluid.layers.relu6(bn) File "/data/yourenchun/share/projects/renzheng/slurm/dist_arcface/models/mnasnet.py", line 198, in inverted_residual_unit name=name + '_expand') File "/data/yourenchun/share/projects/renzheng/slurm/dist_arcface/models/mnasnet.py", line 252, in invresi_blocks name=name + '_' + str(i + 1)) File "/data/yourenchun/share/projects/renzheng/slurm/dist_arcface/models/mnasnet.py", line 110, in net name='conv' + str(i)) File "test.py", line 107, in run emb = model.net(image) File "test.py", line 318, in main testor.run() File "test.py", line 323, in <module> main() ---------------------- Error Message Summary: ---------------------- Error: Fail to allocate CPU memory: size = 114987136 . [Hint: p should not be null.] at (E:\Paddle\paddle\fluid\memory\detail\system_allocator.cc:61) [operator < relu6 > error]
最新发布
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值