Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
My answer (easy to understand)/** * @author: lhdtigerblue */ #include <string.h> #include <stdlib.h> #include <stdio.h> #define PHONE_STR_LEN 7 #define STD_PHONE_STR_LEN (PHONE_STR_LEN + 1) #define LINE_MAX_LEN 17 static char maps[] = " " "2223334445556667777888999"; static int comp_int(const void *a, const void *b) { return *(int *)a - *(int *)b; } static int a_to_i(char *arg) { int num = 0; int c; while ((c = *arg++) != '\0') { num = 10 * num + c - '0'; } return num; } static int get_filtrate_line(char *line) { int len = 0; int c; while ((c = getchar()) != EOF) { if (c == '-') continue; if (c == '\n') break; if (isdigit(c)) { line[len++] = c; } else { line[len++] = maps[c]; } } line[len] = '\0'; return len; } int main(void) { int lines; char line[LINE_MAX_LEN]; int line_len; int index = 0; int *phone_ints; int nr; int div; int no_dup = 1; scanf("%d\n", &lines); phone_ints = malloc(sizeof(int)* lines); while ((line_len = get_filtrate_line(line)) > 0) { phone_ints[index++] = a_to_i(line); } if (index == lines) { qsort(phone_ints, lines, sizeof(int), comp_int); int i = 0, j = 0; while (i < lines - 1) { j = i + 1; while (phone_ints[i] == phone_ints[j] && j < lines) j++; if (j != i + 1) { no_dup = 0; nr = phone_ints[i]; printf("%03d-%04d %d\n", nr/10000, nr%10000, j - i); } i = j; } if (no_dup) printf("No duplicates.\n"); } free(phone_ints); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator