根本不慌 2019-03-30 16:45
浏览 191

PAT乙1028 求求各位大哥大嫂,萌新测试点2死活过不去

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。
输入格式:
输入在第一行给出正整数 N,取值在(0,10
​5
​​];随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。
输出格式:
在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。
输入样例:
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
输出样例:
3 Tom John
#include
#include
#include
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef struct List {
char name[6];
char date[11];
int year, month, day;
};
typedef struct seq{
List data[100000];
};
bool compare(List a, List b)
{
if (a.year != b.year)
return a.year > b.year;
else if (a.year == b.year)
if (a.month != b.month)
return a.month > b.month;
else return a.day > b.day;
}
int main()
{
seq L;
int count = 0;
cin >> count;
for (int i = 0; i < count; i++)
{
cin >> L.data[i].name >> L.data[i].date;
L.data[i].year = (L.data[i].date[0] - '0') * 1000 + (L.data[i].date[1] - '0') * 100 + (L.data[i].date[2] - '0') * 10 + (L.data[i].date[3] - '0');
L.data[i].month = (L.data[i].date[5] - '0') * 10 + L.data[i].date[6] - '0';
L.data[i].day = (L.data[i].date[8] - '0') * 10 + L.data[i].date[9] - '0';
getchar();
}//将所有数据写到结构体数组中
sort(L.data,L.data+(count), compare);//不管有没有错误数据,先从年轻到年老排序
int count1 = count, count2 = 0;
count = 0;
char oldest[6], youngest[6];
for (int i = 0; i < count1; i++)//过滤错误数据
{
if (count == 1 && count2 == 0)
{
strcpy(youngest, L.data[i - 1].name);
count2++;
}
if (L.data[i].year > 1814 && L.data[i].year {
count++;
strcpy(oldest, L.data[i].name);
}
else if (L.data[i].year == 1814)
{
if (L.data[i].month > 9)
{
count++;
strcpy(oldest, L.data[i].name);
}
else if (L.data[i].month == 9)
if (L.data[i].day >= 6)
{
count++;
strcpy(oldest, L.data[i].name);
}
}
else if(L.data[i].year==2014)
{
if (L.data[i].month < 9)
{
count++;
strcpy(oldest, L.data[i].name);
}
else if (L.data[i].month == 9)
if (L.data[i].day <= 6)
{
count++;
strcpy(oldest, L.data[i].name);
}
}
}
if (count == 0)
{
cout << count;
return 0;
}
cout << count <<" "<< oldest <<" "<< youngest;
system("pause");
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32流水灯+呼吸灯+外部中断按键
    • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
    • ¥15 NX MCD仿真与博途通讯不了啥情况
    • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
    • ¥15 gradio的web端页面格式不对的问题
    • ¥15 求大家看看Nonce如何配置
    • ¥15 Matlab怎么求解含参的二重积分?
    • ¥15 苹果手机突然连不上wifi了?
    • ¥15 cgictest.cgi文件无法访问
    • ¥20 删除和修改功能无法调用