PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025

给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。

输出格式:

对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1 此题一大坑儿就是: 遇到-1则结束,不管提供的节点是否有剩余
 #include<stdio.h>

 int main()
{
int head=,n=,v=;
scanf("%d%d%d",&head,&n,&v); int address=,data=,next=,prior=,ppp=;
int node[][]={};// 0-prior 1-Data 2-Next
for(int i=;i<n;i++)
{
scanf("%d%d%d",&address,&data,&next);
node[address][]=data;
node[address][]=next;
} next=head; //修补前指针
prior=head;
for(int i=;i<n;i++)
{
node[next][]=prior;
prior=next;
next=node[next][];
while(next<)
{
n=i+;
break; }
}
ppp=next;//node[next][0]=prior; prior=-;
next=head;
if(v<=) v=n+;
for(int i=;i<n;)
{
next=head;
if(i+v<=n)
{
for(int j=i;j<i+v;j++)
{
if(j==i) next=head;
else next=node[next][];
} head=node[next][];
for(int j=i;j<i+v;j++)
{
if(j) printf(" %05d\n",next);
printf("%05d %d",next,node[next][]); next=node[next][];
}
}
else
{
for(int j=i;j<n;j++)
{
if(j) printf(" %05d\n",next);
printf("%05d %d",next,node[next][]);
next=node[next][];
}
} i+=v;
}
printf(" %d",ppp);
return ;
}

PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)的更多相关文章

  1. PAT (Basic Level) Practice (中文)1025 反转链表 (25分)

    1025 反转链表 (25分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→ ...

  2. PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)

    PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...

  3. PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)

    PAT (Basic Level) Practise (中文)-  1022. D进制的A+B (20)  http://www.patest.cn/contests/pat-b-practise/1 ...

  4. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  5. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  6. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

  7. PAT (Basic Level) Practise (中文)-1028. 人口普查(20)

    PAT (Basic Level) Practise (中文)-1028. 人口普查(20)   http://www.patest.cn/contests/pat-b-practise/1028 某 ...

  8. PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)

    PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...

  9. PAT (Basic Level) Practise (中文)-1030. 完美数列(25)

    PAT (Basic Level) Practise (中文)-1030. 完美数列(25)   http://www.patest.cn/contests/pat-b-practise/1030 给 ...

随机推荐

  1. CF438E The Child and Binary Tree(生成函数+多项式开根+多项式求逆)

    传送门 可以……这很多项式开根模板……而且也完全不知道大佬们怎么把这题的式子推出来的…… 首先,这题需要多项式开根和多项式求逆.多项式求逆看这里->这里,这里讲一讲多项式开根 多项式开方:已知多 ...

  2. IT兄弟连 JavaWeb教程 JSTL标签的使用

    假定甲方打算使用乙方开发的标签库,乙方把与标签库相关的所有文件打包成为了一个JAR文件(假定名为standard.jar),在这个JAR文件中包含以下内容: ●  标签处理类及相关的.class文件 ...

  3. GenericKeychain

    KeychainItemWrapper是apple官方例子“GenericKeychain”里一个访问keychain常用操作的封装类,在官网上 下载了GenericKeychain项目后,只需要把“ ...

  4. Python学习 Part2:深入Python函数定义

    在Python中,可以定义包含若干参数的函数,这里有几种可用的形式,也可以混合使用: 1. 默认参数 最常用的一种形式是为一个或多个参数指定默认值. >>> def ask_ok(p ...

  5. pytest入门学习(1)

    系统ubuntu 12.04 , 可上网 一.安装: 1.安装 setuptools 下载页面:https://bitbucket.org/pypa/setuptools/get/default.ta ...

  6. assembly x86(nasm)串比较

    预留字符串口令,输入口令串与预留密码串比较.若匹配则显示“MATCH!CONGRATULATION”,否则显示“NOMATCH!”,并让用户重新输入,程序能对口令进行测试,但测试次数最多3次,若3次输 ...

  7. Apollo应用相关JVM配置参数

    -Dapollo_profile=github,auth-Ddev_meta=http://localhost:8080/-Dserver.port=8070-Dspring.datasource.u ...

  8. Verify the Developer App certificate for youraccount is trusted on your device

    运行时报错-Verify the Developer App certificate for youraccountis   trusted on your device. Open Settings ...

  9. 关于presentViewController 后调用pushViewController

    错误代码: LoginViewController *loginVc = [[LoginViewController alloc] int]; [self presentViewController ...

  10. Points on Plane Codeforces - 576C

    https://www.luogu.org/problemnew/show/CF576C 看题面,一眼按莫队的方法排一下 直接交就会和我一样发现WA掉了... 算一下会发现,上限是3e9(块内左端点1 ...