【PTA-训练day19】L2-031 深入虎穴 + L1-079 天梯赛的善良

L2-031 深入虎穴 - dfs找最大深度

PTA | 程序设计类实验辅助教学平台

思路:

这题和小字辈很像

【PTA-训练day14】L2-026 小字辈 + L1-054 福到了_Roye_ack的博客-CSDN博客

  • 先找到入口,入口就是所有门都不能通往的那个 可以用一个st数组把出现的门都置为true 则遍历所有门 如果st[i]为false的则为入口
  • 通过入口dfs 搜寻最大深度 并及时记录最大深度所对应的门编号
#include <bits/stdc++.h>
using namespace std;

const int N=1e5+10;
int n,maxx=-1,res;
vector<vector<int>> v; 

void dfs(int u,int d)
{
    if(v[u].size()==0)
        if(maxx<d)
        {
            maxx=d;
            res=u;
        }
    for(int i=0;i<v[u].size();i++)
        dfs(v[u][i],d+1);
}

int main()
{
    cin>>n;
    bool st[n+1]={false};
    v.resize(n+1); //因为vector是动态的 所以行要固定
    for(int i=1;i<=n;i++)
    {
        int k;
        cin>>k;
        while(k--)
        {
            int x;
            cin>>x;
            v[i].push_back(x);
            st[x]=true;//入口是其他门都不能通往的那个 则可以通往的门标为true 入口就是false
        }
    }
    for(int i=1;i<=n;i++) if(!st[i]) dfs(i,1);
    cout<<res;
}

L1-079 天梯赛的善良 - 20 - 弱智题

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,maxx=-1,minx=0x3f3f3f3f,c1=0,c2=0;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        maxx=max(maxx,a[i]);
        minx=min(minx,a[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]==maxx) c1++;
        if(a[i]==minx) c2++;
    }
    cout<<minx<<" "<<c2<<endl<<maxx<<" "<<c1;
}

 

L1-077 大笨钟的心情 - 15

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a[25];
    for(int i=0;i<24;i++) cin>>a[i];
    while(1)
    {
        int x;
        cin>>x;
        if(x<0||x>23) break;
        cout<<a[x]<<" ";
        if(a[x]>50) puts("Yes");
        else puts("No");
    }
    
}

L1-075 强迫症 - 10

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n=s.size();
    if(n==6) cout<<s.substr(0,4)<<"-"<<s.substr(4);
    else{
        string str=s.substr(0,2);
        int t=stoi(str);
        if(t<22) cout<<"20";
        else cout<<"19";
        cout<<str<<"-"<<s.substr(2);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值