排序 + 分离单词技巧
#include <stdio.h> #include <string.h> #include <stdlib.h> bool isLetter(char &ch) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <='Z') { ch += 'a' - 'A'; return true; } return false; } int cmp(const void* _a, const void* _b) { char *a = (char*)_a; char *b = (char*)_b; return strcmp(a, b); } char b[50005][205]; int main() { char tmp; bool flag = false; int i = 0, j = 0; while ((tmp = getchar()) != EOF) { if (isLetter(tmp) && !flag) { i = 0; b[j][i++] = tmp; flag = true; } else if (isLetter(tmp) && flag) { b[j][i++] = tmp; } else if (!isLetter(tmp) && flag) { flag = false; b[j][i] = '\0'; j++; } } qsort(b, j, sizeof (b[0]), cmp); for (i=0; i<j-1; i++) { if (0 == strcmp(b[i], b[i+1])) continue; printf("%s\n", b[i]); } printf("%s\n", b[i]); return 0; }
作者:zcube 发表于2012-12-29 22:04:51 原文链接
阅读:5 评论:0 查看评论