输入n个朋友的信息,包括姓名、生日、电话号码,本题要求编写程序,按照年龄从大到小的顺序依次输出通讯录。题目保证所有人的生日均不相同。 输入格式: 输入第一行给出正整数n(<10)。随后n行,每行按照“姓名 生日 电话号码”的格式给出一位朋友的信息,其中“姓名”是长度不超过10的英文字母组成的字符串,“生日”是yyyymmdd格式的日期,“电话号码”是不超过17位的数字及+、-组成的字符串。用C语言编写
时间: 2024-03-21 21:44:16 浏览: 48
以下是C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct friend_info { // 定义结构体存储朋友信息
char name[11];
char birthday[9];
char phone[18];
int age;
};
int cmp(const void *a, const void *b) { // 比较函数
return ((struct friend_info*)b)->age - ((struct friend_info*)a)->age;
}
int main() {
int n;
scanf("%d", &n);
struct friend_info friends[n];
for (int i = 0; i < n; i++) {
scanf("%s %s %s", friends[i].name, friends[i].birthday, friends[i].phone);
int year = atoi(friends[i].birthday) / 10000;
friends[i].age = 2021 - year; // 计算年龄
}
qsort(friends, n, sizeof(struct friend_info), cmp); // 排序
for (int i = 0; i < n; i++) {
printf("%s %s %s\n", friends[i].name, friends[i].birthday, friends[i].phone); // 输出
}
return 0;
}
```
输入格式示例:
```
3
Alice 19981012 12345678901
Bob 19961120 +8612345678901
Charlie 19950508 88888888
```
输出格式示例:
```
Bob 19961120 +8612345678901
Alice 19981012 12345678901
Charlie 19950508 88888888
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)