zoukankan      html  css  js  c++  java
  • 1028 人口普查(20)(20 分)

    1028 人口普查(20)(20 分)

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

    这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过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
    

    思考

    在读入的时候就应该过滤掉不合理的生日

    写个结构体比较好。

    AC代码

    #include<stdio.h>
    struct birth{
    	char name[6];
    	int year;
    	int month;
    	int day;
    };
    int cmp(struct birth a,struct birth b){
    	if(a.year<b.year)
    	return 0;//代表a老 
    	else if(a.year>b.year)
    	return 1;//代表b老 
    	if(a.month<b.month)
    	return 0;
    	else if(a.month>b.month) 
    	return 1;
    	if(a.day<b.day)
    	return 0;
    	else if(a.day>b.day)
    	return 1;
    	return 0;//完全相同,默认不操作
    }
    int main(){
    	int N,number=0;
    	scanf("%d",&N);
    	struct birth person[N+2];
    	person[0].year=1814;
    	person[0].month=9;
    	person[0].day=6;
    	person[N+1].year=2014;
    	person[N+1].month=9;
    	person[N+1].day=6;
    	
    	int old=N+1,young=0; //两个哨兵,一比较就要更新 
    	int flag;
    	for(int i=1;i<N+1;i++){
    		scanf("%s %d/%d/%d",person[i].name ,&person[i].year ,&person[i].month,&person[i].day);
    		//printf("%s %d/%d/%d",person[i].name,person[i].year,person[i].month,person[i].day);
    		flag=cmp(person[i],person[N+1]);
    		if(flag)
    		continue;
    		flag=cmp(person[0],person[i]);
    		if(flag)
    		continue;
    		
    		number++;//这一句正常运转了 
    		flag=cmp(person[old],person[i]);
    		if(flag)
    		old=i;
    		flag=cmp(person[i],person[young]);
    		if(flag)
    		young=i;
    	}
    	//printf("%d %d
    ",old,young);//没更新,所以就失败 
    	if(number!=0) 
    	printf("%d %s %s",number,person[old].name,person[young].name);
    	else printf("0");
    	return 0;//有一个样例过不去,提示为格式错误 ,要特判一种情况,全部日期都不合理 
    } 
    

    胡凡的代码要短一点,而且结构要比我的清晰,我的比较绕,自己就容易绕进去,而且占据的空间资源也比较大了,最后一个样例内存差距有10倍

    B1004就有这种风格上的问题

    #include <cstdio>
    struct person {
        char name[10];
        int yy, mm, dd;
    }oldest, youngest, left, right, temp;
    
    bool LessEqu(person a, person b) {
        if(a.yy != b.yy) return a.yy <= b.yy;
        else if(a.mm != b.mm) return a.mm <= b.mm;
        else return a.dd <= b.dd;
    }
    bool MoreEqu(person a, person b) {
        if(a.yy != b.yy) return a.yy >= b.yy;
        else if(a.mm != b.mm) return a.mm >= b.mm;
        else return a.dd >= b.dd;
    }
    void init() {
        youngest.yy = left.yy = 1814;
        oldest.yy = right.yy = 2014;
        youngest.mm = oldest.mm = left.mm = right.mm = 9;
        youngest.dd = oldest.dd = left.dd = right.dd = 6;
    }
    int main() {
        init();
        int n, num = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);
            if(MoreEqu(temp, left) && LessEqu(temp, right)) {
                num++;
                if(LessEqu(temp, oldest)) oldest = temp;
                if(MoreEqu(temp, youngest)) youngest = temp;
            }
        }
        if(num == 0) printf("0
    ");
        else printf("%d %s %s
    ", num, oldest.name, youngest.name);
        return 0;
    }
    

  • 相关阅读:
    关于宇宙大爆炸的理论模型
    算法系列2《RSA》
    Codeforces Round #248 (Div. 1)——Nanami&#39;s Digital Board
    Cocos2d-x场景变化相关功能介绍
    NYOJ 745 蚂蚁问题(两)
    quick-cocos2d-x endToLua 退出会卡住
    编程算法
    linux基础知识1
    URAL 1553. Caves and Tunnels 树链拆分
    2014/11/13_ 随想
  • 原文地址:https://www.cnblogs.com/lingr7/p/9391280.html
Copyright © 2011-2022 走看看