1028. 人口普查(20)

来源:互联网 发布:eval json 编辑:程序博客网 时间:2024/04/29 14:56

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

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

输入格式:

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

输出格式:

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

输入样例:
5John 2001/05/12Tom 1814/09/06Ann 2121/01/30James 1814/09/05Steve 1967/11/20
输出样例:
3 Tom John

这个也是不难的呢,我遇到了两个困难。

1,在比较年长和年幼的时候,搞反了,出生年月日越大,越年轻~~~~~~~~~~~~

2,自己定义的函数,不能乱命名,比如,我include进来了iostream,然后,定义了,less函数,这就出现了下面的问题,这个问题,在vc6.0里面没有出现,在提交的时候,出现的,编译错误。删去之后,好了。

a.cpp: In function 'int main()':a.cpp:52:62: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[6]' [-Wformat]a.cpp:53:6: error: reference to 'less' is ambiguousa.cpp:15:6: error: candidates are: bool less(people, people)In file included from /usr/include/c++/4.7/string:50:0,                 from /usr/include/c++/4.7/bits/locale_classes.h:42,                 from /usr/include/c++/4.7/bits/ios_base.h:43,                 from /usr/include/c++/4.7/ios:43,                 from /usr/include/c++/4.7/ostream:40,                 from /usr/include/c++/4.7/iostream:40,                 from a.cpp:3:/usr/include/c++/4.7/bits/stl_function.h:233:12: error:                 template<class _Tp> struct std::lessa.cpp:58:7: error: reference to 'less' is ambiguousa.cpp:15:6: error: candidates are: bool less(people, people)In file included from /usr/include/c++/4.7/string:50:0,                 from /usr/include/c++/4.7/bits/locale_classes.h:42,                 from /usr/include/c++/4.7/bits/ios_base.h:43,                 from /usr/include/c++/4.7/ios:43,                 from /usr/include/c++/4.7/ostream:40,                 from /usr/include/c++/4.7/iostream:40,                 from a.cpp:3:/usr/include/c++/4.7/bits/stl_function.h:233:12: error:                 template<class _Tp> struct std::less


不能乱命名啊!!!

AC代码如下

#include<stdio.h>struct people{char name[6];int year;int month;int day;};bool less(people x, people y){if(x.year != y.year)return x.year<=y.year;else if(x.month != y.month)return x.month<=y.month;elsereturn x.day<=y.day;}bool big(people x, people y){if(x.year != y.year)return x.year>=y.year;else if(x.month != y.month)return x.month>=y.month;elsereturn x.day>=y.day;}int main(void){int N,res=0;people lesser,biger,older,younger;biger.year=older.year=2014;biger.month=older.month=9;biger.day=older.day=6;lesser.year=younger.year=1814;lesser.month=younger.month=9;lesser.day=younger.day=6;scanf("%d",&N);people tmp;for(int i = 0; i<N; i++){scanf("%s %d/%d/%d",&tmp.name,&tmp.year,&tmp.month,&tmp.day);if(less(tmp,biger)&&big(tmp,lesser)){res++;if(big(tmp,younger))younger=tmp;if(less(tmp,older))older=tmp;}}if(res==0)printf("0");elseprintf("%d %s %s",res,older.name,younger.name);return 0;}



0 0
原创粉丝点击