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 |
谁帮忙看下啊...为什么为TLE啊!!!!用一链表存放输入电话号码和出现次数. 建立链表的时候就是一有序链表. 算算时间不至于超时的....郁闷 #include "stdio.h" #include "stdlib.h" #include "string.h" struct telphone{ long num; //电话号码 long f; //出现次数 struct telphone *next; }; //字母映射(虽然QZ不出现,但是设置数值也没关系的) int map[26] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9}; void main() { int n; char temp[20]; struct telphone *head = NULL, *p, *pp; int i, len; long tel; int flag = 0; char output[7]; scanf("%d", &n); head = (struct telphone *)malloc(sizeof(struct telphone)); head->next = NULL; head->num = -1; head->f = 1; while(n--) { tel = 0; scanf("%s", temp); len = strlen(temp); for(i = 0; i < len; i++) { if(temp[i] >= 'A' && temp[i] <= 'Z') tel = tel * 10 + map[temp[i] - 'A']; else if(temp[i] >= '0' && temp[i] <= '9') tel = tel * 10 + temp[i] - '0'; } if(!(head->next)) { p = (struct telphone *)malloc(sizeof(struct telphone)); p->next = NULL; p->num = tel; p->f = 1; head->next = p; pp = (struct telphone *)malloc(sizeof(struct telphone)); pp->next = NULL; pp->num = 10000000; pp->f = 1; p->next = pp; } else { p = head; while(p && p->next) { if(tel >= p->num && tel < p->next->num) break; else p = p->next; } if(p->num == tel) (p->f)++; else { pp = (struct telphone *)malloc(sizeof(struct telphone)); pp->next = p->next; pp->num = tel; pp->f = 1; p->next = pp; } } } p = head; while(p) { if(p->f > 1) { for(i = 6; i >= 0; i--) { output[i] = p->num % 10 +'0'; p->num /= 10; } for(i = 0; i < 7; i++) { if(i == 3) printf("-"); printf("%c", output[i]); } flag = 1; printf(" %ld\n", p->f); } p = p->next; } if(!flag) printf("No duplicates.\n"); } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator