您的位置:首页 > 其它

487-3279电话号码映射

2010-05-17 11:35 141 查看
引用

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 129968Accepted: 22133
Description
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

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
The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.

Output
Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

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;

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()
{

char chPhone[20];
int Num;

PhoneBook Book1;
PhoneBook* tail;
tail=&Book1;

cout<<"Input Qty of PhoneNo:(0~100000)"<<endl;
cin>>Num;

while(Num<=0 || Num>100000)
{
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* a1;
PhoneBook* a2;
a1=&Book1;
if(a1->next)
a2=a1->next;

while(a1)
{
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;

//Result排序

Result* R_tmp;
R_tail=&R1;
if(R_tail->next)
R_tmp=R_tail->next;

SortResult(R_tail,R_tmp);

//Output
R_tail=&R1;
cout<<"Sample Output:"<<endl;
if(R1.dupli==1 && R1.next==NULL)
cout<<"No duplicates."<<endl;

while(R_tail->dupli>1)
{
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;
}

bool IsSame(int* a,int* b)
{
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];

b->dupli=tmp.dupli;
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: