本文主要是介绍PAT-乙级 1028 人口普查(C语言),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
借鉴:https://blog.csdn.net/qq_40941722/article/details/94429909
有错版本:2020/06/21 02:16:12
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int queryAgeLegitimacy(char* age) {int year, month, day;age[4] = '\0'; age[7] = '\0';char* yearPointer = &age[0];char* monthPointer = &age[5];char* dayPointer = &age[8];year = atoi(yearPointer); month = atoi(monthPointer); day = atoi(dayPointer);if (year < 1814 || year>2014)return 0;else if ((year == 1814 && month <= 9 && day < 6) || (year == 2014 && month >= 9 && day > 6))return 0;elsereturn year + month + day;
}
int main() {int residentNumbers, minAge = 10000, maxAge = 0, middleAge, count = 0;char name[6], maxName[6], minName[6];char age[11];scanf("%d", &residentNumbers);for (int i = 0; i < residentNumbers; i++) {scanf("%s", &name);scanf("%s", &age);middleAge = queryAgeLegitimacy(&age);if (middleAge != 0) {count++;if (middleAge > maxAge) {maxAge = middleAge;strcpy(minName, name);}if (middleAge < minAge) {minAge = middleAge;strcpy(maxName, name);}}}printf("%d %s %s", count,maxName,minName);//system("pause");return 0;
}
更正版本:2020/06/21 02:52:34
#include<stdio.h>
#include<string.h>
const char minInputAge[11] = "2014/09/06";
const char maxInputAge[11] = "1814/09/06";
int main() {int residentNumber, i, count = 0;char maxAge[11] = "1814/09/06", minAge[11] = "2014/09/06", maxName[11], minName[11];char name[6], age[11];scanf("%d", &residentNumber);for (i = 0; i < residentNumber; i++) {scanf("%s", &name);scanf("%s", &age);if (strcmp(maxInputAge,age)<=0&&strcmp(minInputAge,age)>=0){count++;if (strcmp(minAge, age) > 0) {strcpy(minAge, age);strcpy(maxName, name);}if (strcmp(maxAge, age)<0) {strcpy(maxAge, age);strcpy(minName, name);}}}printf("%d", count);if (count > 0)printf(" %s %s", maxName,minName);//system("pause");return 0;
}
这篇关于PAT-乙级 1028 人口普查(C语言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!