浙大PTA 学生顺序表的建立

完成对一个顺序表的建立,表中的每个元素是同学们的学号、姓名和三门课程的成绩,输入5个同学的信息,然后显示在屏幕上。(要求利用顺序表的基本操作)

输入格式:
首先收入学生人数5,然后依次输入5个学生的学号、姓名和三门课的成绩

输出格式:
输入5个学生的学号、姓名和三门课的成绩

输入样例:
5
01 张三 89 89 89
02 李四 90 90 90
03 王五 89 89 89
04 钱六 97 97 97
05 赵倩 90 90 90

输出样例:
1 张三 89.0 89.0 89.0
2 李四 90.0 90.0 90.0
3 王五 89.0 89.0 89.0
4 钱六 97.0 97.0 97.0
5 赵倩 90.0 90.0 90.0
思路:将输入的学生信息存储在结构体数组中,输入5个学生,按输入顺序输出学生信息。
ac代码:

#include 

using namespace std;
const int maxn = 10;
typedef struct LNode * List;
struct LNode{
    int id[maxn];
    string name[maxn];
    float score1[maxn], score2[maxn], score3[maxn];
    int last;
};

int main()
{
    List L;
    L = (List)malloc(sizeof(struct LNode));
    L->last = 0;
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        int id;
        string name;
        float score1, score2, score3;
        scanf("%d", &id);
        cin>>name;
        scanf("%f%f%f", &score1, &score2, &score3);
        L->id[i] = id;
        L->name[i] = name;
        L->score1[i] = score1;
        L->score2[i] = score2;
        L->score3[i] = score3;
        L->last++;
    }

    for(int i = 0; i < L->last; i++){
        printf("%d ", L->id[i]);
        cout<<L->name[i];
        printf(" %.1f %.1f %.1f\n", L->score1[i], L->score2[i], L->score3[i]);
    }

    return 0;
}

你可能感兴趣的:(数据结构pta)