pat a 1052 Linked List Sorting

#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){//1
		return 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){//1
		return 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.有鏈表的題注意判斷無效節點,判斷鏈表是否爲空。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章