1028 人口普查 (20 分)

1028 人口普查 (20 分)

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

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数 N,取值在(0,105];随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人1的姓名,其间以空格分隔。

输入样例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

输出样例:

3 Tom John

思路

1.有效生日的个数,计数有效生日的个数falg++

2.最年长人older 最年轻人younger

3.判断生日是否有效:若有效,判断年龄最大的人和最年轻的人

代码

改了又改还是部分格式错误的代码

#include<stdio.h> 
#include<string.h>
struct birth{
	char name[6];
	int year;
	int month;
	int day;
}older, younger, temp;

int main(){
	int n;
	scanf("%d", &n);
	int vaild = 0;
	int max_year = 2015, max_month = 10, max_day = 7;
	int min_year = 1813, min_month = 8, min_day = 5;
	char older_name[6];
	char younger_name[6];
	for(int i = 0; i < n; i++){
		scanf("%s%d/%d/%d", &temp.name, &temp.year, &temp.month, &temp.day);
		if(temp.year == 1814){//在边界日期处 
			if(temp.month == 9){
				if(temp.day >= 6){
					vaild++;
					if( temp.day < max_day){
						max_year = temp.year;
						max_month = temp.month;
						max_day = temp.day;
						strcpy(older_name, temp.name);
						
					}
					if(temp.day > min_day){
						min_day = temp.day;
						min_month = temp.month;
						min_year = temp.year;
						strcpy(younger_name, temp.name);
					}
				}				 
			}else if(temp.month > 9){
				vaild++;
				if(temp.month < max_month){
					max_year = temp.year;
					max_month = temp.month;
					max_day = temp.day;
					strcpy(older_name, temp.name);
				}
				if(temp.month > min_month){
					min_year = temp.year;
					min_month = temp.month;
					min_day = temp.day;
					strcpy(younger_name, temp.name);
				}
			} 
		}else if(temp.year > 1814 && temp.year < 2014){
			vaild++;
			if(temp.year == max_year){//同年 
				if(temp.month == max_month){ //同年同月 
					if(temp.day < max_day) { 
						max_year = temp.year;
						max_month = temp.month;
						max_day = temp.day;
						strcpy(older_name, temp.name);
					}
					if(temp.day > min_day){
						min_year = temp.year;
						min_month = temp.month;
						min_day = temp.day;
						strcpy(younger_name, temp.name);
					}
				}else{//同年不同月 
					if(temp.month < max_month){
						max_year = temp.year;
						max_day = temp.day;
						max_month = temp.month;
						strcpy(older_name, temp.name);
					}
					if(temp.month > min_month){
						min_year = temp.year;
						min_day = temp.day;
						min_month = temp.month;
						strcpy(younger_name, temp.name);
					}	
				}	
			}else{//不同年 
				if(temp.year < max_year){
					max_year = temp.year;
					max_month = temp.month;
					max_day = temp.day;
					strcpy(older_name, temp.name);	
				} 
				if(temp.year > min_year){
					min_year = temp.year;
					min_month = temp.month;
					min_day = temp.day;
					strcpy(younger_name, temp.name);
				}	
			} 
		}else if(temp.year == 2014){
			if(temp.month == 9){
				if(temp.day <= 6) {
					vaild++;
					if(temp.day < max_day){
						max_month = temp.month;
						max_year = temp.year;
						max_day = temp.day;
						strcpy(older_name, temp.name);
					}
					if(temp.day < min_day){
						min_month = temp.month;
						min_year = temp.year;
						min_day = temp.day;
						strcpy(younger_name, temp.name);
					}
				}
			}else if(temp.month < 9){
				vaild++;
				if(temp.month < max_month){
					max_year = temp.year;
					max_month = temp.month;
					max_day = temp.day;
					strcpy(older_name, temp.name);
				}
				if(temp.month > min_month){
					min_month = temp.month;
					min_year = temp.year;
					min_day = temp.day;
					strcpy(younger_name, temp.name);
				}
			} 
		}
	}
	
	printf("%d %s %s\n", vaild, older_name, younger_name);
	return 0;
}
  • 测试点3错误:格式错误在于有可能所有的人的日期都不在合法区间,这是必须特判0,否个会因为后面多输出空格而返回格式错误

    #include<stdio.h> 
    #include<string.h>
    struct birth{
    	char name[6];
    	int year;
    	int month;
    	int day;
    }older, younger, temp;
    
    int main(){
    	int n;
    	scanf("%d", &n);
    	int vaild = 0;
    	int max_year = 2015, max_month = 10, max_day = 7;
    	int min_year = 1813, min_month = 8, min_day = 5;
    	char older_name[6];
    	char younger_name[6];
    	for(int i = 0; i < n; i++){
    		scanf("%s%d/%d/%d", &temp.name, &temp.year, &temp.month, &temp.day);
    		if(temp.year == 1814){//在边界日期处 
    			if(temp.month == 9){
    				if(temp.day >= 6){
    					vaild++;
    					if( temp.day < max_day){
    						max_year = temp.year;
    						max_month = temp.month;
    						max_day = temp.day;
    						strcpy(older_name, temp.name);
    						
    					}
    					if(temp.day > min_day){
    						min_day = temp.day;
    						min_month = temp.month;
    						min_year = temp.year;
    						strcpy(younger_name, temp.name);
    					}
    				}				 
    			}else if(temp.month > 9){
    				vaild++;
    				if(temp.month < max_month){
    					max_year = temp.year;
    					max_month = temp.month;
    					max_day = temp.day;
    					strcpy(older_name, temp.name);
    				}
    				if(temp.month > min_month){
    					min_year = temp.year;
    					min_month = temp.month;
    					min_day = temp.day;
    					strcpy(younger_name, temp.name);
    				}
    			} 
    		}else if(temp.year > 1814 && temp.year < 2014){
    			vaild++;
    			if(temp.year == max_year){//同年 
    				if(temp.month == max_month){ //同年同月 
    					if(temp.day < max_day) { 
    						max_year = temp.year;
    						max_month = temp.month;
    						max_day = temp.day;
    						strcpy(older_name, temp.name);
    					}
    					if(temp.day > min_day){
    						min_year = temp.year;
    						min_month = temp.month;
    						min_day = temp.day;
    						strcpy(younger_name, temp.name);
    					}
    				}else{//同年不同月 
    					if(temp.month < max_month){
    						max_year = temp.year;
    						max_day = temp.day;
    						max_month = temp.month;
    						strcpy(older_name, temp.name);
    					}
    					if(temp.month > min_month){
    						min_year = temp.year;
    						min_day = temp.day;
    						min_month = temp.month;
    						strcpy(younger_name, temp.name);
    					}	
    				}	
    			}else{//不同年 
    				if(temp.year < max_year){
    					max_year = temp.year;
    					max_month = temp.month;
    					max_day = temp.day;
    					strcpy(older_name, temp.name);	
    				} 
    				if(temp.year > min_year){
    					min_year = temp.year;
    					min_month = temp.month;
    					min_day = temp.day;
    					strcpy(younger_name, temp.name);
    				}	
    			} 
    		}else if(temp.year == 2014){
    			if(temp.month == 9){
    				if(temp.day <= 6) {
    					vaild++;
    					if(temp.day < max_day){
    						max_month = temp.month;
    						max_year = temp.year;
    						max_day = temp.day;
    						strcpy(older_name, temp.name);
    					}
    					if(temp.day < min_day){
    						min_month = temp.month;
    						min_year = temp.year;
    						min_day = temp.day;
    						strcpy(younger_name, temp.name);
    					}
    				}
    			}else if(temp.month < 9){
    				vaild++;
    				if(temp.month < max_month){
    					max_year = temp.year;
    					max_month = temp.month;
    					max_day = temp.day;
    					strcpy(older_name, temp.name);
    				}
    				if(temp.month > min_month){
    					min_month = temp.month;
    					min_year = temp.year;
    					min_day = temp.day;
    					strcpy(younger_name, temp.name);
    				}
    			} 
    		}
    	}
    	if(vaild != 0) printf("%d %s %s\n", vaild, older_name, younger_name);
    	else printf("0");
    	return 0;
    }
    
  • 改过之后测试点4还是有错,我也不知道为啥QAQ,换代码

参考代码

#include<cstdio>
struct person{
	char name[10];
    int yy,mm,dd;
}oldest,youngest,left,right,temp;
//oldest和youngest存放最年长与最年轻的人,left与right存放合法日期的左右边界

bool LessEqu(person a, person b){//如果a的日期小于等于b,返回true
    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){//如果a的日期大于等于b,返回true
    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;
}

//youngest与lef为1814年9月6日,oldest与right为2014年9月6日
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;//num放合法日期的人数
    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;//更新oldest
            if(MoreEqu(temp, youngest)) youngest = temp;//更新youngest
        }
    }
    if(num == 0) printf("0\n");//所有人的日期都不合法,只输出0
    else printf("%d %s %s\n", num, oldest.name, youngest.name);
    return 0;
}#include<cstdio>
struct person{
	char name[10];
    int yy,mm,dd;
}oldest,youngest,left,right,temp;
//oldest和youngest存放最年长与最年轻的人,left与right存放合法日期的左右边界

bool LessEqu(person a, person b){//如果a的日期小于等于b,返回true
    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){//如果a的日期大于等于b,返回true
    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;
}

//youngest与lef为1814年9月6日,oldest与right为2014年9月6日
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;//num放合法日期的人数
    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;//更新oldest
            if(MoreEqu(temp, youngest)) youngest = temp;//更新youngest
        }
        if(num == 0) printf("%0\n");//所有人的日期都不合法,只输出0
        else printf("%d %s %s\n", num, oldest.name, youngest.name);
        return 0;
    }
}

参考代码(柳婼大神)

#include <iostream>
using namespace std;
int main() {
     int n, cnt = 0;
     cin >> n;
     string name, birth, maxname, minname, maxbirth = "1814/09/06", minbirth =
"2014/09/06";
     for (int i = 0; i < n; i++) {
         cin >> name >> birth;
         if (birth >= "1814/09/06" && birth <= "2014/09/06") {
             cnt++;
			if (birth >= maxbirth) {
				maxbirth = birth;
				maxname = name;
			}
             if (birth <= minbirth) {
                 minbirth = birth;
                 minname = name;
             }
         }
     }
    cout << cnt;
    if (cnt != 0) cout << " " << minname << " " << maxname;
    return 0;
}
posted @ 2021-07-28 23:07  shiff  阅读(52)  评论(0编辑  收藏  举报