习题9-5 通讯录排序 (20分)

输入n个朋友的信息,包括姓名、生日、电话号码,本题要求编写程序,按照年龄从大到小的顺序依次输出通讯录。题目保证所有人的生日均不相同。

输入格式:

输入第一行给出正整数n(<10)。随后n行,每行按照“姓名 生日 电话号码”的格式给出一位朋友的信息,其中“姓名”是长度不超过10的英文字母组成的字符串,“生日”是yyyymmdd格式的日期,“电话号码”是不超过17位的数字及+-组成的字符串。

输出格式:

按照年龄从大到小输出朋友的信息,格式同输出。

输入样例:

3
zhang 19850403 13912345678
wang 19821020 +86-0571-88018448
qian 19840619 13609876543

输出样例:

wang 19821020 +86-0571-88018448
qian 19840619 13609876543
zhang 19850403 13912345678

解答:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct {

    char name[11];
    int birth;
    char phone[18];

}friends;

int main()
{

    friends *p,t;
    int n,i,j;

    scanf("%d",&n);
    getchar();
    p=(friends*)malloc(sizeof(friends)*n);

    for(i=0;i<n;i++){

        scanf("%s %d %s",p[i].name,&p[i].birth,p[i].phone);

    }

    for(i=0;i<n;i++){

        for(j=0;j<n-1-i;j++){

            if(p[j].birth>p[j+1].birth){

                t=p[j];
                p[j]=p[j+1];
                p[j+1]=t;

            }
        }
    }

    for(i=0;i<n;i++){

        printf("%s %d %s\n",p[i].name,p[i].birth,p[i].phone);

    }

    free(p);

    return 0;

}
发布了98 篇原创文章 · 获赞 2 · 访问量 3718

猜你喜欢

转载自blog.csdn.net/qq_30377869/article/details/104807222
今日推荐