1 条题解

  • 0
    @ 2025-4-12 21:50:59

    C :

    #include <stdio.h>
    int main()
    {
      while(1)
      {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
        {
          break;
        }
        int notes[26];
        for(int i=0;i<26;i++)
        {
          notes[i]=-1;
        }
        for(int i=0;i<n;i++)
        {
          char ch[4];
          scanf("%s",ch);
          if(ch[1]!='-')
          {
            notes[ch[1]-'A']=ch[0]-'A';
          }
          if(ch[2]!='-')
          {
            notes[ch[2]-'A']=ch[0]-'A';
          }
        }
        for(int i=0;i<m;i++)
        {
          char ch[4];
          scanf("%s",ch);
          int count=-1;
          int j=ch[0]-'A';
          while(notes[j]!=-1&&notes[j]!=ch[1]-'A')
          {
            count++;
            j=notes[j];
          }
          if(notes[j]!=-1)
          {
            for(int k=0;k<count;k++)
            {
              printf("great-");
            }
            if(count!=-1)
            {
              printf("grandparent\n");
            }
            else
            {
              printf("parent\n");
            }
            continue;
          }
          count=-1;
          j=ch[1]-'A';
          while(notes[j]!=-1&&notes[j]!=ch[0]-'A')
          {
            count++;
            j=notes[j];
          }
          if(notes[j]!=-1)
          {
            for(int k=0;k<count;k++)
            {
              printf("great-");
            }
            if(count!=-1)
            {
              printf("grandchild\n");
            }
            else
            {
              printf("child\n");
            }
            continue;
          }
          printf("-\n");
        }
      }
    }
    

    C++ :

    #include <cstdio>
    #include <cmath>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    map < char, char > child;
    
    int cal(char a, char b) {
        int ret = 0;
        while (a != child[a]) {
            if (a == b)
                return ret;
            a = child[a];
            ++ret;
        }
        return 0;
    }
    
    int main() {
        //freopen("data.in", "r", stdin);
        //freopen("data.out", "w", stdout);
        int n, m;
        while (EOF != scanf("%d %d", &n, &m)) {
            if (0 == n && 0 == m)
                break;
            child.clear();
            char s[10];
            while (n--) {
                scanf("%s", s);
                for (int i = 1; i < 3; ++i)
                    if ('-' != s[i])
                        child[ s[i] ] = s[0];
            }
            while (m--) {
                scanf("%s", s);
                int dis = cal(s[0], s[1]) - cal(s[1], s[0]);
                if (0 == dis)
                    puts("-");
                else {
                    for (int i = abs(dis); i > 2; --i)
                        printf("great-");
                    if (abs(dis) >= 2)
                        printf("grand");
                    puts( dis > 0 ? "parent" : "child" );
                }
            }
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    1870
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者