PAT乙级真题及训练题 1025. 反转链表 (25)


感觉几个世纪没打代码了,真是坏习惯,调了两小时把反转链表调出来了,心情舒畅。

这道题的步骤

  • 数据输入,数组纪录下一结点及储存值
  • 创建链表并储存上下结点“位置”
  • 按照规律遍历一遍链表并反转链表
  • 输出整条反转完的链表
#include <iostream>
#include <cstdio>
using namespace std;
int nextnum[100010],value[100010];
struct Node{
int value,pos,nnext;
Node *pnext,*plast,*final;
};
int main() {
int start,n,k,pos,next,val,count=1; /*-------------数据输入,数组纪录下一结点及储存值-------------*/
scanf("%d%d%d",&start,&n,&k);
for (int i=0; i<n; i++) {
scanf("%d%d%d",&pos,&val,&next);
nextnum[pos]=next;
value[pos]=val;
}
/*------------------------------------------------------*/ /*-------------创建链表并储存上下结点“位置”-----------------*/
pos=start;
Node *p1,*p2,*head;
p1=p2=new Node;
p1->value=value[pos];
p1->pos=pos;
head=p1;
pos=nextnum[pos];
while (pos!=-1) {
p1=new Node;
p2->pnext=p1;
p1->plast=p2;
p1->value=value[pos];
p1->pos=pos;
p2=p1;
pos=nextnum[pos];
count++;
}
p1->pnext=NULL; if (count==1) {
printf("%05d %d -1\n",head->pos,head->value);
return 0;
}
/*------------------------------------------------------*/ /*-------------反转链表----------------------------------*/
int cur=0;
Node *temp,*temp1=NULL;
temp=head;
p1=head;
while (p1) {
if (cur==k-1) {
head=p1;
}//find the head if (cur%k==0 && cur<count/k*k) {
p1->nnext=-1;
p1->final=NULL;
if (cur%(2*k)==0) temp=p1;
else temp1=p1;
}else {
if (cur<count/k*k) {
p1->final=p1->plast;
p1->nnext=p1->final->pos;
}else {
p1->final=p1->pnext;
if (cur<count-1) p1->nnext=p1->final->pos;
else p1->nnext=-1;
}
} if (cur>k-1 && (cur+1)%k==0) {
if ((cur+1)%(2*k)==0) {
temp->final=p1;
temp->nnext=p1->pos;
}else {
temp1->final=p1;
temp1->nnext=p1->pos;
} }
if (cur==count/k*k) {
if (cur%(2*k)!=0) {
temp->final=p1;
temp->nnext=p1->pos;
}else {
temp1->final=p1;
temp1->nnext=p1->pos;
} } cur++;
p1=p1->pnext;
if (cur==count) {
break;
}
}
/*------------------------------------------------------*/ /*-------------输出整条反转完的链表------------------------*/
p1=head;
while (p1) {
if (p1->nnext==-1) printf("%05d %d %d\n",p1->pos,p1->value,p1->nnext);
else printf("%05d %d %05d\n",p1->pos,p1->value,p1->nnext);
p1=p1->final;
} return 0;
}

另贴出一两个月前用指针结合数组的做法做的,作为对比

#include <iostream>
#include <cstdio>
using namespace std;
struct Node{
Node * pointer;
int nnext;
int position;
int nvalue;
};
Node line[100010],input[100010];
int main(){
int first,n,mol,
num,value,next,
i;
cin >> first >> n >> mol;
if (n==1) {
cin >> num >> value >> next;
printf("%05d %d -1\n",num,value);
}else{
for (i=0; i<n; i++) {
cin >> num >> value >> next;
if (next == -1) next=100002;
input[num].nvalue = value;
input[num].nnext = next;
input[num].position = num;
if (num == first) line[0] = input[num];
}
for (i=1; i<n; i++) {
line[i]=input[line[i-1].nnext];
if (line[i].nnext == 100002) break;
}
n=i+1;
int nreverse=(n/mol)*mol;
for (i=0; i<nreverse; i++) {
if (i%mol!=0) line[i].pointer = &line[i-1];
else if (i==nreverse-mol) line[i].pointer = &line[nreverse];
else line[i].pointer = &line[i+2*mol-1];
line[i].nnext = line[i].pointer->position;
}
for (i=nreverse; i<n-1; i++) line[i].pointer = &line[i+1];
if (n%mol == 0) {
line[n-mol].pointer = NULL;
line[n-mol].nnext = 100002;
}
else line[n-1].pointer = NULL;
Node * p= &line[mol-1];
if (n<mol) p = &line[0];
while (p != NULL) {
if (p->nnext != 100002) printf("%05d %d %05d\n",p->position,p->nvalue,p->nnext);
else printf("%05d %d -1\n",p->position,p->nvalue);
p = p->pointer;
}
}
return 0;
}

PAT乙级真题及训练题 1025. 反转链表 (25)的更多相关文章

  1. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

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

  2. 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→ ...

  3. PAT乙级 1025. 反转链表 (25)

    1025. 反转链表 (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个常数K以及一个单链表L,请 ...

  4. PAT 1025 反转链表 (25)(STL-map+思路+测试点分析)

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

  5. PAT-乙级-1025. 反转链表 (25)

    1025. 反转链表 (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个常数K以及一个单链表L,请 ...

  6. PAT 1025. 反转链表 (25)

    给定一个常数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,即最后 ...

  7. 1025 反转链表 (25 分)C语言

    题目描述 给定一个常数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 ...

  8. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  9. PAT乙级真题1003. 我要通过!(20)(解题)

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...

随机推荐

  1. How to describe the wind sprial in computer system?

    How to describe the wind sprial in computer system? 2017-02-21 刘崇军 风螺旋线 If we want get the approval ...

  2. Mouse点击之后,复制GridView控件的数据行

    本篇是实现用mouse点击GridView控件任意一行,把所点击的数据复制至另一个GridView控件上. 实现大概思路,把所点击的数据行的记录主键找出来,再去过滤数据源. 点击功能,已经实现,可以参 ...

  3. C# int? 关键字

    1.int?  关键字说明 (1).int? 表示一个int类型,且该int类型可空,如果不加?的话,那么int类型的默认值为0,不能赋null值,代码如下: int aa = null; (2).当 ...

  4. [EWS]查找 文件夹

    摘要 有时在操作exchange的时候,需要查找用户exchange文件夹,比如用户新建了一些文件夹. 一个例子 这里以查找用户outlook邮箱中的历史对话文件夹为例. private const ...

  5. 【Mysql】mysql和mariadb的区别

    MySQL之父Widenius先生离开了Sun之后,觉得依靠Sun/Oracle来发展MySQL,实在很不靠谱,于是决定另开分支,这个分支的名字叫做MariaDB.MariaDB跟MySQL在绝大多数 ...

  6. c++类内存分布解析

    首先使用Visual Studio工具查看类的内存分布,如下: 先选择左侧的C/C++->命令行,然后在其他选项这里写上/d1 reportAllClassLayout,它可以看到所有相关类的内 ...

  7. 华中农业大学第五届程序设计大赛网络同步赛-G

    G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...

  8. git 报错:error: failed to push some refs to 'https://github.com/Anderson-An/******.git'(已解决)

    提交push 报错: $ git push origin masterTo https://github.com/Anderson-An/******.git ! [rejected] master ...

  9. BZOJ2208: [Jsoi2010]连通数(tarjan bitset floyd)

    题意 题目链接 Sol 数据水的一批,\(O(n^3)\)暴力可过 实际上只要bitset优化一下floyd复杂度就是对的了(\(O(\frac{n^3}{32})\)) 还可以缩点之后bitset维 ...

  10. 08:Vigenère密码

    08:Vigenère密码 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 16世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法— ...