題意:統計字元出現頻率,將字元由頻率小到大輸出其ASCII值與其頻率。
解法:直接做。
TAG:水題
注意:
程式碼:
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
/** | |
* Tittle: 10062 - Tell me the frequencies! | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/03/14 | |
* File: 10062 - Tell me the frequencies.cpp - solve it | |
*/ | |
// include files | |
#include <iostream> | |
#include <cstdio> | |
#include <cstring> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
// definitions | |
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i ) | |
#define clr(x,v) memset( x, v, sizeof(x) ) | |
#define N 256 | |
typedef vector<int> VI; | |
// declarations | |
int cnt[N]; | |
bool vis[N]; | |
VI arr; | |
int n; | |
char str[1005]; | |
int sl; | |
// functions | |
const bool cmp( int i, int j ) | |
{ | |
if( cnt[i] == cnt[j] ) return i > j; | |
return cnt[i] < cnt[j]; | |
} | |
// main function | |
int main( void ) | |
{ | |
bool nl = false; | |
// input | |
while( gets(str) != NULL ) { | |
// init | |
clr( cnt, 0 ); | |
clr( vis, false ); | |
sl = strlen( str ); | |
arr.clear(); | |
// solve | |
FOR( i, 0, sl-1 ) { | |
if( !vis[str[i]] ) { | |
vis[str[i]] = true; | |
arr.push_back( str[i] ); | |
} | |
++cnt[str[i]]; | |
} | |
sort( arr.begin(), arr.end(), cmp ); | |
// output | |
if( nl ) putchar('\n'); | |
else nl = true; | |
FOR( i, 0, arr.size()-1 ) | |
printf( "%d %d\n", arr[i], cnt[arr[i]] ); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽