《算法笔记》4.1小节——算法初步->排序
冒泡排序
int bubblesort() {
int num;
scanf("%d", &num);
int a[100];
for (int i = 0; i < num; i++)
scanf("%d", &a[i]);
for (int i = 1; i < num; i++) {
for (int j = num - 1; j >= i;j--) {
if (a[j] < a[j - 1]) swap(a[j], a[j - 1]);
}
}
for (int i = 0; i < num; i++) {
printf("%d\n", a[i]);
}
}
选择排序
void selectsort() {
int num;
scanf("%d", &num);
int a[100];
for (int i = 0; i < num; i++)
scanf("%d", &a[i]);
for (int i = 0; i < num; i++) {
int min = i;
for (int j = i; j < num; j++) {
if (a[min] > a[j]) min = j;
}
swap(a[i], a[min]);
}
for (int i = 0; i < num; i++) {
printf("%d\n", a[i]);
}
}
插入排序
int insertsort() {
int num;
scanf("%d", &num);
int a[100];
for (int i = 0; i < num; i++)
scanf("%d", &a[i]);
//出现j-1,则要考虑从0开始会导致越界
for (int i = 1; i < num; i++) {
int temp = a[i];
int j=i;
for (; j >= 1 && temp < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = temp;
}
for (int i = 0; i < num; i++) {
printf("%d\n", a[i]);
}
}
1923 Problem A 排序
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
int a[110];
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
printf("%d", a[i]);
if (i != n - 1)printf(" ");
else printf("\n");
}
}
return 0;
}
1925 Problem B 特殊排序
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
int n;
int a[1010];
while (scanf("%d", &n)!=EOF) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
printf("%d\n", a[n - 1]);
if (n == 1) printf("-1\n");
else {
for (int i = 0; i < n - 1; i++) {
printf("%d", a[i]);
if (i < n - 2)printf(" ");
else printf("\n");
}
}
}
return 0;
}
1926 Problem C EXCEL排序
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct student {
int num;
char name[20];
int score;
};
bool cmp1(student a, student b) {
return a.num < b.num;
}
bool cmp2(student a, student b) {
if (strcmp(a.name, b.name) == 0) return a.num < b.num;
else return strcmp(a.name, b.name) < 0;
}
bool cmp3(student a, student b) {
if (a.score == b.score) return a.num < b.num;
else return a.score < b.score;
}
int main() {
int n, ca;
student stu[100010];
int flag = 0;
while (scanf("%d %d", &n, &ca)!=EOF) {
flag++;
if (n == 0) break;
for (int i = 0; i < n; i++) {
scanf("%d %s %d", &stu[i].num, &stu[i].name, &stu[i].score);
}
if(ca==1)sort(stu, stu + n, cmp1);
else if(ca==2) sort(stu, stu + n, cmp2);
else if(ca==3) sort(stu, stu + n, cmp3);
else break;
printf("Case %d:\n", flag);
for (int i = 0; i < n; i++) {
printf("%06d %s %d\n", stu[i].num,stu[i].name,stu[i].score);
}
}
return 0;
}
1927 Problem D 字符串内排序
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
char a[210];
//输入的字符串中可能有空格,不用scanf用gets
while (gets(a)) {
sort(a, a + strlen(a));
printf("%s\n", a);
}
return 0;
}
1978 Problem E Problem B
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int m = 0;
while(scanf("%d", &m) != EOF) {
int mm[15][15];
int sum[40] = { 0 };
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &mm[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
sum[i] += mm[i][j];
sum[i + m] += mm[j][i];
if (i == j)sum[2 * m] += mm[i][j];
if (i == m - 1 - j)sum[2 * m + 1] += mm[i][j];
}
}
sort(sum, sum + 2 * m + 2, cmp);
for (int i = 0; i < (2 * m + 2); i++) {
printf("%d", sum[i]);
(i < (2 * m + 1)) ? printf(" ") : printf("\n");
}
}
return 0;
}
2043 Problem F 小白鼠排队
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct mouse {
int weight;
char color[10];
};
bool cmp(mouse a,mouse b) {
return a.weight > b.weight;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
mouse mou[110];
for (int i = 0; i < n; i++)
scanf("%d %s", &mou[i].weight, &mou[i].color);
sort(mou, mou + n, cmp);
for (int i = 0; i < n; i++)
printf("%s\n", mou[i].color);
}
return 0;
}
2069 Problem G 中位数
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
int n;
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
int num[10010] = { 0 };
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
sort(num, num + n);
if (n % 2 ==0) printf("%d\n", (num[(n-1) / 2] + num[n/2]) / 2);
else printf("%d\n", num[(n-1)/ 2]);
}
return 0;
}
2080 Problem H 整数奇偶排序
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int num[10] = { 0 };
while (scanf("%d", &num[0]) != EOF) {
int even[10] = { 0 };
int odd[10] = { 0 };
int evennum = 0, oddnum = 0;
if (num[0] % 2 == 0) even[evennum++] = num[0];
else odd[oddnum++] = num[0];
for (int i = 1; i < 10; i++) {
scanf("%d", &num[i]);
if (num[i] % 2 == 0) even[evennum++] = num[i];
else odd[oddnum++] = num[i];
}
sort(even, even + evennum);
sort(odd, odd + oddnum,cmp);
for (int i = 0; i < oddnum; i++) {
printf("%d ", odd[i]);
}
for (int i=0; i < evennum; i++) {
printf("%d", even[i]);
if (i != evennum - 1)printf(" ");
}
printf("\n");
}
return 0;
}
2088 Problem I 排名
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct student {
char id[25];
int test_sum;
int test_num[15];
int score;
};
bool cmp(student a, student b) {
if(a.score!=b.score) return a.score > b.score;
else return a.id > b.id;
}
int main() {
int n, m, line;
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
scanf("%d %d", &m, &line);
student stu[1000];
int point[15];
int pass = 0;
for (int i = 0; i < m; i++) {
scanf("%d", &point[i]);
}
for (int i = 0; i < n; i++) {
stu[i].score = 0;
scanf("%s %d", &stu[i].id, &stu[i].test_sum);
for (int j = 0; j < stu[i].test_sum; j++) {
scanf("%d", &stu[i].test_num[j]);
stu[i].score += point[stu[i].test_num[j]-1];
}
if(stu[i].score>=line) pass++;
}
sort(stu, stu + n, cmp);
printf("%d\n", pass);
for (int i = 0; i < pass; i++) {
printf("%s %d\n", stu[i].id, stu[i].score);
}
}
return 0;
}