pat a 1052 Linked List Sorting
来源:互联网 发布:盗图投诉淘宝复审 编辑:程序博客网 时间:2024/06/02 04:33
#include<cstdio>#include<algorithm> using namespace std;const int maxn=100005;struct Node{int address;int data;int next;bool flags=false; }node[maxn];bool cmp(Node a,Node b){if(a.flags>b.flags){//1return true;}else{return a.data<b.data;}}int main(){int num,head,ad;scanf("%d%d",&num,&head);for(int i=0;i<num;i++){ scanf("%d%d%d",&ad,&node[ad].data,&node[ad].next);//2 node[ad].address=ad; }int active=head;int count=0;while(active!=-1){node[active].flags=true;active=node[active].next;count++;} if(count==0){printf("0 -1\n"); }else{ sort(node,node+maxn,cmp);printf("%d %05d\n",count,node[0].address);for(int i=0;i<count;i++){printf("%05d %d",node[i].address,node[i].data);if(i==count-1){printf(" -1\n");}else{printf(" %05d\n",node[i+1].address);}}}return 0;} /*将标记2的地方修改为: scanf("%d",&ad); scanf("%d%d",&node[ad].data,&node[ad].next);
//from liuchuo:
#include <cstdio>#include <algorithm>using namespace std;struct NODE { int address; int key; int next; bool flag;}node[100000];int cmp1(NODE a, NODE b) { if(a.flag == false || b.flag == false) { return a.flag > b.flag; } else { return a.key < b.key; }}int main() { int n, cnt = 0, s, a, b, c; scanf("%d%d", &n, &s); for(int i = 0; i < n; i++) { scanf("%d%d%d", &a, &b, &c); node[a].address = a; node[a].key = b; node[a].next = c; } for(int i = s; i != -1; i = node[i].next) { node[i].flag = true; cnt++; } if(cnt == 0) { printf("0 -1"); } else { sort(node, node + 100000, cmp1); printf("%d %05d\n", cnt, node[0].address); for(int i = 0; i < cnt; i++) { printf("%05d %d ", node[i].address, node[i].key); if(i != cnt - 1) printf("%05d\n", node[i + 1].address); else printf("-1\n"); } } return 0;}
//My::错误百出第一版,越改越错,码生昏暗#include<cstdio>#include<algorithm> using namespace std;struct Node{int address;int data;int next;bool flags=false; }node[100005];bool cmp(Node a,Node b){if(a.flags>b.flags){return true;}//这里错了!!cmp只能用if-else-if.不能有两个if!!if(a.flags==true&&b.flags==true){return a.data<b.data;}}int main(){int num,head,ad;scanf("%d%d",&num,&head);for(int i=0;i<num;i++){ scanf("%d%d%d",&ad,&node[ad].data,&node[ad].next); node[ad].address=ad; }int active=head;int count=0;while(active!=-1){node[active].flags=true;active=node[active].next;count++;} /*while(active!=-1){for(int i=0;i<num;i++){if(active==node[i].address){node[i].flags=true;active=node[i].next;count++;break;}}}这种写法运行超时了*/if(count==0){/*判断空链表!!!*/printf("0 -1\n"); }else{ sort(node,node+100005,cmp);/*因为前面将改写成了随机存取,所以这里要对整张链表排序*/ printf("%d %05d\n",count,node[0].address);for(int i=0;i<count;i++){printf("%05d %d",node[i].address,node[i].data);if(i==count-1){printf(" -1\n");}else{printf(" %05d\n",node[i+1].address);}}}return 0;}//
段错误,答案错误,运行超时……快凑齐了//段错误一般是内存越界。//由版本一修改而来的错误百出版本二:这是有两个错误点的代码:得到结果:段错误没了,
*/将标记1的地方修改为:if(a.flags==false||b.flags==false){//1return a.flags>b.flags;后经测试:错误点cmp函数也会引起段错误。已经用了很长时间,故不再研究。总结:1.严格规范编写cmp;注意只能有一个if,return一定是不等式。2.连续两个输入,后一个输入需要用到前一个的结果,那就用两次scanf()。全写在一个里会爆段错误。3.注意时间复杂度,空间换时间。有m个数,求m中的数在另外n个数中是否出现:hash表的时间复杂度是O(n+m)(n次存入,m次匹配)。用二层迭代就是O(n*m).4.有链表的题注意判断无效节点,判断链表是否为空。
阅读全文
0 0
- pat a 1052 Linked List Sorting
- PAT A 1052. Linked List Sorting (25)
- PAT(A) - 1052. Linked List Sorting (25)
- PAT-A-1052. Linked List Sorting (25)
- Pat 1052 Linked List Sorting (25)
- PAT 1052. Linked List Sorting
- PAT-Linked List Sorting (25)
- 【PAT】1052. Linked List Sorting
- PAT--1052. Linked List Sorting
- 1052 Linked List Sorting
- Pat(Advanced Level)Practice--1052(Linked List Sorting)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- 1052. Linked List Sorting (25)-PAT
- 【PAT】1052. Linked List Sorting (25)
- PAT (Advanced) 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- 拉钩异步数据抓取
- Mac下更新npm版本
- 数组常见算法比较
- Tesseract-OCR的 实现训练
- LVS-DR配置篇
- pat a 1052 Linked List Sorting
- 为适配平板而生的Dialog(同样也支持手机)
- JSON对象和JSON字符串间的相互转换
- TCP/IP 链路层
- Appium三种等待方式
- Spring MVC + redis 消息的订阅发布
- 顺序表的实现
- Typescript代码实例解析
- 算法规律 rotate String