Statistical phone number

Statistical phone number

Companies prefer to use easy to remember telephone numbers. Make a telephone number of a way to remember it is written in an easy to remember word or phrase. For example, when you need to call the University of Waterloo, you can call TUT-GLOP. Sometimes, only the phone number part numbers spelled words. When you get back to the hotel at night, you can order a pizza to Gino's by dialing 310-GINO. Another way to make a telephone number to be remembered is to be a good way to remember the digits of the number are grouped. By "three ten" number to dial Pizza Hut's 3-10-10-10, you can order pizza from them.
Standard format telephone number is seven decimal number, and a connector between the third and fourth digits. Telephone dial provides a mapping from letters to numbers, the mapping relationship is as follows: A, B, and C are mapped to 2 D, E, and F are mapped to the 3 G, H, and I are mapped to 4 J, K, and L are mapped to 5 M, N, and O mapping to 6 P, R, and S is mapped to a 7 T, U, and V are mapped to 8 W, X, and Y are mapped to 9 Q and Z are not mapped to any number, a hyphen is not requires dial-up, you can add and delete arbitrary. TUT-GLOP the standard format is a standard format 888-4567,310-GINO standard format 310-4466,3-10-10-10 is 310-1010. If the two numbers have the same standard format, then they are identical (same dial-up)
your company is writing a telephone directory for the local companies. As part of quality control, you want to check whether there are two and more companies have the same telephone number.
Input: the format of the input, the first row is a positive integer specifying the number in the phone book numbers (up to 100,000). The remaining one per line is a telephone number. Each phone number consists of numbers and uppercase (except for Q and Z), and a connector consisting
output: number of repeating each occurrence produce one line of output, the output is a standard format of the number is followed by a space and its repetitions. If the presence of a plurality of duplicate output numbers in ascending order according to the number of the dictionary. If there are no duplicate numbers, output line:
No duplicates.

Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 char map[]="22233344455566677778889999";  //Q和Z先正常考虑,如若不考虑,对应关系就会受到影响。
 char str[80],telNumbers[100000][9];
 int compare(const void *elem1,const void *elem2)  //const修饰指向的对象,A可变,A指向的对象不可变
 	 //为函数模板sort定义数组元素的比较函数 
 {
 	 return(strcmp((char*)elem1,(char*)elem2)); 
 };
 void standardizeTel(int n)//是一个函数,使标准化,规格化 。简洁易读。
 {
 	int j,k; 	
 	j=k=-1;//... 
 	while(k<8)
 	{
 		j++;
		 if(str[j]=='-')	
 			continue;
 		k++;
 		if(k==3)
 		{
 			telNumbers[n][k]='-';
 			k++;
		 }
		 if(str[j]>='A'&&str[j]<='Z') 
		 {
		 	telNumbers[n][k]=map[str[j]-'A'];
		 	continue;
		 };
		 telNumbers[n][k]=str[j];	
	} 
	telNumbers[n][k]='\0';
	return;
 }
 int main()
 {
 	int n,j,i;
 	bool noduplicate;
 	
 	scanf("%d",&n);
 	for(i=0;i<n;i++)	//输入电话号码,储存到数组telNumbers中
 	{
		 scanf("%s",str);
		 standardizeTel(i);//将str中的电话号码转化成标准形式 ,储存在telNumbers中第i行 
	 }
	 qsort(telNumbers,n,9,compare);//对输入的电话号码进行排序
	  noduplicate=true;//对布尔变量赋值true 
	  i=0;
	  while(i<n)//搜索重复的电话号码,并进行输出
	  {	 
	   j=i;//j用来记录原来的位置 
	  i++;
	  while(i<n&&strcmp(telNumbers[i],telNumbers[j])==0) i++;//相邻的比较,相同则i加一,此时是已经排序了的
	  if(i-j>1)  //大于1说明存在相同的两行 
	  {
	  	printf("%s%d\n",telNumbers[j],i-j);
		noduplicate=false;
	   } 	
	   } 
	 if(noduplicate) printf("No duplicates.\n");
}

analysis
Number for duplicate telephone numbers (1) determining the input: the various telephone numbers representation into a standard representation: a string of 8 characters are the first three figures, the first four characters are '-', after four characters are numbers.
(2) a two-dimensional array telNumbers [100000] [9] to store the entire phone number, a telephone number stored in each row
of the standard representation. Each telephone number is read, it is first converted into a standard representation, and then stored in a two-dimensional array
telNumbers in.
(3) telNumbers as a one-dimensional array, where each element is a string, sort function template with C / C ++ provides the sort. Comparison with a string comparison function strcmp telNumbers adjacent phone number, determining whether there are duplicate telephone numbers, and calculates the number of repetitions.
to sum up
(1) First, the longer the subject of this question requires careful analysis to identify the key points. Secondly, the issues involved are more characters, more form, need to consider how to convert more convenient and clear.
(2) to solve this problem, the more we want to use the function. As the beginner, you need to know too much.
sort () is defined in the header file <algorithm> in. The sort function is a function of the standard template library, the start and end of address is known to sort, any container can be used for comparison (random iterators must meet) any element, any condition, execution speed is generally faster than qsort, quite in its upgraded version. Further, Sort () is a generic function, it can be used to compare any container, any element, any conditions.
int compare (const void * elem1, const void * elem2) is qsort comparison function.
strcmp () is also a comparison function.
** standardize () ** make the program simple and clear structure.

Guess you like

Origin blog.csdn.net/m0_45128748/article/details/91781237