#include <stdio.h>
#include<string.h>
int main()
{
int n, i, year, month, day, count=0, temp, low=20140906, high=18140906;
char name[10], num[10], low_name[10]={"NULL"}, high_name[10]={"NULL"};
scanf("%d", &n);
for (i=1; i<=n; i++) {
scanf("%s %d/%d/%d", name, &year, &month, &day); //输入格式里的'/'要写上,否则会出错
//printf("%d %d %d\n", year, month, day); //测试代码
sprintf(num, "%04d%02d%02d",year, month, day); //月份和日期可能是一位数,故要写成%02d; 将整型输入存储为字符型
//printf("%s\n", num); //测试代码
sscanf(num, "%d", &temp); //错误写法 sscanf(temp, "%s", num); 将字符串数字转化为整型数字
//printf("%d\n", temp); //测试代码
if (temp>=18140906 && temp<=20140906) { //如果日期合理
count++;
if (temp>=high) {
high=temp;
strcpy(high_name,name); //存储年少者的名字
}
if (temp<=low) {
low=temp;
strcpy(low_name,name); //存储年长者的名字
}
}
}
if (count==0) printf("0\n"); //如果所有的都不合法,仅输出0。(真狗)
else printf("%d %s %s\n", count, low_name, high_name);
}
1、字符串与 整型之间的转换用sprintf、sscanf,注意书写格式。
或者调用atoi()函数将字符串数字转换成整型数,所需要头文件为stdlib.h。num=stoi(str);
atof():将字符串转换为双精度浮点型值。
atoi():将字符串转换为整型值。
atol():将字符串转换为长整型值。
strtod():将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字。
strtol():将字符串转换为长整值,并报告不能被转换的所有剩余数字。
strtoul():将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字。
2、输入2013/01/27时,写scanf时不要漏了'/'。
3、输入03,存储为整型时变成3,故输出时用%02d。
4、这题一个坑就是,如果都不合理则只输出0.