完成一部分
This commit is contained in:
parent
c5a4bf6456
commit
ab2e91de78
2
5/main.c
2
5/main.c
|
@ -46,5 +46,5 @@ int main() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尚未修改到课程版本
|
|
||||||
// Ref: P1996 约瑟夫问题
|
// Ref: P1996 约瑟夫问题
|
||||||
|
// 已修改为实验版本
|
120
6/main.c
120
6/main.c
|
@ -1,66 +1,91 @@
|
||||||
#include <bits/stdc++.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
using namespace std;
|
#include <string.h>
|
||||||
|
|
||||||
int se; // 学期总数
|
int se; // 学期总数
|
||||||
int sx; // 学分上限
|
int sx; // 学分上限
|
||||||
int cl; // 课程数量
|
int cl; // 课程数量
|
||||||
int ren[12], fe[12], re[12][105];
|
int ren[12], fe[12], re[12][105];
|
||||||
|
|
||||||
struct Class {
|
typedef struct {
|
||||||
string id; // 课程编号
|
char *id; // 课程编号
|
||||||
int sx; // 学分
|
int sx; // 学分
|
||||||
int xq;
|
int xq;
|
||||||
int sel; // 是否已经选择
|
int sel; // 是否已经选择
|
||||||
int pren, nexn; // 先修课程数量
|
int pren, nexn; // 先修课程数量
|
||||||
int ppren; // use for topo
|
int ppren; // use for topo
|
||||||
string pre[105]; // 先修课程
|
char *pre[105]; // 先修课程
|
||||||
string next[105]; // 后续课程
|
char *next[105]; // 后续课程
|
||||||
} ss[105];
|
} Class;
|
||||||
|
|
||||||
void pt(int detail = 0) {
|
Class ss[105];
|
||||||
|
|
||||||
|
Class *queue[105];
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
|
||||||
|
void push(Class *c) { queue[tail++] = c; }
|
||||||
|
|
||||||
|
Class *pop() { return queue[head++]; }
|
||||||
|
|
||||||
|
int empty() { return head == tail; }
|
||||||
|
|
||||||
|
Class *front() { return queue[head]; }
|
||||||
|
|
||||||
|
void pt(int detail) {
|
||||||
// print
|
// print
|
||||||
for (int i = 0; i < cl; i++) {
|
for (int i = 0; i < cl; i++) {
|
||||||
cout << ss[i].id << " " << ss[i].sx << " " << ss[i].pren << " "
|
printf("%s %d %d %d\n", ss[i].id, ss[i].sx, ss[i].pren, ss[i].nexn);
|
||||||
<< ss[i].nexn << endl;
|
|
||||||
if (!detail)
|
if (!detail)
|
||||||
continue;
|
continue;
|
||||||
if (ss[i].pren > 0) {
|
if (ss[i].pren > 0) {
|
||||||
cout << "pre:" << endl;
|
printf("pre:\n");
|
||||||
for (int j = 0; j < ss[i].pren; j++) {
|
for (int j = 0; j < ss[i].pren; j++) {
|
||||||
cout << ss[i].pre[j] << " ";
|
printf("%s ", ss[i].pre[j]);
|
||||||
}
|
}
|
||||||
cout << endl;
|
printf("\n");
|
||||||
}
|
}
|
||||||
if (ss[i].nexn > 0) {
|
if (ss[i].nexn > 0) {
|
||||||
cout << "next:" << endl;
|
printf("next:\n");
|
||||||
for (int j = 0; j < ss[i].nexn; j++) {
|
for (int j = 0; j < ss[i].nexn; j++) {
|
||||||
cout << ss[i].next[j] << " ";
|
printf("%s ", ss[i].next[j]);
|
||||||
}
|
}
|
||||||
cout << endl;
|
printf("\n");
|
||||||
}
|
}
|
||||||
cout << endl;
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 寻找 id
|
// 寻找 id
|
||||||
int fd(string id) {
|
int fd(char *id) {
|
||||||
for (int i = 0; i < cl; i++) {
|
for (int i = 0; i < cl; i++) {
|
||||||
if (ss[i].id == id) {
|
if (strcmp(ss[i].id, id) == 0) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *substr(char *str, int start, int cnt) {
|
||||||
|
char *res = (char *)malloc(sizeof(char) * (cnt + 1));
|
||||||
|
for (int i = 0; i < cnt; i++) {
|
||||||
|
res[i] = str[start + i];
|
||||||
|
}
|
||||||
|
res[cnt] = '\0';
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
// 去除字符串两边空格和换行符
|
// 去除字符串两边空格和换行符
|
||||||
string ql(string s) {
|
char *ql(char *s) {
|
||||||
int l = 0, r = s.length() - 1;
|
char *res = (char *)malloc(sizeof(char) * (strlen(s) + 1));
|
||||||
|
int l = 0, r = strlen(s) - 1;
|
||||||
while (s[l] == ' ' || s[l] == '\n')
|
while (s[l] == ' ' || s[l] == '\n')
|
||||||
l++;
|
l++;
|
||||||
while (s[r] == ' ' || s[r] == '\n')
|
while (s[r] == ' ' || s[r] == '\n')
|
||||||
r--;
|
r--;
|
||||||
return s.substr(l, r - l + 1);
|
for (int i = 0; i <= r - l; i++) {
|
||||||
|
res[i] = s[l + i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 寻找先修课程的最迟学期
|
// 寻找先修课程的最迟学期
|
||||||
|
@ -95,26 +120,25 @@ int xz2(int i) {
|
||||||
void topo() {
|
void topo() {
|
||||||
int sorted[105] = {0};
|
int sorted[105] = {0};
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
queue<Class*> q;
|
|
||||||
for (int i = 0; i < cl; i++) {
|
for (int i = 0; i < cl; i++) {
|
||||||
if (ss[i].ppren == 0) {
|
if (ss[i].ppren == 0) {
|
||||||
q.push(&ss[i]);
|
push(&ss[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (!q.empty()) {
|
while (!empty()) {
|
||||||
Class* tmp = q.front();
|
Class *tmp = front();
|
||||||
sorted[cnt] = fd(tmp->id);
|
sorted[cnt] = fd(tmp->id);
|
||||||
q.pop();
|
pop();
|
||||||
cnt++;
|
cnt++;
|
||||||
for (int i = 0; i < tmp->nexn; i++) {
|
for (int i = 0; i < tmp->nexn; i++) {
|
||||||
ss[fd(tmp->next[i])].ppren--;
|
ss[fd(tmp->next[i])].ppren--;
|
||||||
if (ss[fd(tmp->next[i])].ppren == 0) {
|
if (ss[fd(tmp->next[i])].ppren == 0) {
|
||||||
q.push(&ss[fd(tmp->next[i])]);
|
push(&ss[fd(tmp->next[i])]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cnt != cl) {
|
if (cnt != cl) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
Class newss[105];
|
Class newss[105];
|
||||||
|
@ -172,42 +196,44 @@ void pte() {
|
||||||
for (int i = 0; i < se; i++) {
|
for (int i = 0; i < se; i++) {
|
||||||
if (ren[i] == 0)
|
if (ren[i] == 0)
|
||||||
continue;
|
continue;
|
||||||
cout << "学期 " << i + 1 << " 学分 " << fe[i] << " 课程数 " << ren[i] << " 课程:";
|
printf("学期 %d 学分 %d 课程数 %d 课程:", i + 1, fe[i], ren[i]);
|
||||||
for (int j = 0; j < ren[i]; j++) {
|
for (int j = 0; j < ren[i]; j++) {
|
||||||
cout << ss[re[i][j]].id << " ";
|
printf("%s ", ss[re[i][j]].id);
|
||||||
}
|
}
|
||||||
cout << endl;
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
freopen("in.txt", "r", stdin);
|
freopen("in.txt", "r", stdin);
|
||||||
cin >> se >> sx;
|
scanf("%d%d", &se, &sx);
|
||||||
if (se <= 0 || sx <= 0 || se > 6 || sx > 10) {
|
if (se <= 0 || sx <= 0 || se > 6 || sx > 10) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
cin >> cl;
|
scanf("%d", &cl);
|
||||||
if (cl <= 0 || cl > 12) {
|
if (cl <= 0 || cl > 12) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < cl; i++) {
|
for (int i = 0; i < cl; i++) {
|
||||||
cin >> ss[i].id;
|
char *id = (char *)malloc(sizeof(char) * 105);
|
||||||
|
scanf("%s", id);
|
||||||
ss[i].id = ql(ss[i].id);
|
ss[i].id = ql(ss[i].id);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < cl; i++) {
|
for (int i = 0; i < cl; i++) {
|
||||||
cin >> ss[i].sx >> ss[i].pren;
|
scanf("%d", &ss[i].sx);
|
||||||
|
scanf("%d", &ss[i].pren);
|
||||||
ss[i].ppren = ss[i].pren;
|
ss[i].ppren = ss[i].pren;
|
||||||
for (int j = 0; j < ss[i].pren; j++) {
|
for (int j = 0; j < ss[i].pren; j++) {
|
||||||
string tmp;
|
char *tmp = (char *)malloc(sizeof(char) * 105);
|
||||||
cin >> tmp;
|
scanf("%s", tmp);
|
||||||
tmp = ql(tmp);
|
tmp = ql(tmp);
|
||||||
if (tmp.size() == 0)
|
if (strlen(tmp) == 0)
|
||||||
continue;
|
continue;
|
||||||
int fid = fd(tmp);
|
int fid = fd(tmp);
|
||||||
if (fid == -1) {
|
if (fid == -1) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
ss[i].pre[j] = ss[fid].id;
|
ss[i].pre[j] = ss[fid].id;
|
||||||
|
@ -252,7 +278,7 @@ int main() {
|
||||||
}
|
}
|
||||||
// 判断是否全部选中
|
// 判断是否全部选中
|
||||||
if (ttc != cl) {
|
if (ttc != cl) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// for (int i = 0; i < cl; i++) {
|
// for (int i = 0; i < cl; i++) {
|
||||||
|
@ -260,9 +286,9 @@ int main() {
|
||||||
// }
|
// }
|
||||||
// pte();
|
// pte();
|
||||||
int kind;
|
int kind;
|
||||||
cin >> kind;
|
scanf("%d", &kind);
|
||||||
if (kind != 1 && kind != 2) {
|
if (kind != 1 && kind != 2) {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// 输入分配方式,1是负担均匀,2是尽早学习
|
// 输入分配方式,1是负担均匀,2是尽早学习
|
||||||
|
@ -271,7 +297,7 @@ int main() {
|
||||||
} else if (kind == 2) {
|
} else if (kind == 2) {
|
||||||
// 本来就是今早分配的,无需更改
|
// 本来就是今早分配的,无需更改
|
||||||
} else {
|
} else {
|
||||||
cout << "error" << endl;
|
printf("error\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// 输出最终结果
|
// 输出最终结果
|
||||||
|
|
|
@ -0,0 +1,282 @@
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int se; // 学期总数
|
||||||
|
int sx; // 学分上限
|
||||||
|
int cl; // 课程数量
|
||||||
|
int ren[12], fe[12], re[12][105];
|
||||||
|
|
||||||
|
struct Class {
|
||||||
|
string id; // 课程编号
|
||||||
|
int sx; // 学分
|
||||||
|
int xq;
|
||||||
|
int sel; // 是否已经选择
|
||||||
|
int pren, nexn; // 先修课程数量
|
||||||
|
int ppren; // use for topo
|
||||||
|
string pre[105]; // 先修课程
|
||||||
|
string next[105]; // 后续课程
|
||||||
|
} ss[105];
|
||||||
|
|
||||||
|
void pt(int detail = 0) {
|
||||||
|
// print
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
cout << ss[i].id << " " << ss[i].sx << " " << ss[i].pren << " "
|
||||||
|
<< ss[i].nexn << endl;
|
||||||
|
if (!detail)
|
||||||
|
continue;
|
||||||
|
if (ss[i].pren > 0) {
|
||||||
|
cout << "pre:" << endl;
|
||||||
|
for (int j = 0; j < ss[i].pren; j++) {
|
||||||
|
cout << ss[i].pre[j] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
if (ss[i].nexn > 0) {
|
||||||
|
cout << "next:" << endl;
|
||||||
|
for (int j = 0; j < ss[i].nexn; j++) {
|
||||||
|
cout << ss[i].next[j] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 寻找 id
|
||||||
|
int fd(string id) {
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
if (ss[i].id == id) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 去除字符串两边空格和换行符
|
||||||
|
string ql(string s) {
|
||||||
|
int l = 0, r = s.length() - 1;
|
||||||
|
while (s[l] == ' ' || s[l] == '\n')
|
||||||
|
l++;
|
||||||
|
while (s[r] == ' ' || s[r] == '\n')
|
||||||
|
r--;
|
||||||
|
return s.substr(l, r - l + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 寻找先修课程的最迟学期
|
||||||
|
int xz(int i) {
|
||||||
|
// cout << ss[i].id << " " << ss[i].pren << endl;
|
||||||
|
if (ss[i].pren == 0)
|
||||||
|
return 0;
|
||||||
|
int maxn = 0;
|
||||||
|
for (int j = 0; j < ss[i].pren; j++) {
|
||||||
|
int fid = fd(ss[i].pre[j]);
|
||||||
|
if (ss[fid].xq > maxn) {
|
||||||
|
maxn = ss[fid].xq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maxn + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 寻找后修课程的最早学期
|
||||||
|
int xz2(int i) {
|
||||||
|
if (ss[i].nexn == 0)
|
||||||
|
return se;
|
||||||
|
int minn = se;
|
||||||
|
for (int j = 0; j < ss[i].nexn; j++) {
|
||||||
|
int fid = fd(ss[i].next[j]);
|
||||||
|
if (ss[fid].xq < minn) {
|
||||||
|
minn = ss[fid].xq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minn;
|
||||||
|
}
|
||||||
|
// 拓扑排序,将排好序的id 放入sorted
|
||||||
|
void topo() {
|
||||||
|
int sorted[105] = {0};
|
||||||
|
int cnt = 0;
|
||||||
|
queue<Class *> q;
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
if (ss[i].ppren == 0) {
|
||||||
|
q.push(&ss[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (!q.empty()) {
|
||||||
|
Class *tmp = q.front();
|
||||||
|
sorted[cnt] = fd(tmp->id);
|
||||||
|
q.pop();
|
||||||
|
cnt++;
|
||||||
|
for (int i = 0; i < tmp->nexn; i++) {
|
||||||
|
ss[fd(tmp->next[i])].ppren--;
|
||||||
|
if (ss[fd(tmp->next[i])].ppren == 0) {
|
||||||
|
q.push(&ss[fd(tmp->next[i])]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cnt != cl) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
Class newss[105];
|
||||||
|
// 将 ss 顺序改为 sorted 顺序
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
newss[i] = ss[sorted[i]];
|
||||||
|
}
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
ss[i] = newss[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void avgf() {
|
||||||
|
// 计算平均每学期学习数量
|
||||||
|
int avg = cl / se;
|
||||||
|
// cout << avg << endl;
|
||||||
|
// 按从后向前,如果学期学习数量小于平均值,则将前面的课程往后移动
|
||||||
|
for (int i = se - 1; i >= 1; i--) {
|
||||||
|
if (ren[i] < avg) {
|
||||||
|
for (int j = i - 1; j >= 0; j--) {
|
||||||
|
// cout << j << " " << ren[j] << endl;
|
||||||
|
if (ren[j] <= 0)
|
||||||
|
continue;
|
||||||
|
int flag = 0;
|
||||||
|
for (int ii = 0; ii < ren[j] && ren[i] < avg; ii++) {
|
||||||
|
int c = re[j][ii];
|
||||||
|
// cout << " " << ss[c].id << " " << xz2(2)<< endl;
|
||||||
|
// cout << c << endl;
|
||||||
|
if (xz2(c) > i) {
|
||||||
|
// 把 c 课程放到 i 学期
|
||||||
|
re[i][ren[i]++] = c;
|
||||||
|
ss[c].xq = i;
|
||||||
|
// 把 c 课程从 j 学期删除
|
||||||
|
for (int jj = ii; jj < ren[j] - 1; jj++) {
|
||||||
|
re[j][jj] = re[j][jj + 1];
|
||||||
|
}
|
||||||
|
ren[j]--;
|
||||||
|
flag = 1;
|
||||||
|
}
|
||||||
|
// 如果 i 学期中的课程数量大于平均值,则跳出循环
|
||||||
|
if (ren[i] >= avg) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (flag == 1) {
|
||||||
|
ii = -1;
|
||||||
|
flag = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pte() {
|
||||||
|
for (int i = 0; i < se; i++) {
|
||||||
|
if (ren[i] == 0)
|
||||||
|
continue;
|
||||||
|
cout << "学期 " << i + 1 << " 学分 " << fe[i] << " 课程数 " << ren[i]
|
||||||
|
<< " 课程:";
|
||||||
|
for (int j = 0; j < ren[i]; j++) {
|
||||||
|
cout << ss[re[i][j]].id << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
freopen("in.txt", "r", stdin);
|
||||||
|
cin >> se >> sx;
|
||||||
|
if (se <= 0 || sx <= 0 || se > 6 || sx > 10) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
cin >> cl;
|
||||||
|
if (cl <= 0 || cl > 12) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
cin >> ss[i].id;
|
||||||
|
ss[i].id = ql(ss[i].id);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
// 学分和前置课程
|
||||||
|
cin >> ss[i].sx >> ss[i].pren;
|
||||||
|
ss[i].ppren = ss[i].pren;
|
||||||
|
for (int j = 0; j < ss[i].pren; j++) {
|
||||||
|
string tmp;
|
||||||
|
cin >> tmp;
|
||||||
|
tmp = ql(tmp);
|
||||||
|
if (tmp.size() == 0)
|
||||||
|
continue;
|
||||||
|
int fid = fd(tmp);
|
||||||
|
if (fid == -1) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
ss[i].pre[j] = ss[fid].id;
|
||||||
|
ss[fid].next[ss[fid].nexn++] = ss[i].id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// pt(1);
|
||||||
|
topo();
|
||||||
|
// pt(1);
|
||||||
|
for (int i = 0; i < cl; i++) {
|
||||||
|
ss[i].xq = xz(i);
|
||||||
|
}
|
||||||
|
int ttc = 0;
|
||||||
|
for (int i = 0; i < se; i++) {
|
||||||
|
for (int j = 0; j < cl; j++) {
|
||||||
|
// 判断是否已选择
|
||||||
|
if (ss[j].sel == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 判断是否学分超过上限
|
||||||
|
if (ss[j].sx + fe[i] > sx) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 判断是否先修课程已经学习
|
||||||
|
int flag = 0;
|
||||||
|
for (int k = 0; k < ss[j].pren; k++) {
|
||||||
|
int fid = fd(ss[j].pre[k]);
|
||||||
|
if (ss[fid].sel == 0) {
|
||||||
|
flag = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (flag == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 选择该课程
|
||||||
|
ss[j].sel = 1;
|
||||||
|
fe[i] += ss[j].sx;
|
||||||
|
re[i][ren[i]++] = j;
|
||||||
|
ttc++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 判断是否全部选中
|
||||||
|
if (ttc != cl) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
// for (int i = 0; i < cl; i++) {
|
||||||
|
// cout << ss[i].id << " " << ss[i].xq << endl;
|
||||||
|
// }
|
||||||
|
// pte();
|
||||||
|
int kind;
|
||||||
|
cin >> kind;
|
||||||
|
if (kind != 1 && kind != 2) {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
// 输入分配方式,1是负担均匀,2是尽早学习
|
||||||
|
if (kind == 1) {
|
||||||
|
avgf();
|
||||||
|
} else if (kind == 2) {
|
||||||
|
// 本来就是今早分配的,无需更改
|
||||||
|
} else {
|
||||||
|
cout << "error" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
// 输出最终结果
|
||||||
|
pte();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
7
|
||||||
|
004 L4 89 86 85 86.67
|
||||||
|
002 L2 89 67 88 81.00
|
||||||
|
001 L1 78 77 90 81.00
|
||||||
|
005 L5 67 88 76 77.00
|
||||||
|
007 L6 78 76 70 74.67
|
||||||
|
003 L3 56 66 78 66.00
|
||||||
|
006 L6 45 54 67 55.00
|
|
@ -0,0 +1,8 @@
|
||||||
|
7
|
||||||
|
001 L1 78 77 90
|
||||||
|
002 L2 89 67 88
|
||||||
|
004 L4 89 86 85
|
||||||
|
003 L3 56 66 78
|
||||||
|
005 L5 67 88 76
|
||||||
|
007 L6 78 76 70
|
||||||
|
006 L6 45 54 67
|
|
@ -0,0 +1,8 @@
|
||||||
|
7
|
||||||
|
005 L5 67 88 76
|
||||||
|
004 L4 89 86 85
|
||||||
|
001 L1 78 77 90
|
||||||
|
007 L6 78 76 70
|
||||||
|
002 L2 89 67 88
|
||||||
|
003 L3 56 66 78
|
||||||
|
006 L6 45 54 67
|
|
@ -0,0 +1,269 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *uid;
|
||||||
|
char *name;
|
||||||
|
int math, eng, cs;
|
||||||
|
} Score;
|
||||||
|
|
||||||
|
void save_to_file(Score **scores, int n, char *file_name) {
|
||||||
|
FILE *fp = fopen(file_name, "w");
|
||||||
|
fprintf(fp, "%d\n", n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
fprintf(fp, "%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
Score *scores[105];
|
||||||
|
int n;
|
||||||
|
// 如果 score.dat 不存在,就从键盘输入
|
||||||
|
FILE *fp = fopen("score.dat", "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
printf("score.dat not found, please input:\n");
|
||||||
|
scanf("%d", &n);
|
||||||
|
fp = fopen("score.dat", "w");
|
||||||
|
fprintf(fp, "%d\n", n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
scores[i] = (Score *)malloc(sizeof(Score));
|
||||||
|
char *uid = (char *)malloc(sizeof(char) * 105);
|
||||||
|
char *name = (char *)malloc(sizeof(char) * 105);
|
||||||
|
scanf("%s %s %d %d %d", uid, name, &scores[i]->math,
|
||||||
|
&scores[i]->eng, &scores[i]->cs);
|
||||||
|
scores[i]->uid = uid;
|
||||||
|
scores[i]->name = name;
|
||||||
|
fprintf(fp, "%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
} else {
|
||||||
|
// 从 score.dat 读取
|
||||||
|
fscanf(fp, "%d", &n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
scores[i] = (Score *)malloc(sizeof(Score));
|
||||||
|
char *uid = (char *)malloc(sizeof(char) * 105);
|
||||||
|
char *name = (char *)malloc(sizeof(char) * 105);
|
||||||
|
fscanf(fp, "%s %s %d %d %d", uid, name, &scores[i]->math,
|
||||||
|
&scores[i]->eng, &scores[i]->cs);
|
||||||
|
scores[i]->uid = uid;
|
||||||
|
scores[i]->name = name;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Total: %d\n", n);
|
||||||
|
|
||||||
|
printf("当前数据:\n");
|
||||||
|
// 输出所有人的信息
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
printf("--------------------\n");
|
||||||
|
|
||||||
|
// 按照每个科目排序并输出对应的排名
|
||||||
|
printf("按照数学成绩排序:\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int max = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (scores[j]->math > scores[max]->math) {
|
||||||
|
max = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Score *tmp = scores[i];
|
||||||
|
scores[i] = scores[max];
|
||||||
|
scores[max] = tmp;
|
||||||
|
printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
save_to_file(scores, n, "math.dat");
|
||||||
|
printf("--------------------\n");
|
||||||
|
|
||||||
|
printf("按照英语成绩排序:\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int max = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (scores[j]->eng > scores[max]->eng) {
|
||||||
|
max = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Score *tmp = scores[i];
|
||||||
|
scores[i] = scores[max];
|
||||||
|
scores[max] = tmp;
|
||||||
|
printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
save_to_file(scores, n, "eng.dat");
|
||||||
|
printf("--------------------\n");
|
||||||
|
|
||||||
|
printf("按照计算机成绩排序:\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int max = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (scores[j]->cs > scores[max]->cs) {
|
||||||
|
max = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Score *tmp = scores[i];
|
||||||
|
scores[i] = scores[max];
|
||||||
|
scores[max] = tmp;
|
||||||
|
printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
}
|
||||||
|
save_to_file(scores, n, "cs.dat");
|
||||||
|
printf("--------------------\n");
|
||||||
|
|
||||||
|
// 计算平均分,按平均成绩排序,写到 average.dat
|
||||||
|
printf("平均分:\n");
|
||||||
|
FILE *fp_average = fopen("average.dat", "w");
|
||||||
|
fprintf(fp_average, "%d\n", n);
|
||||||
|
double avg_scores[105];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
avg_scores[i] =
|
||||||
|
(scores[i]->math + scores[i]->eng + scores[i]->cs) / 3.0;
|
||||||
|
}
|
||||||
|
// sort
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int max = i;
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (avg_scores[j] > avg_scores[max]) {
|
||||||
|
max = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int tmp = avg_scores[i];
|
||||||
|
avg_scores[i] = avg_scores[max];
|
||||||
|
avg_scores[max] = tmp;
|
||||||
|
Score *tmp_score = scores[i];
|
||||||
|
scores[i] = scores[max];
|
||||||
|
scores[max] = tmp_score;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("%s %s %d %d %d %.2f\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs,
|
||||||
|
avg_scores[i] * 1.0);
|
||||||
|
fprintf(fp_average, "%s %s %d %d %d %.2f\n", scores[i]->uid,
|
||||||
|
scores[i]->name, scores[i]->math, scores[i]->eng, scores[i]->cs,
|
||||||
|
avg_scores[i] * 1.0);
|
||||||
|
}
|
||||||
|
fclose(fp_average);
|
||||||
|
printf("--------------------\n");
|
||||||
|
|
||||||
|
// 求出各门课程的平均成绩、最高分、最低分、不及格人数、60-69分人数、70-79分人数、
|
||||||
|
// 80-89分人数、90分以上人数。
|
||||||
|
int math_sum = 0, eng_sum = 0, cs_sum = 0;
|
||||||
|
int math_max = 0, eng_max = 0, cs_max = 0;
|
||||||
|
int math_min = 100, eng_min = 100, cs_min = 100;
|
||||||
|
int math_fail = 0, eng_fail = 0, cs_fail = 0;
|
||||||
|
int math_60_69 = 0, eng_60_69 = 0, cs_60_69 = 0;
|
||||||
|
int math_70_79 = 0, eng_70_79 = 0, cs_70_79 = 0;
|
||||||
|
int math_80_89 = 0, eng_80_89 = 0, cs_80_89 = 0;
|
||||||
|
int math_90 = 0, eng_90 = 0, cs_90 = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
math_sum += scores[i]->math;
|
||||||
|
eng_sum += scores[i]->eng;
|
||||||
|
cs_sum += scores[i]->cs;
|
||||||
|
if (scores[i]->math > math_max) {
|
||||||
|
math_max = scores[i]->math;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng > eng_max) {
|
||||||
|
eng_max = scores[i]->eng;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs > cs_max) {
|
||||||
|
cs_max = scores[i]->cs;
|
||||||
|
}
|
||||||
|
if (scores[i]->math < math_min) {
|
||||||
|
math_min = scores[i]->math;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng < eng_min) {
|
||||||
|
eng_min = scores[i]->eng;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs < cs_min) {
|
||||||
|
cs_min = scores[i]->cs;
|
||||||
|
}
|
||||||
|
if (scores[i]->math < 60) {
|
||||||
|
math_fail++;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng < 60) {
|
||||||
|
eng_fail++;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs < 60) {
|
||||||
|
cs_fail++;
|
||||||
|
}
|
||||||
|
if (scores[i]->math >= 60 && scores[i]->math <= 69) {
|
||||||
|
math_60_69++;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng >= 60 && scores[i]->eng <= 69) {
|
||||||
|
eng_60_69++;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs >= 60 && scores[i]->cs <= 69) {
|
||||||
|
cs_60_69++;
|
||||||
|
}
|
||||||
|
if (scores[i]->math >= 70 && scores[i]->math <= 79) {
|
||||||
|
math_70_79++;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng >= 70 && scores[i]->eng <= 79) {
|
||||||
|
eng_70_79++;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs >= 70 && scores[i]->cs <= 79) {
|
||||||
|
cs_70_79++;
|
||||||
|
}
|
||||||
|
if (scores[i]->math >= 80 && scores[i]->math <= 89) {
|
||||||
|
math_80_89++;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng >= 80 && scores[i]->eng <= 89) {
|
||||||
|
eng_80_89++;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs >= 80 && scores[i]->cs <= 89) {
|
||||||
|
cs_80_89++;
|
||||||
|
}
|
||||||
|
if (scores[i]->math >= 90) {
|
||||||
|
math_90++;
|
||||||
|
}
|
||||||
|
if (scores[i]->eng >= 90) {
|
||||||
|
eng_90++;
|
||||||
|
}
|
||||||
|
if (scores[i]->cs >= 90) {
|
||||||
|
cs_90++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("数学:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d "
|
||||||
|
"60-69分人数:%d "
|
||||||
|
"70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n",
|
||||||
|
math_sum * 1.0 / n, math_max, math_min, math_fail, math_60_69,
|
||||||
|
math_70_79, math_80_89, math_90);
|
||||||
|
printf("英语:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d "
|
||||||
|
"60-69分人数:%d "
|
||||||
|
"70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n",
|
||||||
|
eng_sum * 1.0 / n, eng_max, eng_min, eng_fail, eng_60_69, eng_70_79,
|
||||||
|
eng_80_89, eng_90);
|
||||||
|
printf("计算机:\n平均分:%.2f 最高分:%d 最低分:%d 不及格人数:%d "
|
||||||
|
"60-69分人数:%d "
|
||||||
|
"70-79分人数:%d 80-89分人数:%d 90分以上人数:%d\n",
|
||||||
|
cs_sum * 1.0 / n, cs_max, cs_min, cs_fail, cs_60_69, cs_70_79,
|
||||||
|
cs_80_89, cs_90);
|
||||||
|
|
||||||
|
// 根据姓名或学号查询某人的各门成绩,重名情况也能处理。
|
||||||
|
while (1) {
|
||||||
|
printf("请输入学号或姓名:\n");
|
||||||
|
char *query = (char *)malloc(sizeof(char) * 105);
|
||||||
|
scanf("%s", query);
|
||||||
|
int found = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (strcmp(scores[i]->uid, query) == 0 ||
|
||||||
|
strcmp(scores[i]->name, query) == 0) {
|
||||||
|
printf("%s %s %d %d %d\n", scores[i]->uid, scores[i]->name,
|
||||||
|
scores[i]->math, scores[i]->eng, scores[i]->cs);
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
printf("未找到\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
7
|
||||||
|
002 L2 89 67 88
|
||||||
|
004 L4 89 86 85
|
||||||
|
001 L1 78 77 90
|
||||||
|
007 L6 78 76 70
|
||||||
|
005 L5 67 88 76
|
||||||
|
003 L3 56 66 78
|
||||||
|
006 L6 45 54 67
|
|
@ -0,0 +1,8 @@
|
||||||
|
7
|
||||||
|
001 L1 78 77 90
|
||||||
|
002 L2 89 67 88
|
||||||
|
003 L3 56 66 78
|
||||||
|
004 L4 89 86 85
|
||||||
|
005 L5 67 88 76
|
||||||
|
006 L6 45 54 67
|
||||||
|
007 L6 78 76 70
|
Loading…
Reference in New Issue