| ||||||||||
| 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 | |||||||||
1002题,求助啊各位大哥 请问http://acm.pku.edu.cn/ 上的第1002题为什么把电话号码设为long 型通不过,
以下是从网上找的一个通过了的c++的程序
但当输入网页上的测试数据时输出却是:
002-0818 2
002-3615 4
-02--8329 3
这很明显是一个错误的结果,却能通过
#include<stdio.h>
#include<stdlib.h>
int cmp(const void *elem1,const void *elem2)
{
return (*(int*)elem1-*(int*)elem2);
}
void main()
{
int a[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};
int pow[7]={1000000,100000,10000,1000,100,10,1};
int *tels,no=1,count;
int n,i,j,s;
char raw[41],temp;
scanf("%d",&n);
tels=new int [n];
for(i=0;i<n;i++)
{
tels[i]=0;
scanf("%s",raw);
s=0;
for(j=0;j<7;s++)
{
temp=raw[s];
if(temp>='0'&&temp<='9')
{
tels[i]+=pow[j]*(temp-'0');
j++;
}
else
if(temp>='A'&&temp<='Z')
{
tels[i]+=pow[j]*a[temp-'A'];
j++;
}
}
}
qsort(tels,n,sizeof(int),cmp);
for(i=0;i<n;)
{
count=0;
for(j=i+1;j<n&&tels[j]==tels[i];j++)
count++;
if(count)
{
no=0;
printf("%03d-%04d %d\n",tels[i]/10000,tels[i]%10000,++count);
}
i=j;
}
if(no)
printf("No duplicates.\n");
}
以下是我用C写的用二叉排序树实现的程序
其结果能与网页上的测试数据相稳合,
但不能通过
#include<stdio.h>
#include<stdlib.h>
int inc;
struct Node
{
long phone;
long repeat;
struct Node *left,*right;
}*Node;
void createtree(struct Node *t,long temp)
{
struct Node *s,*p;
s=t;
if(t->phone==0)
{
t->phone=temp;
t->repeat=1;
t->left=t->right=NULL;
return;
}
while(t)
{
if(temp==t->phone) {(t->repeat)++;inc=1;return;}
else if(temp>t->phone) {s=t;t=t->right;}
else {s=t;t=t->left;}
}
p=(struct Node *)malloc(sizeof(struct Node));
p->phone=temp;
p->repeat=1;
p->left=p->right=NULL;
if(temp>s->phone) s->right=p;
else s->left=p;
}
void output(struct Node *head)
{
if(!head) return;
output(head->left);
if(head->repeat!=1)
printf("%ld-%ld %ld\n",head->phone/10000,head->phone%10000,head->repeat);
output(head->right);
}
void freeNode(struct Node *head)
{
struct Node *left,*right;
if(!head) return;
left=head->left;
right=head->right;
free((void *)head);
freeNode(left);
freeNode(right);
}
int main()
{
int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
long i,j,k,n;
char ch;
long temp;
struct Node *head,*p,*q,*s;
head=(struct Node *)malloc(sizeof(struct Node));
head->left=head->right=NULL;
head->phone=0;
scanf("%ld",&n);
getchar();
inc=0;
for(i=0;i<n;i++)
{
temp=0;
while((ch=getchar())!='\n')
{
if(ch>='A'&&ch<='Z') temp=temp*10+a[ch-65];
else
if(ch>='0'&&ch<='9') temp=temp*10+ch-48;
}
createtree(head,temp);
}
if(inc==0) printf("No duplicates.\n");
else
output(head);
freeNode(head);
return 0;
}
而必须把我的程序改为如下才能通过,真郁闷
#include<stdio.h>
#include<stdlib.h>
int inc;
struct Node
{
long phone;
long repeat;
struct Node *left,*right;
}*Node;
void createtree(struct Node *t,long temp)
{
struct Node *s,*p;
s=t;
if(t->phone==0)
{
t->phone=temp;
t->repeat=1;
t->left=t->right=NULL;
return;
}
while(t)
{
if(temp==t->phone) {(t->repeat)++;inc=1;return;}
else if(temp>t->phone) {s=t;t=t->right;}
else {s=t;t=t->left;}
}
p=(struct Node *)malloc(sizeof(struct Node));
p->phone=temp;
p->repeat=1;
p->left=p->right=NULL;
if(temp>s->phone) s->right=p;
else s->left=p;
}
void output(struct Node *head)
{
if(!head) return;
output(head->left);
if(head->repeat!=1)
printf("%03d-%04d %ld\n",(int)(head->phone)/10000,(int)(head->phone)%10000,head->repeat);
output(head->right);
}
void freeNode(struct Node *head)
{
struct Node *left,*right;
if(!head) return;
left=head->left;
right=head->right;
free((void *)head);
freeNode(left);
freeNode(right);
}
int main()
{
int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
long i,j,k,n;
char ch;
long temp;
struct Node *head,*p,*q,*s;
head=(struct Node *)malloc(sizeof(struct Node));
head->left=head->right=NULL;
head->phone=0;
scanf("%ld",&n);
getchar();
inc=0;
for(i=0;i<n;i++)
{
temp=0;
while((ch=getchar())!='\n')
{
if(ch>='A'&&ch<='Z') temp=temp*10+a[ch-65];
else
if(ch>='0'&&ch<='9') temp=temp*10+ch-48;
}
createtree(head,temp);
}
if(inc==0) printf("No duplicates.\n");
else
output(head);
freeNode(head);
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator