解题思路:
结构体,冒泡排序法
参考代码:
冒泡排序算法
//冒泡排序 void sort(int& array) { for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) {//从小到大排序 int temp; temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } }
参考代码:
#include<iostream> //#include<bits/stdc++.h> #include<cmath> #include<cstring> #include<algorithm> #include<cstdio> #include<malloc.h> using namespace std; bool cmp(int a, int b) { return a > b; } struct student { int id; int score; }; student stu1[100]; student stu2[100]; student stu3[200]; int n, m; void input1() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> stu1[i].id >> stu1[i].score; } for (int i = 0; i < m; i++) { cin >> stu2[i].id >> stu2[i].score; } } void listUnit() { for (int i = 0; i < n; i++) { stu3[i].id = stu1[i].id; stu3[i].score = stu1[i].score; } for (int i = 0; i < m; i++) { stu3[n + i].id = stu2[i].id; stu3[n + i].score = stu2[i].score; } } int main() { input1(); listUnit(); student min; for (int i = 0; i < n + m; i++) {//此处冒泡排序 for (int j = 0; j < n + m - 1 - i; j++) { if (stu3[j].id > stu3[j + 1].id) { min.id = stu3[j].id; min.score = stu3[j].score; stu3[j].id = stu3[j + 1].id; stu3[j].score = stu3[j + 1].score; stu3[j+1].id = min.id; stu3[j+1].score = min.score; } } } for (int i = 0; i < n + m; i++) { cout << stu3[i].id << " " << stu3[i].score << endl; } return 0; }
0.0分
2 人评分
#include <stdio.h> #include <stdlib.h> typedef struct { int num; int g; } elem; typedef struct lnode { elem data; struct lnode *next; } Lnode, *Linklist; int main() { void creat(Linklist head, int N); void conect(Linklist p1, Linklist p2); void sort(Linklist a, int N, int M); void print(Linklist a); Linklist a, b; a = (Lnode *)malloc(sizeof(Lnode)); a->next = NULL; b = (Lnode *)malloc(sizeof(Lnode)); b->next = NULL; int N, M; scanf("%d%d", &N, &M); creat(a, N); creat(b, M); conect(a, b); sort(a, N, M); print(a);
数组输出 (C语言代码)浏览:811 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:674 |
大神老白 (C语言代码)浏览:694 |
简单的a+b (C语言代码)浏览:783 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:644 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:691 |
校门外的树 (C语言代码)浏览:988 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:588 |
求组合数 (C语言代码)浏览:1207 |
sizeof的大作用 (C语言代码)浏览:1593 |
你tm劈我代码是吧 2021-10-29 22:04:05 |
return 0; } void creat(Linklist head, int N) { Lnode *p, *q; p = head; for (int i = 0; i < N; i++) { q = (Linklist)malloc(sizeof(Lnode)); q->next = NULL; scanf("%d%d", &q->data.num, &q->data.g); p->next = q; p = p->next; } } void conect(Linklist p1, Linklist p2) { while (p1->next) { p1 = p1->next; } p1->next = p2->next; } void sort(Linklist a, int N, int M) { Lnode *x; x = a->next; for (int i = N + M - 1; i >= 1; i--) { x = a->next; for (int j = 1; j <= i; j++) { if (x->data.num > x->next->data.num) { Lnode *t; t = (Linklist)malloc(sizeof(Lnode)); t->data = x->data; x->data = x->next->data; x->next->data = t->data; free(t); } x = x->next; } } } void print(Linklist a) { Linklist p; p = a->next; while (p) { printf("%d %d ", p->data.num, p->data.g); p = p->next; } }