首页 > 其他 > 详细

[PAT] A1032 Sharing

时间:2020-06-01 13:19:32      阅读:37      评论:0      收藏:0      [点我收藏+]

静态链表。见算法笔记第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;
}

[PAT] A1032 Sharing

原文:https://www.cnblogs.com/yue36/p/13024465.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!