找出出現最多的單字
排序先依照出現次數,後依照字母順序
大小寫視為一樣的,所以我轉大寫
----------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define ASCII 128
typedef struct
{
int letter_index;
int appear_times;
}type_letter;
type_letter letter_var[ASCII];
int cmp();
int main()
{
char **sentences = NULL, buffer[2048];
int lines, i, temp, j;
scanf("%d", &lines);
getchar(); /* For the remaining '\n' */
for(i = 0; i < ASCII; i++) /* Initiailize appear_times and index */
{
letter_var[i].letter_index = i;
letter_var[i].appear_times = 0;
}
sentences = (char **)malloc(lines * sizeof(char *) );
for(i = 0; i < lines; i++) /* Read in */
{
gets(buffer);
sentences[i] = (char *)malloc(strlen(buffer) + 1);
strcpy(sentences[i], buffer);
for(j = 0; sentences[i][j]; j++) /* To count the number of letters. */
{
temp = toupper(sentences[i][j]);
if(isalpha(temp) ) /* Only counts letters. */
letter_var[temp].appear_times++;
}
}
qsort( (void *)letter_var, ASCII, sizeof(letter_var[0]), cmp);
for(i = 0; letter_var[i].appear_times; i++)
printf("%c %d\n", letter_var[i].letter_index, letter_var[i].appear_times);
for(i = 0; i < lines; i++)
free( (void *)sentences[i]);
free( (void *)sentences);
return 0;
}
int cmp(const void *a, const void *b)
{
/*
1.First level -> appear_times
2.Second level -> alphabetical order
*/
int aa_times = ( (type_letter *)a) -> appear_times, aa_index = ( (type_letter *)a) -> letter_index;
int bb_times = ( (type_letter *)b) -> appear_times, bb_index = ( (type_letter *)b) -> letter_index;
if(aa_times == bb_times)
return aa_index - bb_index;
return bb_times - aa_times; /* From more to less. */
}
-2015-05-22-------------------------------------
package test;
import java.util.*;
public class main {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
int testCases = in.nextInt();
in.nextLine(); // Read the redundant '\n'
ArrayList<MyChar> alMyChars = new ArrayList<>();
final String limit = "`1234567890-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?";
while (testCases-- > 0) {
String getString = in.nextLine();
for (int i = 0; i < limit.length(); i++) {
// Replace limit word to white space.
getString = getString.replace(limit.charAt(i), ' ');
}
String[] strArr = getString.split(" ");
for (String string : strArr) {
string = string.toUpperCase();
for (int i = 0; i < string.length(); i++) {
int ch = (int) string.charAt(i), idx;
// 沒有放過才加進去
if ((idx = indexOf(alMyChars, (char) ch)) == -1) {
alMyChars.add(new MyChar((char) ch));
alMyChars.get(alMyChars.size() - 1).incRepeatTimes();
} else {
// 放進去過
alMyChars.get(idx).incRepeatTimes();
}
}
}
}
// Anonymous sorting
alMyChars.sort(new Comparator<MyChar>() {
@Override
public int compare(MyChar o1, MyChar o2) {
if (o2.getRepeatTimes() == o1.getRepeatTimes()) {
// Sort by alphabetical. From A to Z.
return o1.getCh() - o2.getCh();
}
// From more to less
return o2.getRepeatTimes() - o1.getRepeatTimes();
}
});
// Output
for (MyChar alMyChar : alMyChars) {
System.out.println("" + alMyChar.getCh() + " " + alMyChar.getRepeatTimes());
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static int indexOf(ArrayList<MyChar> al, char ch) {
for (int i = 0; i < al.size(); i++) {
if (ch == (al.get(i).getCh())) {
return i;
}
}
return -1;
}
}
class MyChar {
private char ch;
private int repeatTimes;
public MyChar(char ch) {
this.ch = ch;
this.repeatTimes = 0;
}
public char getCh() {
return ch;
}
public int getRepeatTimes() {
return repeatTimes;
}
public void incRepeatTimes() {
this.repeatTimes++;
}
}