Open roberthsu2003 opened 2 weeks ago
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// 定義結構
typedef struct student{
string name;
int chinese;
int english;
int math;
int sum = 0;
double average;
}Student;
int main() {
int n = 50;
int min = 50;
int max = 100;
Student tmp;
srand( time( NULL ) );
// 建立結構變數
Student students[n];
for(int i = 0; i < n; i++){
string name_ori = "學生" + to_string(i+1);
students[i].name = name_ori;
students[i].chinese = rand() % (max - min +1) + min;
students[i].english = rand() % (max - min +1) + min;
students[i].math = rand() % (max - min +1) + min;
students[i].sum = students[i].chinese + students[i].english + students[i].math;
students[i].average = students[i].sum / 3.0;
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if( students[i].average < students[j].average ){
tmp = students[i];
students[i] = students[j];
students[j] = tmp;
}
}
}
for(int i = 0; i < n; i++){
cout << "名次:" << i + 1 << endl;
cout << students[i].name << endl;
cout << "中文:" << students[i].chinese << " ";
cout << "英文:" << students[i].english << " ";
cout << "數學:" << students[i].math << "\n";
cout << "總分:" << students[i].sum << "\n";
printf("平均:%.2lf\n\n", students[i].average);
}
}
改為