PAT(甲级)2019年秋季考试 7-2 Merging Linked Lists

tech2024-07-08  65

#include<cstdio> #include<vector> using namespace std; struct node{ int data, address, next; }m[100010]; vector<int> a, b; int main() { int heada, headb, n; scanf("%d%d%d", &heada, &headb, &n); for(int i = 0; i < n; i++) { int ad; scanf("%d", &ad); scanf("%d%d", &m[ad].data, &m[ad].next); m[ad].address = ad; } int pa = heada, pb = headb; while(pa != -1) { a.push_back(pa); pa = m[pa].next; } while(pb != -1) { b.push_back(pb); pb = m[pb].next; } vector<int> small, big, res; if(a.size() > b.size()) { big = a; small = b; } else { big = b; small = a; } int index = small.size()-1; for(int i = 0; i < big.size(); i++) { res.push_back(big[i]); if(i % 2 == 1 && index >= 0) res.push_back(small[index--]); } for(int i = 0; i < res.size(); i++) { printf("%05d %d ", m[res[i]].address, m[res[i]].data); if(i < res.size()-1) printf("%05d\n", m[res[i+1]].address); else printf("-1\n"); } }
最新回复(0)