从一个不懂链表,到反反复复改了不下50遍,提交该题页数更是突破了五页,从周三下午到周五中午的面向对象课前的20分钟,终于把这道题AC了,其实这题本来是原来C语言综合实验的一道题,但是本次在PAT上的审核却没有那么简单。

说下心路历程

  • 其实这个链表在开始的十个小时的学习中对我来说就跟之前的CLASS一样,完完全全的新东西,不知道从何temp->next,也不知道题意,让我反转什么。圈圈画画,终于写了出来,后来发现前面写的指针代码都比较复杂。可以简化。在之后的小时里都是在跟PTA的测试点作斗争,正如前面的随笔所言,只有两个AC,我就在想为什么有两个AC而其他的问题都是答案错误,于是我遍历了所有的代码,感觉虽然愚钝,但是符合逻辑,可执行才对,于是开始了各种数据的测试,从小数据,特殊数据,到大数据,乱七八糟的,打乱顺序的都试过,然而命运就像是和我开玩笑,测试的数据全对,但是过不了PAT的测试点。当时的我真的想把PAT的测试点挖出来问问,到底是什么原因。因此我上了C语言综合实验的测试网站,提交了自己的代码,结果竟然是AC了。这我就更疑惑了,为什们我能够通过那个测试,却过不了PAT,在沉默了跟打代码一样长的时间后,我终于发现了问题症结。PAT测试点会呈现段错误,而段错误在我这里会因为某种原因继续下去,因为我的段错误是一种非数组越界,或者是栈 队列溢出那种的问题,我的问题在于循环中加入了太多的循环和判断,我想这个问题原因可能是对于小数据,PAT会规定具体的循环次数,以及if的次数如果超过,就会段错误,但是会继续执行,答案输出了,即使对了,也会按照答案错误处理并张贴。这个就会让人误以为是代码的问题,而实际并不是。PAT果然严格。不过希望他能告诉我是段错误。

下面贴出代码和截图。



代码部分

#include<iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include<stdio.h>
using namespace std; int main()
{
struct Date
{
int Adress;
int num;
int Next;
struct Date *next;
};
int all, ts, i = 0;
Date*head = (Date*)malloc(sizeof(Date));
Date*p1 = (Date*)malloc(sizeof(Date));
Date*p2 = (Date*)malloc(sizeof(Date));
scanf("%d %d %d", &head->Adress, &all,&ts); while (i != all)
{
Date*p1 = (Date*)malloc(sizeof(Date));
scanf("%d %d %d", &p1->Adress, &p1->num, &p1->Next); if (i == 0)
head->next = p1; else
p2->next = p1; p2 = p1;
i++;
}
p2->next = NULL;
Date*temp1 = (Date*)malloc(sizeof(Date));
Date*temp2 = (Date*)malloc(sizeof(Date));
Date*r = (Date*)malloc(sizeof(Date));
Date*p = (Date*)malloc(sizeof(Date));
temp1 = head;
temp2 = temp1->next;
while (1)
{
if (head->Adress == temp2->Adress)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
}
r = head->next;
head->next = temp2;
temp2->next = r;
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
temp2 = temp1->next; while (temp1 != NULL)
{
while (temp2 != NULL)
{
if (temp2->Adress == temp1->Next&&temp2->Next != -1)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
}
r = temp1->next;
temp1->next = temp2;
temp2->next = r;
break; }
else if (temp2->Adress == temp1->Next&&temp2->Next == -1)
{
p = temp1;
for (;;)
{
if (p->next == temp2)
{
p->next = temp2->next;
break;
}
else
{
p = p->next;
}
} temp1->next = temp2;
temp2->next = NULL;
break;
} else
{
temp2 = temp2->next;
continue;
}
}
if (temp1->next != NULL)
{
temp1 = temp1->next;
temp2 = temp1->next;
}
else
break;
} int js = 0;
Date*check = (Date*)malloc(sizeof(Date));
check = head->next;
for (;;)
{
if (check->Next != -1)
{
check = check->next;
js++;
}
else if (check->Next == -1)
{
js++;
break;
}
} all =js;
if (ts == 0)
ts = 1; int j=all/ts;
if (ts > all || all == 1||j==0)
j = 0;
else
{
temp1 = head;
temp2 = temp1->next;
i = 0;
for (int l = 1; j > 0; l++, j--)
{
while (i != l*(ts - 1))
{
temp2 = temp2->next;
i++;
}
p1 = temp2;
r = temp2->next;
p = temp1;
while (p->next != temp2)
{
for (;;)
{
if (p->next == temp2)
{
temp2->next = p;
temp2 = temp2->next;
p = temp1;
break;
}
else
{
p = p->next;
}
}
}
temp2->next = r;
temp1->next = p1;
while (temp1 != temp2)
{
temp1 = temp1->next;
}
if (temp1 == NULL)
break;
else
{
temp2 = temp1->next;
}
}
} temp1 = head->next;
temp2 = temp1->next;
head->Adress = head->next->Adress;
for (i = 0; i < all-1 ; i++)
{
temp1->Next = temp2->Adress;
temp1 = temp1->next;
temp2 = temp1->next; }
if(temp1->next==NULL)
temp1->Next = -1; Date*out = (Date*)malloc(sizeof(Date));
out = head->next;
while (out != NULL)
{
if (out->Next != -1)
{
cout << setfill('0') << setw(5) << out->Adress << " " << out->num << " " << setfill('0') << setw(5) << out->Next << endl;
out = out->next;
}
else
{
cout << setfill('0') << setw(5) << out->Adress << " " << out->num << " " << out->Next << endl;
out = out->next;
}
} return 0;
}

最新文章

  1. Ubuntu操作系统相关
  2. js之事件冒泡和事件捕获详细介绍
  3. Android DiskLruCache 源码解析 硬盘缓存的绝佳方案
  4. WCF实例上下文模式与并发模式对性能的影响
  5. 每天一个linux命令(39):iostat命令
  6. Symantec更新服务器
  7. IntelliJ IDEA 开发前的设置
  8. AcmeAir安装AI探针--SaaS版
  9. zabbix 默认item采集使用被动模式 需要改为主动模式
  10. sql相关操作
  11. C++程序设计实践指导1.7超长数列中n个数排序改写要求实现
  12. css 的包含块 、负外边距,字体,文本行高
  13. 谈下spring下的 aop日志记录
  14. React从入门到放弃之前奏(2):React简介
  15. 企业面试必会shell
  16. erlang开发工具之intellij idea基本使用
  17. c++多态及实现原理
  18. hexo——轻量、简易、高逼格的博客
  19. 设计模式笔记:策略模式(Strategy)
  20. 浏览器内核控制标签meta说明

热门文章

  1. 【C#高级】泛型(一)
  2. Linq lambda 匿名方法
  3. [javaSE] 多线程(守护线程)
  4. 十五、读写锁ReentrantReadWriteLock
  5. 使用dom4j写xml文件——源码
  6. [LeetCode]Maximum Subarray题解
  7. http_proxy_module 模块(proxy_pass 指令)
  8. unity3d之游戏优化
  9. Java数据库操作(JDBC)
  10. js在ie6下的一个bug—未结束标签的错误