#include<stdio.h> const int MAXN = 1000; int n, left[MAXN], right[MAXN]; void link(int X, int Y) { right[X] = Y; left[Y] = X; } int main() { int m, X, Y; char type[9]; scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { left[i] = i-1; right[i] = i+1; } for(int i = 0; i < m; i++) { scanf("%s%d%d", &type, &X, &Y); link(left[X], right[X]); if(type[0] == 'A') { link(left[Y], X); link(X, Y); } else { link(X, right[Y]); link(Y, X); } } for(int X = right[0]; X != n+1; X = right[X]) printf("%d ", X); printf("\n"); return 0; }
上面這是原本的代碼
sample input :
6 2
A 1 4
B 3 5
sample output :
2 1 4 5 3 6
source code表面上看起來是對的,sample I&O放進去也沒錯
但是如果輸入的X不等於1的話
在printf()的那個for loop,X會一直為0,於是進入死循環...
讀者們自己debug就知道啦~
如何修正?
多加個一行指令就可以了
right[0] = 1; //不難思考加入的位置在哪,自行加入吧!
若有更好的方式請別吝嗇告訴我>ω<