一道编程题,我实在是不会了。求求各位大侠帮帮忙吧
Forseverallinesofstringwhicharemadebyalphabets'a'~'z',judgethemostfrequentcharacterin...
For several lines of string which are made by alphabets 'a'~'z', judge the most frequent character in it and sort the characters by their frequence.
Input
Several lines of string which is made by 'a'~'z' and one line has not more than 1000 characters.A new line with a single string "###" means the end of Input.
Output
The most frequent character and its frequence. If there are more than one character which frequence are the most frequent, then output the smallest one in dictionary order. If you can, please sort all the characters in the string by frequence and output them in different lines followed their frequence. If there are more than one character which have the same frequence, sort them by dictionary order, too.
Sample Input
aldjfasdfjasjfdlasdfjalsdjfals
dfjlasjdlfjalfruqewourqweyrqpwyerpucnxzvcmvnzbzcv
###
Sample Output
Max:
a 8
Sort:
a 8
f 8
j 8
d 7
l 7
s 6
r 4
c 3
e 3
q 3
u 3
v 3
w 3
z 3
n 2
p 2
y 2
b 1
m 1
o 1
x 1 展开
Input
Several lines of string which is made by 'a'~'z' and one line has not more than 1000 characters.A new line with a single string "###" means the end of Input.
Output
The most frequent character and its frequence. If there are more than one character which frequence are the most frequent, then output the smallest one in dictionary order. If you can, please sort all the characters in the string by frequence and output them in different lines followed their frequence. If there are more than one character which have the same frequence, sort them by dictionary order, too.
Sample Input
aldjfasdfjasjfdlasdfjalsdjfals
dfjlasjdlfjalfruqewourqweyrqpwyerpucnxzvcmvnzbzcv
###
Sample Output
Max:
a 8
Sort:
a 8
f 8
j 8
d 7
l 7
s 6
r 4
c 3
e 3
q 3
u 3
v 3
w 3
z 3
n 2
p 2
y 2
b 1
m 1
o 1
x 1 展开
1个回答
展开全部
#include "stdio.h"
#include "string.h"
#define ERR -1
#define OK 0
char abc[26];
char input[1001];
int res[26] = {0};
int main()
{
int i,j,tmp1,tmp2;
char ch;
scanf ( "%s", input );
while ( strcmp ( input, "###" ) )
{
for ( i = 0; input[i] != 0; i++ )
{
ch = input[i] - 'a';
if ( ch > 25 || ch < 0 )
{
printf ( "Input Error!\n" );
return ERR;
}
res[ch]++;
}
scanf ( "%s", input );
}
for ( i = 0; i < 26; i++ )
{
abc[i] = i + 'a';
}
for ( i = 0; i < 25; i++ )
{
tmp1 = 25 - i;
for ( j = 0; j < tmp1; j++ )
{
if ( res[j] < res[j+1] )
{
tmp2 = res[j];
res[j] = res[j+1];
res[j+1] = tmp2;
ch = abc[j];
abc[j] = abc[j+1];
abc[j+1] = ch;
}
}
}
printf ( "Max:\n%c %d\nSort:\n", abc[0], res[0] );
for ( i = 0; i < 26 && res[i] != 0; i++ )
{
printf ( "%c %d\n", abc[i], res[i] );
}
return OK;
}
#include "string.h"
#define ERR -1
#define OK 0
char abc[26];
char input[1001];
int res[26] = {0};
int main()
{
int i,j,tmp1,tmp2;
char ch;
scanf ( "%s", input );
while ( strcmp ( input, "###" ) )
{
for ( i = 0; input[i] != 0; i++ )
{
ch = input[i] - 'a';
if ( ch > 25 || ch < 0 )
{
printf ( "Input Error!\n" );
return ERR;
}
res[ch]++;
}
scanf ( "%s", input );
}
for ( i = 0; i < 26; i++ )
{
abc[i] = i + 'a';
}
for ( i = 0; i < 25; i++ )
{
tmp1 = 25 - i;
for ( j = 0; j < tmp1; j++ )
{
if ( res[j] < res[j+1] )
{
tmp2 = res[j];
res[j] = res[j+1];
res[j+1] = tmp2;
ch = abc[j];
abc[j] = abc[j+1];
abc[j+1] = ch;
}
}
}
printf ( "Max:\n%c %d\nSort:\n", abc[0], res[0] );
for ( i = 0; i < 26 && res[i] != 0; i++ )
{
printf ( "%c %d\n", abc[i], res[i] );
}
return OK;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询