我是先一個一個字看它的ASCII碼是多少,
把陣列中它的ASCII碼那格加1,
表示這個字多出現了一次。
我用了三個變數,
一個存最大出現的ASCII碼,
一個存最小出現的ASCII碼,
一個存最多出現的次數。
然後我就從出現一次開始搜尋,直到最多出現的次數,
每次搜尋都是從最大ASCII碼搜尋到最小ASCII碼這樣,
搜尋到次數一樣就輸出。
(當然這題用Sort也是OK啦XD)
[C](0.008)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
int min( int x, int y ) | |
{ | |
return ( x > y )? y : x; | |
} | |
int max( int x, int y ) | |
{ | |
return ( x > y )? x : y; | |
} | |
int main() | |
{ | |
char s[1005]; | |
int blank = 0; | |
while( gets(s) ) | |
{ | |
if( blank ) | |
printf( "\n" ); | |
int ASCII[130] = {0}; | |
int length = strlen(s); | |
int i; | |
int min_ASCII = 200; | |
int max_ASCII = 0; | |
int max_count = 0; | |
for( i = 0 ; i < length ; i++ ) | |
{ | |
ASCII[ (int)s[i] ]++; | |
min_ASCII = min( min_ASCII, (int)s[i] ); | |
max_ASCII = max( max_ASCII, (int)s[i] ); | |
max_count = max( max_count, ASCII[ (int)s[i] ] ); | |
} | |
int j; | |
for( i = 1 ; i <= max_count ; i++ ) | |
for( j = max_ASCII ; j >= min_ASCII ; j-- ) | |
if( ASCII[j] == i ) | |
printf( "%d %d\n", j , ASCII[j] ); | |
blank = 1; | |
} | |
return 0; | |
} |
0 意見:
張貼留言