为什么一直答案错误,vs上没问题啊

qq_39389770 2017-10-27 08:58:04
There are some students’ grade records, in format of “name grade”. In order to make the teacher to view and manage all the records easily, you are asked to develop a program to sort the records first by grade (in descending order), and second by name (in alphabetic order). That means the sorted records will be from the highest grade to lowest grade; and if two students have the same grade, whose name appeared earlier in a dictionary will be shown earlier.

输入
8 grade records, one record on per line.

Only letters and digits appear in the name of a student (i.e. there will be no space in a name). All grades are integers.

输出
The 8 sorted records, one record per line.

样例输入
WangYi 90
LiMing 90
HuRui 50
ZhangLi 65
LiLei 80
ZhangFei 65
ZhaoSan 70
ChenZi 100
样例输出
ChenZi 100
LiMing 90
WangYi 90
LiLei 80
ZhaoSan 70
ZhangFei 65
ZhangLi 65
HuRui 50




#include<iostream>
#include<math.h>
#include<malloc.h>
#include<string.h>
//#include "finished.h"
#include <iomanip>
#include <ctype.h>
using namespace std;

struct Student
{
char name[30];
int grade;
};

Student Stu[30];


int main()
{
int i,j;
for (i = 0; i <8; i++)
cin >> Stu[i].name >> Stu[i].grade;

for (i = 0; i < 7; i++)
{
for (j = i + 1; j <8; j++)
{
if (Stu[i].grade < Stu[j].grade)
{
char temp1[30];
strcpy(temp1, Stu[i].name);
strcpy(Stu[i].name, Stu[j].name);
strcpy(Stu[j].name, temp1);
int temp;
temp = Stu[i].grade;
Stu[i].grade = Stu[j].grade;
Stu[j].grade = temp;
}
}
}

for (i = 0; i < 7; i++)
{
for (j = i + 1; j <8; j++)
{
if ((Stu[i].grade == Stu[j].grade) && strcmp(Stu[i].name, Stu[j].name) == 1)
{
char temp1[30];
strcpy(temp1, Stu[i].name);
strcpy(Stu[i].name, Stu[j].name);
strcpy(Stu[j].name, temp1);
int temp;
temp = Stu[i].grade;
Stu[i].grade = Stu[j].grade;
Stu[j].grade = temp;
}
}
}






for (i = 0; i < 8; i++)
cout<< Stu[i].name<<" "<< Stu[i].grade<<endl;







//char str[300];
// cin.getline(str, 300);
//strlwr(str);
/* cout << char(tolower('A')) << endl;*/



/*cout << right << fixed;
cout.precision(2);

cout<<"("<<x[n]<<" "<<y[n]<<" "<<z[n]<<") "<<min_d<<endl;*/


return 0;
}


...全文
198 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
怆悲、璨 2019-07-25
  • 打赏
  • 举报
回复
博主求倒立摆的代码,没有积分只能在这找您,1546111813@qq.com最近在做这个项目我只有大一,万分感谢
自信男孩 2017-10-27
  • 打赏
  • 举报
回复
#include<iostream>
#include<math.h>
#include<malloc.h>
#include<string.h>
//#include "finished.h"
#include <iomanip>
#include <ctype.h>

using namespace std;

struct Student
{
    char name[30];
    int grade;
};

Student Stu[30];


int main()
{
    int i,j;
    struct Student tmp;
    for (i = 0; i < 8; i++)
        cin >> Stu[i].name >>  Stu[i].grade;

    for (i = 0; i < 7; i++)
    {
        for (j = i + 1; j <8; j++)
        {
            if (Stu[i].grade < Stu[j].grade)
            {
                tmp = Stu[i];
                Stu[i] = Stu[j];
                Stu[j] = tmp;
                /*
                char temp1[30];
                strcpy(temp1, Stu[i].name);
                strcpy(Stu[i].name, Stu[j].name);
                strcpy(Stu[j].name, temp1);
                int temp;
                temp = Stu[i].grade;
                Stu[i].grade = Stu[j].grade;
                Stu[j].grade = temp;
                */
            }
        }
    }

    for (i = 0; i < 7; i++)
    {
        for (j = i + 1; j <8; j++)
        {
            if ((Stu[i].grade == Stu[j].grade) && strcmp(Stu[i].name, Stu[j].name) == 1)
            {
                tmp = Stu[i];
                Stu[i] = Stu[j];
                Stu[j] = tmp;
                /*
                char temp1[30];
                strcpy(temp1, Stu[i].name);
                strcpy(Stu[i].name, Stu[j].name);
                strcpy(Stu[j].name, temp1);
                int temp;
                temp = Stu[i].grade;
                Stu[i].grade = Stu[j].grade;
                Stu[j].grade = temp;
                */
            }
        }
    }

    cout<<endl;
    for (i = 0; i < 8; i++)
        cout<< Stu[i].name<<" "<< Stu[i].grade<<endl;

    //char str[300];
    //   cin.getline(str, 300);
    //strlwr(str);
    /*   cout << char(tolower('A')) << endl;*/



    /*cout << right << fixed;
      cout.precision(2);

      cout<<"("<<x[n]<<" "<<y[n]<<" "<<z[n]<<") "<<min_d<<endl;*/


    return 0;
}
结构体赋值没必要每个成员都交换,直接定义一个结构体中间变量即可。

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧