#include<iostream>
struct statistic_data {
int val;
int times;
};
void statistic(int a[], int size) {
struct statistic_data *data = (struct statistic_data *)calloc(1, sizeof(struct statistic_data) * size);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (a[i] == data[j].val) {
data[j].times++;
break;
}
if (data[j].times == 0) {
data[j].times++;
data[j].val = a[i];
break;
}
}
}
int max_times = 0;
int max_idx = 0;
for (int i = 0; i < size; ++i) {
if (max_times < data[i].times) {
max_times = data[i].times;
max_idx = i;
}
}
printf("num = %d, times = %d\n", data[max_idx].val, data[max_idx].times);
free(data);
}
int main(){
printf("please input 12 numbers\n");
int a[12] = { 0 };
scanf("%d %d %d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9], &a[10], &a[11]);
statistic(a, 12);
return 0;
}
请采纳,谢谢
2020-04-21
#include <stdio.h>
int main(void) {
int a[12], b[12];
int max = -1, most;
for (int i = 0; i < 12; i++) {
scanf("%d", &a[i]);
b[i] = 1;
}
for (int i = 0; i < 12; i++) {
if (b[i] == 0) continue;
int cnt = 1;
for (int j = i + 1; j < 12; j++) {
if (b[j] != 0 && a[j] == a[i]) {
cnt++;
b[j] = 0;
}
}
if (cnt > max) {
max = cnt;
most = a[i];
}
}
printf("num: %d, times=%d", most, max);
return 0;
}
#include<stdio.h>
int main()
{
int a[12],b[1000]={0},max=0,min=0;
for(int i=0;i<12;i++)
{
scanf("%d",&a[i]);
if(i==0)
{
max=a[i];
min=a[i];
}
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
b[a[i]]++;
}
int k=b[min],x=min;
for(int i=min;i<=max;i++)
{
if(b[i]>k)
{
x=i;
k=b[i];
}
}
printf("num: %d, times=%d",x,k);
return 0;
}