2 条题解

  • 2
    @ 2024-2-12 16:49:45

    来发c++的,闲着没事写了个setset玩,才发现用法几乎忘光了,所以写了个及其拙劣的解法,二话不说先上代码思路在后面。

    #include <bits/stdc++.h>
    using namespace std;
    int n, m;
    struct T
    {
        string name, id;
    };
    bool operator <(T a, T b)
    {
        return a.id > b.id;
    }
    set<T> s1, s2;
    bool finds1(string x)
    {
        for (auto b : s1)
            if (b.name == x || b.id == x)
                return 1;
        return 0;
    }
    bool finds2(string x)
    {
        for (auto b : s2)
            if (b.name == x || b.id == x)
                return 1;
        return 0;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cout << "你竟然抄题解!!!" << endl;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            string name, id, other;
            cin >> name >> id >> other;
            ((other == "M") ? s1.insert({name, id}) : s2.insert({name, id}));
        }
        cin >> m;
        for (int i = 1; i <= m; i++)
        {
            string a, b;
            cin >> a >> b;
            if (finds1(a) && finds2(b))
                cout << "Y" << endl;
            else if (finds1(b) && finds2(a))
                cout << "Y" << endl;
            else
                cout << "N" << endl;
        }
        return 0;
    }
    

    这段代码中,我直接模拟了整个过程,结构体下的一个小于运算符的重载是用来给setset定义排列顺序的,好像setset没法个字符串排序来着,具体我也忘了,只需要记住这是一个防止CECE的东西即可,至于finds1finds1finds2finds2,这两个只是为了查找姓名或学号的,不必多想,反正姓名和学号是不可能相同的,只需记住这东西就是用来找男女生中是否有这个人了,如果有,就返回1,否则返回0,可能有人会问,为什么不判断输入的是学号还是名字呢?答案便在finds1finds1finds2finds2里了,因为我不管它输入的是名字还是学号,我只需要判断它是在名字里存在还是在学好里存在,如果输入的a和b是异性,那就输出Y,否则输出N,有人可能会问,既然你不管它输入的是字符还是数字,那你为什么还要在做这些操作之前些判断呢?因为当时我思路有误,写了这么一个,后来发现了现在这思路,但是不想删了,所以就留着了,以起到警示我自己要多多思考,至于我为什么用setset,是因为我原本想用setset中自带的findfind函数来查找,但是我竟然忘了定义成结构体是如何进行findfind了,所以就改了,懒得改就直接用了setset来做了,并且我喜欢用autoautosetsetmapmap进行遍历,毕竟我初学autoauto时前三次使用就是用在了这两个数据结构上的

    • 0
      @ 2023-8-2 22:35:19

      嗨嗨嗨,我又来了! 今天咱们聊一聊# 949. 【基础】新生舞会


      思路

      这题看着很简单:开三个列表,分别记录姓名,学号,性别,然后需要时从输入中找出属于前两者中的哪个,求出索引,带入性别列表中判断即可,于是,我立马写出以下代码(60分)

      a=input()
      name=[]
      class_num=[]
      F_and_M=[]
      for i in range(int(a)):
          b=input().split()
          name.append(b[0])
          class_num.append(b[1])
          F_and_M.append(b[2])
      q=int(input())
      for i in range(q):
          p=input().split()
          student1=p[0]
          student2=p[1]
          if ((student1 in class_num) or (student1 in name)) and ((student2 in class_num) or (student2 in name)):
              try:
                  student1=int(student1)
              except:#str
                  F_and_M_s1=name.index(student1) 
              else: 
                  F_and_M_s1=class_num.index(str(student1)) 
              '''
              这里解释一下try……except……的作用,
              它们是异常调试语句,try内代码块像普通代码一样执行,但产生的错误会被except捕获,由except下的代码块处理。
              至于else语句,作用类if-elif-else,故无需过多赘述。
              '''
              try:
                  student2=int(student2)
              except:#str
                  F_and_M_s2=name.index(student2) 
              else:
                  F_and_M_s2=class_num.index(str(student2)) 
              if F_and_M[F_and_M_s1] != F_and_M[F_and_M_s2]:
                  print('Y')
              else:
                  print('N')
          else:
              print('N')
      result:60 score(grass!)
      

      image 一开始我一脸懵:不是,这啥啊这是,怎么就错了呢?直到我老老实实地把样例拿出运行,才发现…… image image 于是,我揪出了Bug,然后就AC了,至于哪里有Bug,我不说你猜!(tips:python的int函数返回值会把前导0清除


      Loading:29/100………^_^

      • 1

      信息

      ID
      949
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      53
      已通过
      11
      上传者