[PAT] A1032 Sharing

tech2022-09-02  98

静态链表。见算法笔记第261页。 跟字母是啥并没有关系。。。

Tips

注意输出的时候要保证5位,因为高位可能为0,但也要输出。 scanf使用%c格式时是可以读入空格的,因此在输入地址、数据、后继结点时,要在中间加空格: scanf("%d %c %d", &tad1, &tc, &tad2);

AC代码

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<iostream> using namespace std; #define MAX 100010 struct Node { int next; bool have; }node[MAX]; int main() { int start1, start2, n; scanf("%d%d%d", &start1, &start2, &n); for (int i = 0;i < MAX;i++) { node[i].have = false; } for (int i = 0;i < n;i++) { int tad1, tad2; char tc; scanf("%d %c %d", &tad1, &tc, &tad2); node[tad1].next = tad2; } for (int i = start1;i != -1;i = node[i].next) node[i].have = true; for (int i = start2;i != -1;i = node[i].next) { if (node[i].have == true) { printf("%05d", i); return 0; } } printf("-1"); return 0; }
最新回复(0)