printf中:%5d,固定为5位不足由空格从左面补全
%05d固定为5位不足由0从左面补全
new int [n] n可以为0
sort(begin,end) or sort(begin,end,cmp)
其中比较函数cmp只能返回bool型
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
struct list{
int begin;
int key;
int next;
int flag;
};
bool cmp(struct list a,struct list b){
return a.key < b.key;
}
int main(){
freopen("1.in", "r", stdin);
int n, start,next;
scanf("%d%d", &n, &start);
struct list *a = new struct list [n+5];
vector <struct list> h;
int i;
for (i = 0; i < n; i++){
scanf("%d%d%d", &a[i].begin, &a[i].key, &a[i].next);
a[i].flag = 0;
}
int flag = 0,f = 0;
while (start != -1){
f = 0;
for (i = 0; i < n;i++)
if (a[i].begin == start&&!a[i].flag){
a[i].flag = 1;
start = a[i].next;
h.push_back(a[i]);
f = 1;
}
if (!f)
{
flag = 1;
break;
}
}
if (flag||!n||h.size() == 0)
printf("0 -1\n");
else {
vector<struct list>::iterator k;
sort(h.begin(), h.end(), cmp);
printf("%d %05d\n", h.size(), (*h.begin()).begin);
for (k = h.begin(),i = 1; k != h.end(); k++,i++)
{
printf("%05d %d", (*k).begin, (*k).key);
if ((k+1) == h.end())
printf(" -1\n");
else printf(" %05d\n", (*(k + 1)).begin);
}
}
return 0;
}