以下是本题中可能用到的地区的代码。
在身份证号码中,可能只有33出现,0000是由其他数字取代。
这是塞缪尔的身份证号码331004198910120036你能告诉我他来自哪里吗? 最初的两位表示他来自浙江省,19891012表示是他的生日日期(年/月/日)。
Input
输入包含两部分:
第一行为一个整数n,代表有n组数据
接下来为测试数据,每一行为一个代表身份证号码的字符串。
Output
根据题目描述,输出他来自哪里和他的出生日期。具体格式可以参照样例输出。
Sample Input
1
330000198910120036
Sample Output
He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.*/
方法一:
#include<stdio.h>
#include<string.h>
void result(char *a, int from, int to);
char provin[100][20] = {[33] = "Zhejiang", [11] = "Beijing", [71] = "Taiwan",
[81] = "Hong Kong", [82] = "Macao", [54] = "Tibet",
[21] = "Liaoning", [31] = "Shanghai"};//用二位数组存省份
int main()
{
int n, i;
char a[1000];
int sum;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%s", a);
printf("He/She is from ");
result(a, 0, 1); //provin
printf(",and his/her birthday is on ");
result(a, 10, 11); //month
putchar(',');
result(a, 12, 13); //date
putchar(',');
result(a, 6, 9); //year
printf(" based on the table.\n");
}
return 0;
}
void result(char *a, int from, int to)
{
int i, sum;
if (from == 0)
{
sum = (a[0] - '0') * 10 + (a[1] - '0');//-‘0’是将字符变为数字
printf("%s", provin[sum]);
return;
}
for (i = from; i <= to; i++)
{
printf("%d", a[i] - '0');//-‘0’是将字符变为数字
}
}
方法二:
#include<stdio.h>
#include<string.h>
void SubString(char *dest, char *origin, int from, int to) {
int i = 0;
for (i = from; i <= to; i++) {
dest[i - from] = origin[i];//将原数组指定位数存在新数组中
}
dest[to - from + 1] = '\0';//因为存入新数组是一个字符一个字符存储,所以末尾家‘\0’
}
int main()
{
char idno[20], year[5], month[3], day[3], pro[20];//建立年,月,日,省的新数组
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", idno);
SubString(pro, idno, 0, 1);
SubString(year, idno, 6, 9);
SubString(month, idno, 10, 11);
SubString(day, idno, 12, 13);
if (strcmp(pro, "33") == 0)//省去二维数组的麻烦,利用strcmp函数比较开头两个字符,写出省份
strcpy(pro, "Zhejiang");
else if (strcmp(pro, "11") == 0)
strcpy(pro, "Beijing");
else if (strcmp(pro, "71") == 0)
strcpy(pro, "Taiwan");
else if (strcmp(pro, "81") == 0)
strcpy(pro, "Hong Kong");
else if (strcmp(pro, "82") == 0)
strcpy(pro, "Macao");
else if (strcmp(pro, "54") == 0)
strcpy(pro, "Tibet");
else if (strcmp(pro, "21") == 0)
strcpy(pro, "Liaoning");
else if (strcmp(pro, "31") == 0)
strcpy(pro, "Shanghai");
printf("He/She is from %s,and his/her birthday is on %s,%s,%s based on the table.\n", pro, month, day, year);
}
return 0;
}