生活随笔
收集整理的這篇文章主要介紹了
【PAT乙级】1075 链表元素分类 (25 分)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目地址
#include<cstdio>
#include<iostream>
#include<map>
using namespace std
;
const int N
=1e5+10;
struct node
{int id
;int val
;
}L
[N
],ans
[N
];
map
<int,int>mp
,val
,hush
;
int st
,n
,k
;
int main(void)
{cin
>>st
>>n
>>k
;for(int i
=0;i
<n
;i
++){int a
,b
,c
; cin
>>a
>>b
>>c
;mp
[a
]=c
,val
[a
]=b
;}int index
=0;while(1){L
[index
].id
=st
,L
[index
].val
=val
[st
];if(st
>=0) index
++;st
=mp
[st
];if(st
==-1) break;} n
=index
;index
=0;for(int i
=0;i
<n
;i
++) if(L
[i
].val
<0) ans
[index
++]=L
[i
],hush
[i
]=-1;for(int i
=0;i
<n
;i
++) if(L
[i
].val
<=k
&&hush
[i
]!=-1) ans
[index
++]=L
[i
],hush
[i
]=-1;for(int i
=0;i
<n
;i
++) if(hush
[i
]!=-1) ans
[index
++]=L
[i
];for(int i
=0;i
<n
;i
++){if(i
+1!=n
) printf("%05d %d %05d\n",ans
[i
].id
,ans
[i
].val
,ans
[i
+1].id
);else printf("%05d %d -1\n",ans
[i
].id
,ans
[i
].val
);}
}
#include<bits/stdc++.h>
using namespace std
;
int startx
,n
,k
;
map
<int,int>hush
,val
,ne
;
vector
<pair
<int,int>> ve
,ans
;
int main(void)
{cin
>>startx
>>n
>>k
;for(int i
=0;i
<n
;i
++){int a
,b
,c
; cin
>>a
>>b
>>c
;val
[a
]=b
,ne
[a
]=c
;}while(startx
!=-1){int id
=startx
,s
=val
[startx
];if(startx
>=0) ve
.push_back({id
,s
});startx
=ne
[startx
];}for(int i
=0;i
<ve
.size();i
++) if(ve
[i
].second
<0) hush
[ve
[i
].first
]=1,ans
.push_back(ve
[i
]);for(int i
=0;i
<ve
.size();i
++) if(ve
[i
].second
>=0&&ve
[i
].second
<=k
)hush
[ve
[i
].first
]=1,ans
.push_back(ve
[i
]);for(int i
=0;i
<ve
.size();i
++) if(!hush
[ve
[i
].first
]) ans
.push_back(ve
[i
]);for(int i
=0;i
<ans
.size();i
++){if(i
==ans
.size()-1) printf("%05d %d %d",ans
[i
].first
,ans
[i
].second
,-1);else printf("%05d %d %05d\n",ans
[i
].first
,ans
[i
].second
,ans
[i
+1].first
);}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的【PAT乙级】1075 链表元素分类 (25 分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。