杭电 ACM 1.2.7

来源:互联网 发布:excel数据误删怎么恢复 编辑:程序博客网 时间:2024/05/22 17:10

Identity Card

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2554 Accepted Submission(s): 823 
Problem Description
Do you own an ID card?You must have a identity card number in your family's Household Register. From the ID card you can get specific personal information of everyone. The number has 18 bits,the first 17 bits contain special specially meanings:the first 6 bits represent the region you come from,then comes the next 8 bits which stand for your birthday.What do other 4 bits represent?You can Baidu or Google it.
Here is the codes which represent the region you are in.

However,in your card,maybe only 33 appears,0000 is replaced by other numbers.
Here is Samuel's ID number 331004198910120036 can you tell where he is from?The first 2 numbers tell that he is from Zhengjiang Province,number 19891012 is his birthday date (yy/mm/dd).
 
Input
Input will contain 2 parts:
A number n in the first line,n here means there is n test cases. For each of the test cases,there is a string of the ID card number.
 
Output
Based on the table output where he is from and when is his birthday. The format you can refer to the Sample Output.
 
Sample Input
1330000198910120036
 
Sample Output
He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.
 
Author
Samuel
 
Source
灰色橙子 专场
 
Recommend
zty

这道题主要考察的就是对字符串函数的使用;strcpy,strcat,以及strncpy,strncat等的使用,需要注意的是使用strncpy时,复制过之后,函数并不自动给字符串后面添加字符串结束符,需要自己添加字符串结束符;还有需要注意的是分配数组大小的时候,要多分配一个空间,来存放结束符,这一点容易忽略而造成访问数组越界;

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Reg(char reg[], char regc[])
{
    int a = atoi(reg);
    switch(a)
        {
        case 33:
            strcpy(regc, "Zhejiang");
            break;
        case 11:
            strcpy(regc, "Beijing");
            break;
        case 71:
            strcpy(regc, "Taiwan");
            break;
        case 81:
            strcpy(regc, "Hong Kong");
            break;
        case 82:
            strcpy(regc, "Macao");
            break;
        case 54:
            strcpy(regc, "Tibet");
            break;
        case 21:
            strcpy(regc, "Liaoning");
            break;
        case 31:
            strcpy(regc, "Shanghai");
            break;
        }
}
void Bir(char bir[])
{
    char birc[11];
    char tmp[5];
    strcpy(birc, bir);
    strcpy(bir, "0");
    strncpy(tmp, birc + 4, 2);
    tmp[2] = '\0';
    strcpy(bir, tmp);
    bir[2] = ',';
    bir[3] = '\0';
    strncpy(tmp, bir + 6, 2);
    tmp[2] = '\0';
    strcat(bir, tmp);
    bir[5] = ',';
    bir[6] = '\0';
    strncat(bir, birc, 4);
    bir[10] = '\0';
}
int main(void)
{
    int n;
    int i;
    char arr[19];
    char reg[3];
    char bir[11];
    char regc[15];
    while(scanf("%d", &n) != EOF && n != 0)
    {
        getchar();
        for(i = 0;i < n;i++)
        {
            scanf("%s", arr);
            getchar();
        
            strncpy(reg, arr, 2);
            reg[2] = '\0';
            Reg(reg, regc);
            strncpy(bir, arr + 6, 8);
            bir[8] = '\0';
            Bir(bir);
            printf("He/She is from %s,and his/her birthday is on %s based on the table.\n", regc, bir);
        }
    }
    return 0;
}


原创粉丝点击