【问题描述】建立一个通讯录的结构记录,包括姓名、生日、电话号码。输入n(n<10)个朋友的信息,再按他们的年龄从大到小的顺序依次输出其信息

【输入形式】先输入朋友数n(整型,n<10),再依次输入每个朋友的姓名(字符串)、生日(整型)、和电话号码(字符串)。

【输入输出样例】(下划线部分表示输入)

Input n:3

Input the name,birthday,number of the 1 friend:zhang 19850403 13912345678

Input the name,birthday,number of the 2 friend:wang 19821020 0571-88018448

Input the name,birthday,number of the 3 friend:qian 19840619 13609876543

wang□19821020□0571-88018448

qian□19840619□13609876543

zhang□19850403□13912345678

【样例说明】

按朋友年龄从大到小的顺序依次输出每位朋友的信息,内容与格式如下

姓名□生日□电话号码

□表示空格

以下为答案代码:

#include<stdio.h>
struct friends{
char name[50];
int birth;
char phone[50];
               };
                    
int main(){
   int n;
   printf("Input n:");
   scanf("%d",&n);
   struct friends a[11];
   int i,j;
   int g=1;
    for(i=0;i<n;i++){
    printf("Input the name,birthday,number of the %d friend:",g);
    scanf("%s%d%s",&a[i].name,&a[i].birth,&a[i].phone);
    g++;
        }
   //以上为准备工作 
   struct friends temp;
   for(i=0;i<n-1;i++){
     for(j=0;j<n-i-1;j++){
        if(a[j].birth>a[j+1].birth){
          temp=a[j];
          a[j]=a[j+1];
          a[j+1]=temp;
                     }
         }
     }
  //以上利用冒泡排序将年龄排好 
    for(j=0;j<n;j++){
    printf("%s %d %s",a[j].name,a[j].birth,a[j].phone);
    printf("\n");
               }
   
    return 0;
          } 

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值