前言
20分的题目题目不难,理解也不难,做起来有点问题
L1-034 点赞
微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持。每篇博文都有一些刻画其特性的标签,而你点赞的博文的类型,也间接刻画了你的特性。本题就要求你写个程序,通过统计一个人点赞的纪录,分析这个人的特性。
输入格式:
输出格式:
统计所有被点赞的博文中最常出现的那个特性标签,在一行中输出它的编号和出现次数,数字间隔1个空格。如果有并列,则输出编号最大的那个。
输入样例:
4
3 889 233 2
5 100 3 233 2 73
4 3 73 889 2
2 233 123
输出样例:
233 3
OK,以上就是全部的题目,说白了就是统计每个数出现的次数
代码写了三个个版本,都有分,前两个版本总是过不去,有些bug没有修复出来,第二个全部做出来了,大家看一下代码吧,每个代码都差不多,就第一版写注释了,后面的不同 的会写
代码1(未通过
#include <stdio.h>
int main()
{
int n1,n2,max,number = 0;
scanf("%d",&n1);//输入一共几行
int count[1005] ={0};//计数,每个数出现的次数
int time;//输入的数字,随便写的变量名,在这不太合适,先看逻辑
int i,j;
max = count[time];//求最大值
number = time;
for(i = 0;i < n1;i++)//输入部分
{
scanf("%d",&n2);
for(j = 0; j < n2;j++)
{
time = 0;
scanf("%d",&time);
count[time]++;//每出现一个数+1
if(max <= count[time]||number<=time)//判断出现更大值情况
{
max = count[time];
number = time;
}
}
}
printf("%d %d\n",number,max);//输出结果
}
做完后结果是这样的,没有搞清楚哪里出问题了
代码2(未通过
#include <stdio.h>
int main()
{
int n1,n2,max,number = 0;
scanf("%d",&n1);
int count[1005] ={0};
int time;
int i,j;
max = count[time];
number = time;
for(i = 0;i < n1;i++)
{
scanf("%d",&n2);
for(j = 0; j < n2;j++)
{
time = 0;
scanf("%d",&time);
count[time]++;
if(max <= count[time])
{
max = count[time];
if(number<=time)
number = time;
}
}
}
printf("%d %d\n",number,max);
}
得到的结果是这样的
代码3(通过
#include <stdio.h>
int main()
{
int n1,n2,max,number = 0;
scanf("%d",&n1);
int count[1005] ={0};
int time;
int i,j;
max = count[time];
number = time;
for(i = 0;i < n1;i++)
{
scanf("%d",&n2);
for(j = 0; j < n2;j++)
{
time = 0;
scanf("%d",&time);
count[time]++;
}
}
for(i = 0;i < 1001;i++)//遍历数组输出最大的
{
if(max <= count[i])
{
max = count[i];
number = i;
}
}
printf("%d %d\n",number,max);
}
以上所有代码均为自己编写,本人水平有限,如果有哪里出错或者有更好的解法可以与我私信或在评论区里进行讨论