1165 Block Reversing (25 分)

Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.
注:可能并不是每个点都在链表上
自己记个数

#include<bits/stdc++.h>
using namespace std;
int a[100000][2];
struct node{
int add;
int p;
int next;
}p[100010],ppp[100010];
int main()
{
    int f,n,k,add,data,next;
    cin>>f>>n>>k;
    for(int i=0;i<n;i++){
        cin>>add>>data>>next;
        a[add][0]=data;
        a[add][1]=next;
    }
    int cnt=f,flag=0,xxx=0;
    while(cnt!=-1){
       // cout<<cnt<<endl;
        p[flag].add=cnt;
        p[flag].p=a[cnt][0];
        p[flag].next=a[cnt][1];
        cnt=a[cnt][1];
        flag++;
    }
    n=flag;
    cnt=n;
    while(cnt!=0){
        if(cnt%k!=0){
            flag=cnt%k;
        }else{
            flag=k;
        }
        for(int i=0;i<flag;i++){
            ppp[xxx].add=p[cnt-flag+i].add;
            ppp[xxx].p=p[cnt-flag+i].p;
            ppp[xxx].next=-1;
            if(xxx!=0){
                ppp[xxx-1].next=ppp[xxx].add;
            }
            xxx++;
            //cout<<p[cnt-flag+i].add<<" "<<p[cnt-flag+i].p<<" "<<p[cnt-flag+i].next<<endl;
        }
        cnt-=flag;
    }
    for(int i=0;i<n-1;i++){
        printf("%05d %d %05d\n",ppp[i].add,ppp[i].p,ppp[i].next);
    }
    printf("%05d %d -1\n",ppp[n-1].add,ppp[n-1].p);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值