输入一组字符串,输出出现次数最多的字符串及其个数
输入一个整数n(1 <= n <= 100)然后输入n个字符串(长度 <= 1000 ,字符为小写)
例如:
输入:
3
aaa
bb
bb
输出:
bb 2
2021-01-21 14:01
2021-01-21 15:12
2021-01-21 15:48
程序代码:
#include"stdio.h"
#include"string.h"
int main()
{
int n;
while(~scanf("%d", &n))
{
int s[10001] = {0}, num, max = 0, count = 0;
for(int i = 0; i < n; ++ i)
{
scanf("%d", &num);
s[num] ++;
//printf("%d %d\n", num, s[num]);
if(max <= s[num])
{
if(count < num && s[num] == max)
{
count = num;
continue;
}
if(s[num] > max)
{
max = s[num];
count = num;
}
}
}
printf("%d %d\n", count, max);
}
return 0;
}
2021-01-21 20:27
2021-01-22 09:02
2021-01-22 09:16
程序代码:
#include <stdio.h>
#include <string.h>
int main( void )
{
unsigned n;
scanf( "%u", &n );
char s[100][1001]; // 题目限定 1<=n<=100, 字符串长度<=1000
size_t s_len = 0;
unsigned c[100] = { 0 };
size_t result = 0;
for( size_t i=0; i!=n; ++i )
{
char x[1001];
scanf( "%s", x );
size_t index;
for( index=0; index!=s_len && strcmp(s[index],x)!=0; ++index );
if( index == s_len )
strcpy( s[s_len++], x );
++c[index];
if( c[index] > c[result] )
result = index;
}
printf( "%s %u\n", s[result], c[result] );
}
2021-01-22 09:24
2021-01-22 10:07