文章目录
1.输入n名同学的成绩和学号,对成绩排序,输出对应学号
要求重复的学号重新输入
计算n名同学的平均值,对小于60分的同学删除分数
大于60分的同学输出:优秀:几人,良好:几人,中等:几人,合格:几人
#include <stdio.h>#define number 1000int main() {int n; // 学生人数printf("请输入学生人数 n: ");scanf("%d", &n);int ids[number]; // 存储学号int scores[number]; // 存储成绩int tempId, tempScore; // 用于交换数据// 输入学生成绩和学号for (int i = 0; i < n; ) {printf("请输入第 %d 名学生的学号和成绩: ", i + 1);scanf("%d %d", &tempId, &scores[i]);// 检查学号是否重复int isDuplicate = 0;for (int j = 0; j < i; j++) {if (ids[j] == tempId) {printf("学号重复,请重新输入第 %d 名学生的学号和成绩: ", i + 1);isDuplicate = 1;break;}}if (!isDuplicate) {ids[i] = tempId;i++;}}// 对成绩进行排序,并保持学号的对应关系for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - 1 - i; j++) {if (scores[j] < scores[j + 1]) {// 交换成绩tempScore = scores[j];scores[j] = scores[j + 1];scores[j + 1] = tempScore;// 交换学号tempId = ids[j];ids[j] = ids[j + 1];ids[j + 1] = tempId;}}}// 输出排序后的成绩和对应的学号printf("排序后的成绩和对应的学号:\n");for (int i = 0; i < n; i++) {printf("%d %d\n", scores[i], ids[i]);}// 计算平均分int sum = 0;for (int i = 0; i < n; i++) {sum += scores[i];}double average = (double)sum / n;printf("平均分是: %.2f\n", average);// 删除小于60分的成绩int count = 0;for (int i = 0; i < n; i++) {if (scores[i] >= 60) {scores[count] = scores[i];ids[count] = ids[i];count++;}}n = count; // 更新学生人数// 输出等级分布int oen = 0, two = 0, three = 0, pass = 0;for (int i = 0; i < n; i++) {if (scores[i] >= 90) oen++;else if (scores[i] >= 80) two++;else if (scores[i] >= 70) three++;else if (scores[i] >= 60) pass++;}printf("优秀:%d人,良好:%d人,中等:%d人,合格:%d人\n", oen, two, three, pass);return 0;
}
2,输入45矩阵
先输入35,然后将每一列最大值放在第4行
计算靶点数据(在该行上最小,该列上最大),若有输出该元素和第几行、第几列
对列排序(奇数列最大,偶数列最小)
#include <stdio.h>int main() {int matrix[3][5]; // 原始3x5矩阵int result[4][5]; // 结果4x5矩阵int maxInCol[5]; // 用于存储每列的最大值int targetRow, targetCol; // 用于记录靶点数据的行和列int minInRow, maxInColTemp; // 临时变量用于比较找到靶点数据// 用户输入3x5矩阵printf("请输入3x5矩阵的元素:\n");for (int i = 0; i < 3; i++) {for (int j = 0; j < 5; j++) {scanf("%d", &matrix[i][j]);}}for (int j = 0; j < 5; j++) {maxInCol[j] = matrix[0][j];for (int i = 0; i < 3; i++) {if (matrix[i][j] > maxInCol[j]) {maxInCol[j] = matrix[i][j];}result[i][j] = matrix[i][j];}}// 将每列最大值放入结果矩阵的第4行for (int j = 0; j < 5; j++) {result[3][j] = maxInCol[j];}// 寻找靶点数据for (int i = 0; i < 4; i++) {minInRow = result[i][0];targetCol = 0;for (int j = 1; j < 5; j++) {if (result[i][j] < minInRow) {minInRow = result[i][j];targetCol = j;}}maxInColTemp = result[0][targetCol];targetRow = 0;for (int k = 1; k < 4; k++) {if (result[k][targetCol] > maxInColTemp) {maxInColTemp = result[k][targetCol];targetRow = k;}}if (minInRow == maxInColTemp) {printf("靶点数据为:%d,位于第 %d 行,第 %d 列\n", minInRow, targetRow, targetCol);}}for (int j = 0; j < 5; j++) {if (j % 2 == 1) {// 奇数列,从大到小排序for (int i = 0; i < 4 - 1; i++) {for (int k = 0; k < 4 - i - 1; k++) {if (result[k][j] < result[k + 1][j]) {// 交换元int temp = result[k][j];result[k][j] = result[k + 1][j];result[k + 1][j] = temp;}}}} else {// 偶数列,从小到大排序for (int i = 0; i < 4 - 1; i++) {for (int k = 0; k < 4 - i - 1; k++) {if (result[k][j] > result[k + 1][j]) {// 交换元素int temp = result[k][j];result[k][j] = result[k + 1][j];result[k + 1][j] = temp;}}}}}// 输出排序后的4x5矩阵printf("排序后的4x5矩阵是:\n");for (int i = 0; i < 4; i++) {for (int j = 0; j < 5; j++) {printf("%d ", result[i][j]);}printf("\n");}return 0;
}