【exercise 已解决】 电话号码

来源:互联网 发布:如何练英语口语 知乎 编辑:程序博客网 时间:2024/05/10 04:05

解决这个问题过程中发现了很多问题,包括对于函数调用的不熟悉,以及自己思维上的缺陷,有时候算法想出来了,但是实现起来,数据结构方面考虑的不完善,往往到了一半,发现到操作不方便。

然而有趣的是,在代码未完善的时候,运行起来,360居然报警了,说是木马程序。后来给同学看,也没分析出个所以然来~~


电话号码

 

【问题描述】

 

假设电话号码的标准形式是一个7位数长的十进制数,在第三个与第四个数字之间有一个连字符(如888-1200)。现构建了一个从字母到数字的映射关系,使得电话号码可以对应到单词,从而方便电话号码的记忆。

例如一个名为Gino的披萨店的电话号码为310-4466,那么就可以用310-GINO作为其便于记忆的电话号码。

映射关系如下:

A, B, C对应2

D, E, F对应3

G, H, I对应4

J, K, L对应5

M, N, O对应6

P, R, S对应7

T, U, V对应8

W, X, Y对应9

QZ没有对应的数字,连字符可以出现在任何便于记忆的位置。例如TUT-GLOP对应888-4567,310-GINO对应310-4466,3-10-10-10对应310-1010

同一个号码可以有不同的记忆形式,例如310-44-66310-4466310-GINO都对应310-4466这个号码。

给定一个电话号码本,检查其中是否有重复的电话号码。

请编写一个程序,用来验证是否存在重复的电话号码。

 

【输入形式】

 

从标准输入读取数据。

输入的每行为一个电话号码,可能由数字、大写字母(没有QZ)以及连字符所构成,总共不超过100000个电话号码。

 

【输出形式】

 

向标准输出打印结果。

按照电话号码数字递增的顺序排序输出所有出现了超过一次的电话号码。

每行输出一个电话号码,其格式为:电话号码的标准化形式,然后一个空格,之后是出现的次数。

如果没有重复的电话号码,则输出No

 

【输入样例】

 

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

 

【输出样例】

 

310-1010 2

487-3279 4

888-4567 3

 


//代码

fstream file("G:\\C++ primer\\number.txt");void get_in()//读取txt{string line,word;int x=0,y=0;while(getline(file,line)){istringstream stream(line);while( stream >> word ){number_before.push_back(word);}}for (vector<string>::size_type ix = 0; ix != number_before.size(); ix++ ){for (vector<string>::size_type iy = 0; iy != number_before[ix].size(); iy++){if (number_before[ix][iy] == '-'){} else{number_after1[x][y] = number_before[ix][iy];y++;}}y = 0;x++;}cout << endl;}void deal()//处理,将所有的英文字母转换为数字{for (vector<string>::size_type ix = 0; ix < number_before.size(); ix++ ){for (int iy = 0; iy < 7 ; iy++){switch(number_after1[ix][iy]){case 'A':case 'B':case 'C':number_after1[ix][iy]='2';break;case 'D':case 'E':case 'F':number_after1[ix][iy]='3';break;case 'G':case 'H':case 'I':number_after1[ix][iy]='4';break;case 'J':case 'K':case 'L':number_after1[ix][iy]='5';break;case 'M':case 'N':case 'O':number_after1[ix][iy]='6';break;case 'P':case 'R':case 'S':number_after1[ix][iy]='7';break;case 'T':case 'U':case 'V':number_after1[ix][iy]='8';break;case 'W':case 'X':case 'Y':number_after1[ix][iy]='9';break;}}}}int x;void get_out()//查找重复,并输出{int count = 0, any = 0, resualt[1000];for (vector<string>::size_type ix = 0; ix < number_before.size(); ix++){for (vector<string>::size_type iy = 0; iy < number_before.size(); iy++){if (( number_after1[ix] == number_after1[iy] ) && (ix != iy) && (number_after1[iy]!= "0000000")){number_after1[iy] = "0000000";number_final[x] = number_after1[ix];any=1;count++;}}resualt[x] = count+1;x++;count = 0;}if (!any){cout <<"NO";}for(int i=0;i<=x;i++){if (number_final[i] != "0000000"){for(int j = 0; j<7; j++){if (j == 3){cout << '-';}cout << number_final[i][j];}cout<<" " << resualt[i] << endl;}}}int main(){time_t start,end,time;start=clock();get_in();deal();cout << endl;get_out();end=clock(); time=end-start;//这里的时间是计算机内部时间cout << endl << "time:" << time << endl;system("pause");return 0;}