#include <stdio.h>
#include <stdlib.h>
#define N 20
int a[N] = { 1,2,3,1,2,9,9,10,11,12,1,2,1,2,2,2,1,10 ,11,98};
struct password {
int num;//出现的数据值
int ci;//出现的次数
};
struct password pass[N] = { 0 }; //整体清零
void main() {
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < N; i++) {
printf("%4d", a[i]);
}
printf("\n");
int u = 0;//代表结构体数组第一个
for (int i = 0; i < N; i++) {
pass[u].num = a[i];
int ci = 1;
for (int j = i; j < N; j++) {
if (a[j] == a[j + 1]) {
ci++;
}
else {
i = j;//找到第二个数
break; //中断
}
}
pass[u].ci = ci;//记录次数
u++;
}
for (int i = 0; i < N; i++) {
printf("\n%4d%4d%4d", a[i],pass[i].num,pass[i].ci);
}
int s = 0;
for (int i = 0; i < N; i++) {
if (pass[i].ci == 0) {
s = i;//记录0的位置
break;
}
}
printf("\ns=%d", s);
for (int i = 0; i < s - 1; i++) {
for (int j = 0; j < s - 1 - i; j++) {
if (pass[j].ci < pass[j + 1].ci) {
int temp = pass[j].ci;
pass[j].ci = pass[j + 1].ci;
pass[j + 1].ci = temp;
temp = pass[j].num;
pass[j].num = pass[j + 1].num;
pass[j + 1].num = temp;
}
}
}
printf("\nlast\n");
for (int i = 0; i < N; i++) {
printf("\n%4d%4d%4d", a[i], pass[i].num, pass[i].ci);
}
system("pause");
}
出现次数最多和最少
最新推荐文章于 2024-10-30 19:45:13 发布