【输入形式】先输入朋友数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;
}