用c语言编程从键盘输入n(0<n<100)个整数,计算出其中出次数最多且数值最大的元素及出现次数
答案:4 悬赏:0 手机版
解决时间 2021-04-04 10:22
- 提问者网友:杀生予夺
- 2021-04-04 05:15
请尽快
最佳答案
- 五星知识达人网友:千杯敬自由
- 2021-04-04 06:53
TC2.0下调试通过,楼主放心使用。
#include <stdio.h>
void main()
{
int n,i,max,x;
int t=1;
max=0;
printf("Input n:\n");
scanf("%d",&n);
printf("Input %d numbers(0<numbers<100):\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(max<x)
{
max=x;
t=1;
}
else if(max==x)
t++;
}
printf("Max is %d,for %d times\n",max,t);
}
#include <stdio.h>
void main()
{
int n,i,max,x;
int t=1;
max=0;
printf("Input n:\n");
scanf("%d",&n);
printf("Input %d numbers(0<numbers<100):\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(max<x)
{
max=x;
t=1;
}
else if(max==x)
t++;
}
printf("Max is %d,for %d times\n",max,t);
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-04-04 09:02
#include <stdio.h>
int main(void)
{
int n,i,max,j;
int t=1;
scanf("%d",&n);
scanf("%d",&max);
for(i=1;i<n;++i)
{
scanf("%d",&j);
if(max<j)
{
max=j;
t=1;
}
else if(max==j)
t++;
}
printf("Max is %d,for %d times\n",max,t);
return 0;
}
- 2楼网友:轮獄道
- 2021-04-04 08:55
#include <stdio.h>
int main(void)
{
int n,i,max,j;
int t=1;
max=0;
scanf("%d",&n);
for(i=1;i<n;++i)
{
scanf("%d",&j);
if(max<j)
{
max=j;
t=1;
}
else if(max==j)
t++;
}
printf("Max is %d,for %d times\n",max,t);
return 0;
}
最大的数值不是输入的
- 3楼网友:旧脸谱
- 2021-04-04 08:00
1:次数最多
2:在次数最多的基础上,数值最大
_______________________________________________以下为程序:
#include
// 设置最大值
#define max 100
typedef struct record
{
int number[max];
int counter[max];
}record;
int isexist(int array[], int index, record rd)
{
int i;
int flag = -1;
bool finish = true;
for (i = 0; i < index && finish; i++)
{
if (array[index] == rd.number[i])
{
flag = i;
finish = false;
}
}
return flag;
}
int findmaxnumber(record rd, int index)
{
int i = 0;
int flag = 0;
int maxnumber = rd.number[0];
int maxcounter = rd.counter[0];
while (i < index)
{
if (rd.counter[i] > maxcounter)
{
flag = i;
maxnumber = rd.number[i];
maxcounter = rd.counter[i];
}
else if (rd.counter[i] == maxcounter)
{
if (rd.number[i] > maxnumber)
{
flag = i;
maxnumber = rd.number[i];
maxcounter = rd.counter[i];
}
}
i++;
}
return flag;
}
int main()
{
int i;
int n;
int arr[max];
record rd;
int ct;
int find;
// 初始化结构体
for (i = 0; i < max; i++)
{
rd.counter[i] = 0;
rd.number[i] = 0;
}
// 输入整数的个数n
printf("enter n (0 scanf("%d", &n);
// 采用数组记录输入的整数
printf("enter %d number : ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
ct = 0;
i = 0;
while (i < n)
{
int tempflag;
tempflag = isexist(arr, i, rd);
if (tempflag == -1)
{
rd.number[ct] = arr[i];
rd.counter[ct]++;
ct++;
}
else
{
rd.counter[tempflag]++;
}
i++;
}
find = findmaxnumber(rd, ct);
printf("max number: %d, max counter: %d\n", rd.number[find], rd.counter[find]);
return 0;
}
——————————————————————————————
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯