pat 甲级 1032 Sharing

用while写会有一个一分的点不过,,,不知道为什么,,最后一个字母也测了,,想来想去可能是因为最后一个字母的地方会有问题吧,,,不清楚

/*while(num[n1].out != -1){     
        num[n1].key = true;
        n1 = num[n1].out;   
    }
    num[n1].key = true;
    while(num[n2].out != -1){

        if(num[n2].key == true){
            printf("%05d",n2);
            return 0;
        }
        else n2 = num[n2].out;

    }
    if(num[n2].key == true) {
            printf("%05d",n2);
            return 0;
    }*/

对于题意,目前可以通过的题解都是遇到一个相同字母的就输出了
但是个人觉得应该是字母相同而且之后的字符也都相同啊,,但是这样的话会突然麻烦很多,而且最开始我把两个串都提取出来发现对比到了不知道怎么输出他的地址,,

可能是我理解题意不对吧

还有 输入的时候 "%d %c %d 这里一定要有空格,不然读不进去,,不知道为撒,,

#include <cstdio>
#include <iostream>

using namespace std;

struct stu{

    int out;
    char a;
    bool key = false;
};

stu num[110000];
int n1, n2;
int m;

int main(){

    scanf("%d%d%d",&n1,&n2,&m);
    for(int i=0; i<m; i++){
        int z,x;
        char e;
        scanf("%d %c %d",&z,&e,&x);
        num[z].out = x;
        num[z].a = e;       
    }
    for(int i=n1; i!=-1; i = num[i].out) num[i].key = true;
    for(int i=n2; i!=-1; i = num[i].out) {
        if(num[i].key == true){
            printf("%05d",i);
            return 0;
        }
    }
    printf("-1");

    return 0;
}

思路是对的,落泪

猜你喜欢

转载自blog.csdn.net/mdzz_z/article/details/81609005
今日推荐