鱼C论坛

 找回密码
 立即注册
查看: 1835|回复: 2

[已解决]结构体排序求助!!!

[复制链接]
发表于 2022-11-14 16:17:18 | 显示全部楼层 |阅读模式
20鱼币
在结构体进行排序前可以正常输出,排序之后点运行,输入数据之后会直接退出,有没有大佬可以帮我看看哪里出了问题,怎么解决?


  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4. using std::string;
  5. using namespace std;

  6. struct Student{
  7.         string id;
  8.         int M;
  9.         int m[15];
  10.         int score=0;
  11. }s[1010];

  12. bool cmp(Student a,Student b){
  13.         if(a.score == b.score)
  14.                 return a.id < b.id;
  15.         return a.score > b.score;
  16. }


  17. int main() {
  18.         int N, M, G;
  19.         int m[15];
  20.         while (scanf("%d %d %d", &N, &M, &G) != EOF) {
  21.                 int num = 0;
  22.                 if(N == 0)
  23.                         break;
  24.                 for (int i = 0; i < M; i++)
  25.                         scanf("%d", &m[i]);
  26.                 for (int i = 0; i < N; i++){
  27.                         scanf("%s %d",&s[i].id,&s[i].M);
  28.                         for(int j = 0; j < s[i].M; j++)
  29.                                 scanf("%d",&s[i].m[j]);
  30.                 }
  31.                 for (int i = 0; i < N; i++){
  32.                         for(int j = 0; j < s[i].M; j++){
  33.                                 s[i].score += m[s[i].m[j] - 1];
  34.                         }
  35.                         if(s[i].score >= G){
  36.                                 num += 1;
  37.                         }
  38.                 }
  39.                 printf("%d\n",num);
  40.                 sort(s, s+N, cmp);
  41.                 for(int i=0;i<num;i++){
  42.                         printf("%s %d\n",&s[i].id,s[i].score);
  43.                 }
  44.         }
  45.         return 0;
  46. }
复制代码


最佳答案
2022-11-14 16:17:19
看看这些有没有帮助
  1. test.cpp:32:39: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
  2.                         scanf("%s %d",&s[i].id,&s[i].M);
  3.                                ~~     ^~~~~~~~
  4. test.cpp:47:42: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
  5.                         printf("%s %d\n",&s[i].id,s[i].score);
  6.                                 ~~       ^~~~~~~~
  7. 2 warnings generated.
复制代码
微信图片_20221114161355.png
微信图片_20221114161532.png

最佳答案

查看完整内容

看看这些有没有帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-14 16:17:19 | 显示全部楼层    本楼为最佳答案   
看看这些有没有帮助
  1. test.cpp:32:39: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
  2.                         scanf("%s %d",&s[i].id,&s[i].M);
  3.                                ~~     ^~~~~~~~
  4. test.cpp:47:42: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
  5.                         printf("%s %d\n",&s[i].id,s[i].score);
  6.                                 ~~       ^~~~~~~~
  7. 2 warnings generated.
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-11-14 16:49:45 | 显示全部楼层
dolly_yos2 发表于 2022-11-14 16:27
看看这些有没有帮助

将 结构体中 id 的类型改为 char 解决了 谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-25 22:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表