引用
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 129968 | Accepted: 22133 |
Description
The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
Input
Output
No duplicates.
Sample Input
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
Sample Output
310-1010 2 487-3279 4 888-4567 3
分析:
1. 输入时,对于输入的字符进行映射关系匹配。设置结构体作为存储结构。
2. 结构体的排序,筛选。
我的代码(基本达成目标,有bug待改善)
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;
char chPhone[20]; PhoneBook Book1; while(Num<=0 || Num>100000) //存号码 //排序 PhoneBook* a1; while(a1) } //Result排序 Result* R_tmp; //Output while(R_tail->dupli>1) bool IsSame(int* a,int* b) } b->dupli=tmp.dupli;
struct PhoneBook
{
int p1[7];
struct PhoneBook* next;
};
struct Result
{
int p1[7];
int dupli;
struct Result* next;
};
//申明函数
bool Phonemapping(struct PhoneBook* book,char* phone);//字母映射数字
bool IsSame(int* a,int* b);//比较数组是否相同
void CopyInt(int* des,int* a);//copy数组
void SortResult(Result* a,Result* b);//对比Result
int main()
{
int Num;
PhoneBook* tail;
tail=&Book1;
cout<<"Input Qty of PhoneNo:(0~100000)"<<endl;
cin>>Num;
{
cout<<"wrong qty!"<<"/nInput Qty of PhoneNo:(0~100000)"<<endl;
cin.clear();
cin>>Num;
}
cout<<"Input PhoneNo:"<<endl;
while(Num)
{
cin>>chPhone;
if(Phonemapping(tail,chPhone))
{
if(Num!=1)
{
tail->next=new PhoneBook;
tail=tail->next;
tail->next=NULL;
}
Num--;
}
}
tail=NULL;
struct Result R1;
struct Result* R_tail;
R1.dupli=1;
R_tail=&R1;
PhoneBook* a2;
a1=&Book1;
if(a1->next)
a2=a1->next;
{
while(a2)
{
if(IsSame(a1->p1,a2->p1))
{
R_tail->dupli++;
CopyInt(R_tail->p1,a1->p1);
}
if (a2->next)
a2=a2->next;
else
break;
}
if(R_tail->dupli>1)
{
R_tail->next=new Result;
R_tail=R_tail->next;
R_tail->dupli=1;
R_tail->next=NULL;
}
if(a1->next)
{
a1=a1->next;
a2=a1->next;
}
else
break;
//多new了个struct Result.需要删掉.(为什么会多?可以改善么?)
int Edx;
R_tail=&R1;
for(Edx=0;R_tail->next;Edx++)
R_tail=R_tail->next;
for(R_tail=&R1;Edx>1;Edx--)
R_tail=R_tail->next;
R_tail->next=NULL;
R_tail=&R1;
if(R_tail->next)
R_tmp=R_tail->next;
SortResult(R_tail,R_tmp);
R_tail=&R1;
cout<<"Sample Output:"<<endl;
if(R1.dupli==1 && R1.next==NULL)
cout<<"No duplicates."<<endl;
{
char op[20];
sprintf(op,"%d%d%d-%d%d%d%d %d",R_tail->p1[0],R_tail->p1[1],R_tail->p1[2],R_tail->p1[3],R_tail->p1[4],R_tail->p1[5],R_tail->p1[6],R_tail->dupli);
cout<<op<<endl;
if(R_tail->next)
R_tail=R_tail->next;
else
break;
}
_getch();
return 0;
}
bool Phonemapping(struct PhoneBook* book,char* phone)
{
int index=strlen(phone);
int j=0,i=0;
for(;i<index;i++)
{
if(j==7)
break;
if ((phone[i]>='A' && phone[i]<='Y' && phone[i]!='Q')||(phone[i]>='0' && phone[i]<='9')||phone[i]=='-')
{
switch (phone[i])
{
case '0': book->p1[j++]=0;break;
case '1': book->p1[j++]=1;break;
case 'A': case 'B': case 'C': case '2': book->p1[j++]=2;break;
case 'D': case 'E': case 'F': case '3': book->p1[j++]=3;break;
case 'G': case 'H': case 'I': case '4': book->p1[j++]=4;break;
case 'J': case 'K': case 'L': case '5': book->p1[j++]=5;break;
case 'M': case 'N': case 'O': case '6': book->p1[j++]=6;break;
case 'P': case 'R': case 'S': case '7': book->p1[j++]=7;break;
case 'T': case 'U': case 'V': case '8': book->p1[j++]=8;break;
case 'W': case 'X': case 'Y': case '9': book->p1[j++]=9;break;
}
}
else
{
cout<<"wrong phone number"<<endl;
return false;
}
}
if ((j<7)||(j==7 && phone[i]!=0 && phone[i]!='-'))
{
cout<<"wrong phone number,not store"<<endl;
return false;
}
return true;
}
{
for(int i=0;i<7;i++)
{
if(*(a+i)!=*(b+i))
return false;
}
return true;
}
void CopyInt(int* des,int* a)
{
for(int i=0;i<7;i++)
*(des+i)=*(a+i);
void SortResult(Result* a,Result* b)
{
Result* last;
Result* lp_a;
lp_a=a;
last=a;
while(a)
{
while(b)
{
if(IsSame(a->p1,b->p1))
last->next=b->next;
last=b;
b=b->next;
}
if(a->next==NULL)
break;
a=a->next;
b=a->next;
last=a;
}
a=lp_a;
b=a->next;
while(a)
{
while(b)
{
//按照号码的字典升序输出
int big;
int index=0;
while(1)
{
if(index==7)
{
big=0;break;
}
if(a->p1[index] > b->p1[index])
{
big=1;break;
}
if(a->p1[index] < b->p1[index])
{
big=-1;break;
}
index++;
}
if(big==1)
{
Result tmp;
tmp.dupli=a->dupli;
for(int i=0;i<7;i++)
tmp.p1[i]=a->p1[i];
a->dupli=b->dupli;
for(int i=0;i<7;i++)
a->p1[i]=b->p1[i];
for(int i=0;i<7;i++)
b->p1[i]=tmp.p1[i];
}
b=b->next;
}
if(a->next==NULL)
break;
a=a->next;
b=a->next;
}
}