题解

CC  •  1年前


#include <stdio.h>

char s[25];
int year, month, date;
int coe[] = {0,7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char match[] = {1,0,'X'-'0',9,8,7,6,5,4,3,2}; //为了简洁都减去'0'
int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

void init()
{
	year = month = date = 0; //一定要初始化成0!!!
	for (int i = 7; i <= 10; i++)
	{
		year = year*10 + s[i]-'0'; //直接相加会很丑陋,所以写个循环
	}
	if (year % 4 == 0)
	{
		mon[2] = 29; //年份范围已经确定,直接判断是不是闰年即可
	}
	month = (s[11]-'0')*10 + s[12]-'0'; //直接获取月份
	date  = (s[13]-'0')*10 + s[14]-'0'; //直接获取日期
}


char decode()
{
	int sum = 0;
	for (int i = 1; i <= 17; i++)
	{
		sum += (s[i]-'0')*coe[i];
	}
	return match[sum%11]+'0';
}

void judge()
{
	if (year<1980 || year>2006)
	{
		printf("Y\n");
	}
	else if (month<1 || month>12)
	{
		printf("M\n");
	}
	else if (date<1 || date>mon[month])
	{
		printf("D\n");
	}
	else if (s[18] != decode())
	{
		s[18] = decode();
		printf("%s\n", s+1);
	}
	else
	{
		printf("T\n");
	}
}

int main() 
{
	int n;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%s", s+1); //方便读取下标
		init();
		judge();
		mon[2] = 28; //初始化成原始日期
	}
	return 0;
}

评论:

请先登录,才能进行评论