用C语言写一个在一个数据中找众数程序
时间: 2024-05-09 18:14:40 浏览: 27
以下是使用 C 语言编写的一个找众数的程序:
```c
#include <stdio.h>
int main() {
int n, i, j, maxCount = 0, mode = 0;
printf("请输入数据的数量:");
scanf("%d", &n);
int a[n];
printf("请输入数据:");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
int count = 0;
for (j = 0; j < n; j++) {
if (a[j] == a[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = a[i];
}
}
printf("众数是:%d\n", mode);
return 0;
}
```
该程序的思路是使用双重循环,对于每一个数据,计算出它在数据中出现的次数,找到出现次数最多的数据即为众数。
相关问题
用c语言写一个众数问题
可以使用数组和循环来解决众数问题。首先,定义一个数组来存储数据,然后使用循环来遍历数组,统计每个元素出现的次数。最后,找到出现次数最多的元素即可。
以下是一个简单的众数问题的 C 语言代码示例:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int maxCount = 0, mode = 0;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = arr[i];
}
}
printf("The mode of the array is %d\n", mode);
return 0;
}
```
这个程序的输出结果是:
```
The mode of the array is 1
```
这个程序使用了两个嵌套的循环来遍历数组,时间复杂度为 O(n^2)。如果数组很大,这个算法的效率会比较低。可以使用哈希表等数据结构来优化算法。
帮我写一个循环队列c语言代码,实现从20个数据中取出众数
好的,以下是一个简单的循环队列的 C 语言代码实现:
```c
#include <stdio.h>
#define MAX_SIZE 20
// 循环队列结构体
typedef struct queue {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
// 初始化循环队列
void initQueue(Queue *q) {
q->front = 0;
q->rear = 0;
}
// 判断循环队列是否为空
int isEmptyQueue(Queue *q) {
return q->front == q->rear;
}
// 判断循环队列是否已满
int isFullQueue(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
// 入队
int enQueue(Queue *q, int value) {
if (isFullQueue(q)) {
return 0;
} else {
q->data[q->rear] = value;
q->rear = (q->rear + 1) % MAX_SIZE;
return 1;
}
}
// 出队
int deQueue(Queue *q) {
if (isEmptyQueue(q)) {
return -1;
} else {
int value = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return value;
}
}
// 计算众数
int mode(Queue *q) {
int max = q->data[0];
int count = 1;
int i;
for (i = 1; i < MAX_SIZE; i++) {
if (q->data[i] == max) {
count++;
} else if (q->data[i] > max) {
max = q->data[i];
count = 1;
}
}
return max;
}
int main() {
Queue q;
initQueue(&q);
// 入队
enQueue(&q, 1);
enQueue(&q, 2);
enQueue(&q, 2);
enQueue(&q, 3);
enQueue(&q, 3);
enQueue(&q, 3);
enQueue(&q, 4);
enQueue(&q, 4);
enQueue(&q, 4);
enQueue(&q, 4);
enQueue(&q, 5);
enQueue(&q, 5);
enQueue(&q, 5);
enQueue(&q, 5);
enQueue(&q, 5);
enQueue(&q, 6);
enQueue(&q, 6);
enQueue(&q, 6);
enQueue(&q, 6);
enQueue(&q, 6);
// 计算众数
int m = mode(&q);
printf("众数为:%d\n", m);
return 0;
}
```
这个循环队列会先入队 20 个数据,然后计算这些数据的众数并输出。请注意使用时需要改变数据源和 MAX_SIZE 的值来适应实际需求。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)