-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNum_sort.c
More file actions
74 lines (61 loc) · 1.37 KB
/
Copy pathNum_sort.c
File metadata and controls
74 lines (61 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
int main()
{
printf("请输入数字(0~100),-1作为输入结束,请输入小于100个数: ");
typedef struct frequency
{
int number;
int count;
} frequency;
frequency data[101];
for (int i = 0; i <= 100; i++)
{
data[i].number = i;
data[i].count = 0;
}
while(1)
{
int temp;
scanf("%d", &temp);
if (temp == -1)
{
break;
}
if (temp >= 0 && temp <= 100)
{
data[temp].count++;
}
}
printf("数字 频率\n");
for (int i = 0; i <= 100; i++)
{
if (data[i].count == 0)
{
continue;
}
printf("%-8d%-8d\n", data[i].number, data[i].count);
}
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100 - i; j++)
{
if (data[j].count < data[j + 1].count)
{
frequency temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
printf("\n按频率排序后:\n");
printf("数字 频率\n");
for (int i = 0; i <= 100; i++)
{
if (data[i].count == 0)
{
continue;
}
printf("%-8d%-8d\n", data[i].number, data[i].count);
}
return 0;
}