【PTA-训练day10】L2-022 重排链表 + L1-043 阅览室

L2-022 重排链表 - 模拟双向链表

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

思路:

构造双向链表

e[id] 存的是数据值

ne[id] 存的是下一个节点的id

pre[ne[id]] 存的是下一个节点的前一个节点的地址 也就是id

        1     2     3     4     5     6

        ↑                                 ↑

        i                                   j

输出: 1  6  2  5  3  4

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

const int N=1e5+10;
int e[N],ne[N],pre[N],h;

int main()
{
    int n;
    cin>>h>>n;
    while(n--)
    {
        int id,data,next;
        cin>>id>>data>>next;
        e[id]=data;
        ne[id]=next;
        pre[ne[id]]=id;
    }
    int i=h,j=h;
    while(ne[j]!=-1)
    {
        j=ne[j];
    }
    while(i!=j)
    {
        printf("%05d %d %05d\n",j,e[j],i);
        j=pre[j];
        if(i==j)
        {
            break;
        }
        printf("%05d %d %05d\n",i,e[i],j);
        i=ne[i];
    }
    printf("%05d %d -1\n",i,e[i]);
    return 0;
}

 

!L1-043 阅览室 - 20

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

思路:

用一个二维数组记录

book [id] [0] 记录有没有借出 —— 0表示未借出 1表示已借出

book [id] [1] 记录借出时间

  • 如果ch=='S' 标记为已借出 并记录初始时间
  • 如果ch=='E' 标记为已归还 并用sum记录总借出时间 cnt记录一次完整的归还流程

id=0时结束一天,用while(id),但要注意输入的循环问题

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

int main()
{
    int n;
    cin>>n;
    int book[1001][2]={0};
    while(n--)
    {
        int id,hh,mm,sum=0,cnt=0;
        char ch;
        char t;
        cin>>id>>ch>>hh>>t>>mm;
        while(id)
        {
            if(ch=='S')
            {
                book[id][0]=1;
                book[id][1]=hh*60+mm;
            }
            if(ch=='E'&&book[id][0]==1)
            {
                book[id][0]=0;
                sum+=hh*60+mm-book[id][1];
                cnt++;
            }
            cin>>id>>ch>>hh>>t>>mm;
        }
        if(cnt<2) printf("%d %d\n",cnt,sum);
        else printf("%d %.0f\n",cnt,sum*1.0/cnt);
    }
}

L1-044 稳赢 - 15

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

int main()
{
    int n,cnt=0;
    cin>>n;
    string s;
    while(true)
    {
        cin>>s;
        if(s=="End") break;
        if(cnt==n)
        {
            cout<<s<<endl;
            cnt=0;
            continue;
        }
        if(s=="ChuiZi") cout<<"Bu"<<endl,cnt++;
        if(s=="JianDao") cout<<"ChuiZi"<<endl,cnt++;
        if(s=="Bu") cout<<"JianDao"<<endl,cnt++;
    }
}

L1-041 寻找250 - 10

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

int main()
{
    string s;
    int cnt=0;
    while(true)
    {
        cin>>s;
        cnt++;
        if(s=="250") break;
    }
    cout<<cnt;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值