Given 10 boxes ,numbered from 0 to 9, to store mail items. Every item is also signed with a phone number of its receiver. Write a policy to distribute the items into the boxes. The rule is: try to mach the box number with the last digit of the related phone number, if the box has already ocuppied, use the quadratic probing method to deal with the collision.
输入格式:
a seris of phone numbers separated by commas
输出格式:
the phone numbers of the items in the boxes which orderd from box 0 to 9. If the box is empty, output 0。
输入样例:
13801014370,13901021320,13621026170
输出样例:
13801014370,13901021320,0,0,13621026170,0,0,0,0,0
C语言代码:
#include<stdio.h>
#include<string.h>
char * startPos[10];
int main(){
char string[120];
fgets(string,120,stdin);
int i;
for(i=0;i<10;i++) startPos[i]=NULL;
for(i=0;i*12<strlen(string)-2;i++){
int num=string[i*12+10]-'0';
int increcement=0;
while(startPos[num]){
num+=2*(increcement++)+1;
while(num>9) num-=10;
}
startPos[num]=string+i*12;
}
for(i=0;i<9;i++){
if(startPos[i]){
int j;
for(j=0;j<11;j++){
printf("%c",startPos[i][j]);
}
printf(",");
}
else printf("0,");
}
if(startPos[i]){
int j;
for(j=0;j<11;j++){
printf("%c",startPos[i][j]);
}
}
else printf("0");
printf("\n");
return 0;
}