2021天梯赛赛后补题(L1-乘法口诀数列)(L2 包装机)(L2 病毒溯源)

L1最后一道 L2的前三道皆因为各种原因最后几个测试点出现段错误(也有部分测试点答案错误 )没有拿全部分数。。。
(L2最后一道其实也是段错误,只不过最后改过来了)
段错误段的我人都傻了
先说L1最后一道题 少考虑了乘法结果的末尾为0的情况
原题

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6+5;
int sum[maxn],n;
int main()
{
    cin>>sum[0]>>sum[1]>>n;
    for(int i=2,j=2; j<n; ++i)
    {
        int tmp=sum[i-1]*sum[i-2];
        if(tmp==0)///!!!!比赛的时候就因为这个!!
            sum[j++]=tmp;
        else
        {
            stack<int>t;
            while(tmp)
            {
                t.push(tmp%10);
                tmp/=10;
            }
            while(!t.empty())
            {
                sum[j++]=t.top();
                t.pop();
            }
        }
    }
    for(int i=0; i<n; ++i)
    {
        if(i==0)
        cout<<sum[i];
        else
            cout<<' '<<sum[i];
    }
    return 0;
}

L2第一道题
模拟题 注意各种限制条件
L2 包装机

#include <bits/stdc++.h>

using namespace std;
int n,m,s;
//char mp[105];
string ss[105];
int top[105];
char stk[105];
int sum;
string ans="";
int main()
{
    cin>>n>>m>>s;
    for(int i=1; i<=n; i++)
    {
        cin>>ss[i];
    }
    int x;
    while(cin>>x)
    {
        if(x==-1)break;
        if(x!=0)
        {
            if(top[x]==m)continue;
            if(sum==s)
            {
                ans+=stk[--sum];
            }
            stk[sum++]=ss[x][top[x]++];
        }
        else
        {
            if(sum>0)
                ans+=stk[--sum];
        }

    }
    cout<<ans<<endl;
    return 0;
}

L2第二道题
dfs 栈记录路径 注意第一个病毒不确定需要通过入度确定
L2 病毒溯源

#include <bits/stdc++.h>

using namespace std;

const int maxn=1e4+5;
vector<int>mp[maxn];
int n,m,S;
int in[maxn];

int stk[maxn],top;
int stt[maxn],dep;
bool ok()
{
    for(int i=0; i<top; i++)
    {
        if(stt[i]<stk[i])
        {
            for(; i<top; i++)
            {
                stk[i]=stt[i];
            }
            return true;
        }
        else if(stt[i]>stk[i])
            return false;
    }
    return false;

}

void dfs(int s)
{
    if(mp[s].size()==0)
    {
        if(top==dep&&ok());
        else if(top<dep)
        {
            top=dep;
            for(int i=0; i<dep; i++)
            {
                stk[i]=stt[i];
            }
        }
    }
    for(int i=0; i<mp[s].size(); i++)
    {
        stt[dep++]=mp[s][i];
        dfs(mp[s][i]);
        dep--;
    }

}
int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
    {
        int k;
        cin>>k;
        while(k--)
        {
            cin>>m;
            mp[i].push_back(m);
            in[m]++;
        }
    }
    for(int i=0; i<n; i++)
    {
        if(!in[i])
        {
            S=i;
            break;
        }
    }
    //考试的时候我好像这两句有问题 (..但怎么可能没检查出来呢)
    //但是当时肯定知道首节点需要通过入度查找
    stk[top++]=S;
    stt[dep++]=S;
    dfs(S);
    cout<<top<<endl;
    for(int i=0; i<top; i++)
    {
        if(i==0)
            cout<<stk[i];
        else
            cout<<" "<<stk[i];
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值