天天看點

pat 甲級 1032 Sharing

用while寫會有一個一分的點不過,,,不知道為什麼,,最後一個字母也測了,,想來想去可能是因為最後一個字母的地方會有問題吧,,,不清楚

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

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

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

對于題意,目前可以通過的題解都是遇到一個相同字母的就輸出了

但是個人覺得應該是字母相同而且之後的字元也都相同啊,,但是這樣的話會突然麻煩很多,而且最開始我把兩個串都提取出來發現對比到了不知道怎麼輸出他的位址,,

可能是我了解題意不對吧

還有 輸入的時候

"%d %c %d

這裡一定要有空格,不然讀不進去,,不知道為撒,,

#include <cstdio>
#include <iostream>

using namespace std;

struct stu{

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

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

int main(){

    scanf("%d%d%d",&n1,&n2,&m);
    for(int i=; 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!=-; i = num[i].out) num[i].key = true;
    for(int i=n2; i!=-; i = num[i].out) {
        if(num[i].key == true){
            printf("%05d",i);
            return ;
        }
    }
    printf("-1");

    return ;
}
           

思路是對的,落淚

PAT