7-9 How to Place the Mail

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值