| ||||||||||
| 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 | |||||||||
感觉它的编译器有问题那位大牛看看我的有什么问题。
表示gcc,g++,MS VC++全部通过测试。先前用气泡排序,提示超时。改进了排序算法,快速排序倒退成WA了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void QSort(char (*tel)[8],long low, long high);
char map[]="2223334445556667-77888999-";
int main(void) {
long i,j;
long n; // sum of tel
long count=1; // count duplicates
char (*tel)[8],ch;
scanf("%ld",&n);
tel = (char (*)[8])malloc(n*sizeof(char [8]));
if (!tel) exit(1); // overflow
ch = getchar(); // eliminate line break '\n'
for (i=0; i<n; i++) {
j = 0;
while ((ch = getchar()) != EOF) {
if (ch=='-' || ch == 'Q' || ch == 'Z')
continue; // ignore '-' 'Q' 'Z' chars
if (j<7) {
if (ch>='A' && ch<'Z')
tel[i][j++] = map[ch-'A']; // convert to num
else if (ch>='0' && ch<='9')
tel[i][j++] = ch;
}
else if (j==7)
tel[i][j++] = '\0';
if (ch=='\n') break; // fflush(stdin)
}
}
QSort(tel,0,n-1);
if (n==1) printf("No duplicates.\n");
for (i=0; i<n-1; i++) {
long m=0;
if (strcmp(tel[i],tel[i+1])<0) {
if (count>1) {
for (j=0; j<7; j++) { // output duplicates
putchar(tel[i][j]);
if (j==2) putchar('-');
}
printf(" %ld\n",count);
count = 1; // reset count
}
else
m++; // no duplicates count
}
else
count++; // duplicates count
if (m==n-1) printf("No duplicates.\n");
}
if (count>1) { // last duplicates
for (j=0; j<7; j++) {
putchar(tel[i][j]);
if (j==2) putchar('-');
}
printf(" %ld\n",count);
}
free(tel); tel = NULL;// free memory
return 0;
}
void QSort(char (*tel)[8],long low, long high){
long left = low; long right = high;
char pivot[8];
if (left>=right)
return;
memcpy(pivot,tel[left],8);
while (left<right){
while (left<right && strcmp(tel[right],pivot)>=0)
right--;
memcpy(tel[left],tel[right],8);
while (left<right && strcmp(tel[left],pivot)<=0)
left++;
memcpy(tel[right],tel[left],8);
}
memcpy(tel[left],pivot,8);
QSort(tel,low,left-1);
QSort(tel,left+1,high);
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator