输入某人的 18 位身份证号,输出其出生日期。
输入格式
多实例测试。首先输入一个整数 n,表示测试实例的个数,然后是 n 行,每行是一个 18 位身份证号。
输出格式
对于输入的每个身份证号,输出一行,即其对应的出生日期,输出格式为:‘yyyy-mm-dd’。
提示:scanf()函数中,可用%md指定读取位数。
输出时每行末尾的多余空格,不影响答案正确性
样例输入
3
41010619941117865X
410104198202095479
410122197911218097
样例输出
1994-11-17
1982-02-09
1979-11-21
解题思路1:
scanf()函数中,可用%md指定读取位数,我们可以只读取生日,其余的不用理会
如果在 % 后有一个 " * " 号,这是一个附加说明符,表示读取时跳过它指定的列数。
例如:%*6d%4d%2d%2d 表示读入6位整数但不赋值给任何变量。然后再读入4位整数、2位整数,2位整数,最后结束读取
但是在计蒜客的编译器里面 却没有编译通过不知道是怎么回事,也不知道哪儿有问题,如果有大佬发现问题了 麻烦告知我一下。我是用substr函数做的 编译才通过。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(){
int year,month,day,n;
char str[10];
scanf("%d",&n);
for(int i = 1;i <= n;i++){
//scanf("%*6d%4d%2d%2d%*d",&y,&m,&a);
scanf("%*6d%4d%2d%2d%s",&year,&month,&day,str);
printf("%d-%02d-%02d\n",year,month,day);
}
return 0;
}
解题思路2:
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int m;
cin >> m;
do{
string str,year,month,day;
cin >> str;
year = str.substr(6,4);
month = str.substr(10,2);
day = str.substr(12,2);
cout << year << "-" << month << "-" << day << endl;
m = m - 1;
}while(m > 0);
return 0;
}