2 条题解

  • 0
    @ 2024-12-20 13:57:15
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int m,n,s,boys,girls;
    char st[60]={};
    
    signed main()
    {
    	scanf("%d",&m);
    	getchar();
    	while(m--)
    	{
    		fgets(st,sizeof st,stdin);
    		if(strlen(st)>50)
    			boys--;
    		else
    		{
    			sscanf(st,"%d %d",&n,&s);
    			if(n==48)
    				continue;
    			else if(n<=20)
    				girls++;
    			else
    				boys++;
    		}
    	}
    	printf("%s",boys>girls?"boys":"girls");
    	return 0;
    }
    
    • 0
      @ 2024-12-15 17:02:54
      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          int m;
          cin >> m;
          cin.ignore(); // 忽略第一行输入后的换行符
      
          int girlsScore = 0;
          int boysScore = 0;
      
          for (int i = 0; i < m; ++i) {
              string input;
              getline(cin, input);
      
              // 检查是否输入了多于50个字符,视为吵闹
              if (input.length() > 50) {
                  boysScore -= 1;
                  continue;
              }
      
              // 尝试解析输入为 学号+s 的格式
              int index = input.find(' ');
              if (index == string::npos || index == 0 || index == input.length() - 1) {
                  // 格式不正确,忽略该条输入(但这里其实已经处理了吵闹的情况,所以不会到这里)
                  // 或者可以视为无效输入,不加分也不扣分
                  continue;
              }
      
              int studentId = stoi(input.substr(0, index));
              int score = stoi(input.substr(index + 1));
      
              // 检查是否是48号同学的回答,如果是则忽略
              if (studentId == 48) {
                  continue;
              }
      
              // 根据学号判断是哪一组,并加分
              if (studentId <= 20) {
                  girlsScore += score;
              } else if (studentId <= 50) {
                  boysScore += score;
              }
              // 如果学号不在1-50范围内,则忽略该条输入(理论上不应该发生,因为题目已经限定了学号范围)
          }
      
          // 输出结果
          if (girlsScore > boysScore) {
              cout << "girls" << endl;
          } else {
              cout << "boys" << endl;
          }
      
          return 0;
      }
      
      • 1

      信息

      ID
      598
      时间
      1000ms
      内存
      256MiB
      难度
      10
      标签
      (无)
      递交数
      4
      已通过
      2
      上传者