某蒻鸡的Python题解

shao 发表于 1年前 · 关联问题 排名

面向对象 + 冒泡排序 (呜呜呜

class Stu: def __init__(self, name, sbjs): self.name = name self.sbjs = sbjs self.sum = sum(sbjs) stus = [] n, m = [int(i) for i in input().strip().split()] for i in range(n): s = input().split() stus.append(Stu(s[0], [int(j) for j in s[1:]])) def isnb(a, b): if a.sum > b.sum: return True elif a.sum < b.sum: return False else: for i in range(len(a.sbjs)): if a.sbjs[i] > b.sbjs[i]: return True elif a.sbjs[i] < b.sbjs[i]: return False return False ans = [] for i in range(n - 1): for j in range(n - 1): if not isnb(stus[j], stus[j+1]): stus[j], stus[j+1] = stus[j+1], stus[j] print(*[i.name for i in stus])

shao 发表于 1年前

纯C版本

#include <stdio.h> #define N 1100 #define M 110 int n, m; struct stu /*结构体爽*/ { char name[20]; int sbj[M], sum = 0; } a[N]; int isnb(stu a, stu b) /*比较函数*/ { if (a.sum != b.sum) return a.sum > b.sum; for (int i = 0; i < m; i++) { if (a.sbj[i] != b.sbj[i]) return a.sbj[i] > b.sbj[i]; } return 0; } int main(void) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", a[i].name); for (int j = 0; j < m; j++) { scanf("%d", &a[i].sbj[j]); a[i].sum += a[i].sbj[j]; } } for (int i = 0; i < n - 1; i++) /*冒泡排序爽*/ for (int j = 0; j < n - 1; j++) if (isnb(a[j + 1], a[j])) { stu t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } for (int i = 0; i < n; i++) printf("%s ", a[i].name); return 0; }

不知道是我的编译器的问题,还是楼主故意留了bug(手动滑稽),楼主给出的C的代码无论是在CB还是OJ上都编译错误。我把改正的C的代码贴一下。

#include <stdio.h> #define N 1100 #define M 110 int n, m; struct stu /*结构体爽*/ { char name[20]; int sbj[M]; int sum; } a[N]; int isnb(struct stu a, struct stu b) /*比较函数*/ //第一处更改 { if (a.sum != b.sum) return a.sum > b.sum; for (int i = 0; i < m; i++) { if (a.sbj[i] != b.sbj[i]) return a.sbj[i] > b.sbj[i]; } return 0; } int main(void) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", a[i].name); a[i].sum = 0; for (int j = 0; j < m; j++) { scanf("%d", &a[i].sbj[j]); a[i].sum += a[i].sbj[j]; } } for (int i = 0; i < n - 1; i++) /*冒泡排序爽*/ for (int j = 0; j < n - 1; j++) if (isnb(a[j + 1], a[j])) { a[N] = a[j]; a[j] = a[j + 1]; a[j + 1] = a[N]; //第二处更改 } for (int i = 0; i < n; i++) printf("%s ", a[i].name); return 0; }

shao 发表于 1年前

还是大佬6啊(蒻鸡不敢了哭哭

21软工1班聂云滔 发表于 10天前

大佬啊