1 条题解
-
0
C :
#include<stdio.h> #include <stdlib.h> struct Student{ int id; char name[1000]; int score; }; void main() { typedef struct Student Stu; int n,i,j; scanf("%d",&n); Stu a[n]; for(i=0;i<n;i++){ Stu* s = (Stu*)malloc(sizeof(Stu)); scanf("%d",&s->id); scanf("%s",s->name); scanf("%d",&s->score); a[i] = *s; free(s); } //冒泡排序 for(i=1;i<=n-1;i++){ for(j=0;j<=n-2;j++){ if(a[j].score < a[j+1].score){ Stu temp; temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; }else if(a[j].score == a[j+1].score){ if(a[j].id>a[j+1].id){ Stu temp; temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } for(i=0;i<n;i++){ printf("%d %s %d\n",a[i].id,a[i].name,a[i].score); } }
C++ :
#include <bits/stdc++.h> using namespace std; //声明结构体类型 struct Student{ int num; char name[20]; float score; }; int main(){ //定义结构体数组并赋值 struct Student stu[100]; struct Student t; int i,j,n; cin>>n; for(i = 0;i < n;i++){ cin>>stu[i].num>>stu[i].name>>stu[i].score; } for(i = 0;i < n;i++){ for(j = 0;j < n - i - 1;j++){ if((stu[j].score < stu[j + 1].score) || (stu[j].score == stu[j + 1].score && stu[j].num > stu[j + 1].num)){ t = stu[j]; stu[j] = stu[j + 1]; stu[j + 1] = t; } } } //输出所有人的信息 for(i = 0;i < n;i++){ cout<<stu[i].num<<" "<<stu[i].name<<" "<<stu[i].score<<endl; } }
Python :
class Student: def __init__(self, num, name, score): self.num = num self.name = name self.score = score def mysort(lst): # 冒泡 i: n-1次 j: n-i次 # i: 1->0 ~ n-1->n-1-1 j: 1~n-i # for i in range(0, len(lst) - 1): for j in range(0, n - i - 1): # j 下标的成绩 < j+1 下标的成绩 if lst[j].score < lst[j + 1].score: temp = lst[j] lst[j] = lst[j + 1] lst[j + 1] = temp elif lst[j].score == lst[j + 1].score and lst[j].num > lst[j + 1].num: temp = lst[j] lst[j] = lst[j + 1] lst[j + 1] = temp if __name__ == '__main__': lst = [] n = int(input()) for i in range(n): t = input().split() num = int(t[0]) name = t[1] score = int(t[2]) s = Student(num, name, score) lst.append(s) mysort(lst) for k in lst: print(str(k.num) + " " + k.name + " " + str(k.score))
- 1
信息
- ID
- 2334
- 时间
- 1000ms
- 内存
- 16MiB
- 难度
- 7
- 标签
- 递交数
- 169
- 已通过
- 45
- 上传者