首页 > 代码库 > 【HDOJ】4985 Little Pony and Permutation

【HDOJ】4985 Little Pony and Permutation

水题。

 1 #include <cstdio> 2  3 #define MAXN 100005 4  5 int buf[MAXN], n; 6  7 int main() { 8     int i, j, k; 9 10     while (scanf("%d", &n) != EOF) {11         for (i=1; i<=n; ++i)12             scanf("%d", &buf[i]);13         for (i=1; i<=n; ++i) {14             if (buf[i]) {15                 printf("(%d", i);16                 j = buf[i];17                 buf[i] = 0;18                 while (buf[j]) {19                     printf(" %d", j);20                     k = buf[j];21                     buf[j] = 0;22                     j = k;23                 }24                 printf(")");25             }26         }27         printf("\n");28     }29 30     return 0;31 }

 

【HDOJ】4985 Little Pony and Permutation