其他分享
首页 > 其他分享> > 【专题复习6:链表】1032、1052、1074、1097、1133

【专题复习6:链表】1032、1052、1074、1097、1133

作者:互联网

1032

1032

点击查看代码
#include <bits/stdc++.h>

using namespace std;
struct Node
{
    char key;
    int next;
    bool flag;
}node[100000];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s1,s2,id;
    cin>>s1>>s2>>n;
    for(int i=0;i<n;i++){
        cin>>id;
        cin>>node[id].key>>node[id].next;
    }
    for(int i=s1;i!=-1;i=node[i].next)
        node[i].flag=true;
    for(int i=s2;i!=-1;i=node[i].next){
        if(node[i].flag==true){
            printf("%05d",i);
            return 0;
        }
    }
    printf("-1");
    return 0;
}

1052

1052
可恶,我的眼睛咋这么不好使呢,一个bug卡了半天竟是因为cnt写成了n...

点击查看代码
#include <bits/stdc++.h>

using namespace std;
struct Node
{
    int data,key;
    int next;
    bool flag;
} v[100000];
bool cmp(Node a,Node b)
{
    return !a.flag||!b.flag?a.flag>b.flag:a.key<b.key;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s1,id,cnt=0;
    cin>>n>>s1;
    for(int i=0; i<n; i++)
    {
        cin>>id;
        v[id].data=id;
        cin>>v[id].key>>v[id].next;
    }
    for(int i=s1; i!=-1; i=v[i].next){
        v[i].flag=true;
        cnt++;
    }
    if(cnt==0)
        cout<<"0 -1";
    else
    {
        sort(v,v+100000,cmp);
        printf("%d %05d\n",cnt,v[0].data);
        for(int i=0; i<cnt-1; i++)
        {
            printf("%05d %d %05d\n",v[i].data,v[i].key,v[i+1].data);
        }
        printf("%05d %d -1\n",v[cnt-1].data,v[cnt-1].key);

    }
    return 0;
}

1074

1074
这个用法reverse(begin(result)+i,begin(result)+i+k);还是少,但是柳神这个数学表达式result[i] = list[i / k * k + k - 1 - i % k];也太难想了T^T

点击查看代码
#include <bits/stdc++.h>

using namespace std;
const int maxn=100000;
int Address[maxn],Next[maxn],result[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s,k,id,key,nxt,cnt=0;
    cin>>s>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>id>>key>>nxt;
        Address[id]=key;
        Next[id]=nxt;
    }
    for(int i=s; i!=-1; i=Next[i]){
        result[cnt++]=i;
    }
    for(int i=0;i<cnt-cnt%k;i+=k)
        reverse(begin(result)+i,begin(result)+i+k);
    for(int i=0; i<cnt-1; i++)
    {
        printf("%05d %d %05d\n",result[i],Address[result[i]],result[i+1]);
    }
    printf("%05d %d -1\n",result[cnt-1],Address[result[cnt-1]]);
    return 0;
}

1097

1097

点击查看代码
#include <bits/stdc++.h>

using namespace std;
const int maxn=100000;
struct Node
{
    int address,key,next;
    int num=2*maxn;
}node[maxn];
int vis[maxn];
bool cmp(Node a,Node b){ return a.num<b.num;}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s,id,cnt1=0,cnt2=0;
    cin>>s>>n;
    for(int i=0; i<n; i++)
    {
        cin>>id;
        node[id].address=id;
        cin>>node[id].key>>node[id].next;
    }
    for(int i=s;i!=-1;i=node[i].next){
        if(vis[abs(node[i].key)]==false){
            node[i].num=cnt1;
            vis[abs(node[i].key)]=true;
            cnt1++;
        }else{
            node[i].num=maxn+cnt2;
            cnt2++;
        }
    }
    sort(node,node+maxn,cmp);
    int cnt=cnt1+cnt2;
    for(int i=0; i<cnt; i++)
    {
        if(i!=cnt1-1&&i!=cnt-1)
            printf("%05d %d %05d\n",node[i].address,node[i].key,node[i+1].address);
        else
            printf("%05d %d -1\n",node[i].address,node[i].key);
    }
    return 0;
}

1133

1133

正确答案

点击查看代码
#include <bits/stdc++.h>

using namespace std;
const int maxn=100000;
struct Node
{
    int address,key,next;
}node[maxn];
int vis[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s,id,k;
    cin>>s>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>id;
        node[id].address=id;
        cin>>node[id].key>>node[id].next;
    }
    vector<int> v[3];
    for(int i=s;i!=-1;i=node[i].next){
        if(node[i].key<0){
            v[0].push_back(i);
        }else if(node[i].key>k){
            v[2].push_back(i);
        }else v[1].push_back(i);
    }
    int flag=0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < v[i].size(); j++) {
            if (flag == 0) {
                printf("%05d %d ", v[i][j], node[v[i][j]].key);
                flag = 1;
            } else {
                printf("%05d\n%05d %d ", v[i][j], v[i][j], node[v[i][j]].key);
            }
        }
    }
    printf("-1");
    return 0;
}

19分答案

我这个答案输出处理的和shit一样Orz

点击查看代码
#include <bits/stdc++.h>

using namespace std;
const int maxn=100000;
struct Node
{
    int address,key,next;
}node[maxn];
int vis[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,s,id,k;
    cin>>s>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>id;
        node[id].address=id;
        cin>>node[id].key>>node[id].next;
    }
    vector<int> v[3];
    for(int i=s;i!=-1;i=node[i].next){
        if(node[i].key<0){
            v[0].push_back(i);
        }else if(node[i].key>k){
            v[2].push_back(i);
        }else v[1].push_back(i);
    }
    for(int i=0; i<v[0].size(); i++){
        if(i!=v[0].size()-1)
            printf("%05d %d %05d\n",v[0][i],node[v[0][i]].key,v[0][i+1]);
        else if(v[1].size()!=0)
            printf("%05d %d %d\n",v[0][i],node[v[0][i]].key,v[1][0]);
    }
    for(int i=0; i<v[1].size(); i++){
        if(i!=v[1].size()-1)
            printf("%05d %d %05d\n",v[1][i],node[v[1][i]].key,v[1][i+1]);
        else if(v[2].size()!=0)
            printf("%05d %d %d\n",v[1][i],node[v[1][i]].key,v[2][0]);
    }
    for(int i=0; i<v[2].size(); i++){
        if(i!=v[2].size()-1)
            printf("%05d %d %05d\n",v[2][i],node[v[2][i]].key,v[2][i+1]);
        else
            printf("%05d %d -1\n",v[2][i],node[v[2][i]].key);
    }
    return 0;
}

标签:node,1052,1133,next,链表,int,maxn,key,id
来源: https://www.cnblogs.com/moonlight1999/p/15957599.html