寻找一些数字中的众数的代码
时间: 2023-04-03 16:02:44 浏览: 88
以下是寻找一些数字中的众数的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, j, maxCount = 0, mode, count;
printf("请输入数字的个数:");
scanf("%d", &n);
int a[n];
printf("请输入数字:");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
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;
}
相关问题
寻找一些数字中的众数,如果存在多个众数,就找众数的平均数的代码
以下是寻找数字中众数的代码:
#include <stdio.h>
int main() {
int n, i, j, count, maxCount = 0, modeCount = 0;
float mode = 0;
printf("请输入数字的个数:");
scanf("%d", &n);
int nums[n];
for (i = 0; i < n; i++) {
printf("请输入第%d个数字:", i + 1);
scanf("%d", &nums[i]);
}
for (i = 0; i < n; i++) {
count = 0;
for (j = 0; j < n; j++) {
if (nums[j] == nums[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = nums[i];
modeCount = 1;
} else if (count == maxCount) {
mode += nums[i];
modeCount++;
}
}
if (modeCount == 1) {
printf("众数为%.0f\n", mode);
} else {
printf("众数的平均数为%.2f\n", mode / modeCount);
}
return 0;
}
注意:这个代码只能找到整数数组中的众数,如果要找到浮点数数组中的众数,需要进行一些修改。
如何查找数字中的众数的C语言代码
以下是查找数字中的众数的C语言代码:
#include <stdio.h>
int main() {
int n, i, j, maxCount = , mode, count;
printf("请输入数字的个数:");
scanf("%d", &n);
int arr[n];
printf("请输入数字:");
for (i = ; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = ; i < n; i++) {
count = ;
for (j = ; j < n; j++) {
if (arr[j] == arr[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mode = arr[i];
}
}
printf("众数是:%d\n", mode);
return ;
}
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)