Judge whether our citizen's ID card is legal _ nine degree OJ problem
Source: Internet
Author: User
The characteristics of our citizens ' identity card numbers are as follows:
1, the length is 18 bits;
2, the 1th to 17th place can only be a number;
3, the 18th digit may be the digit or the lowercase English letter x.
4. The 7th to 14th digit of the ID card number indicates the year, month and day information of the holder's birthday.
For example: 511002198808080111 or 51100219880808011x.
Please realize the function of the legitimacy judgment of the ID number. In addition to meeting the above requirements, the holder of the birthday of the year, month, day information to verify. Year is greater than 1900,
is less than equal to 2100. You need to consider the situation of leap year, size month. A leap year, which is divisible by 4 and cannot be divisible by 100 or divisible by 400, and February for leap years is 29 days,
February of the non-leap year is 28 days. Other circumstances of the validity of the check, candidates do not consider. function return value:
1. If the ID card number is valid, return 0;
2. If the length of the ID number is not valid, return 1;
3 If the ID number 1th to 17th contains non-numeric characters, return 2;
4 If the ID number 18th is neither the number nor the English lowercase letter x, return 3;
5 If the annual information of the ID number is illegal, return 4
6. If the monthly information of the ID number is illegal, return 5;
7 If the date information of the ID card is illegal, return 6 (Please note the situation of leap year);
#include <iostream>
#include <string>
using namespace Std;
int GetResult (string s)
{
int len=s.size ();
int y=0;
int m=0;
int d=0;
if (len==18)
{
for (int i=0;i<17;i++)
{
if (!isdigit (S[i]))
return 2;
}
if (!isdigit (s[17]) &&s[17]!= ' x ')
return 3;
for (int i=6;i<10;i++)
{
y=y*10+s[i]-' 0 ';
}
if (y>2100| | y<1900)
return 4;
for (int i=10;i<=11;i++)
{
m=m*10+s[i]-' 0 ';
}
if (m>12| | M<1)
return 5;
for (int i=12;i<=13;i++)
{
d=d*10+s[i]-' 0 ';
}
if (d<1| | D>31)
return 6;
if (d==31&& (m==4| | m==6| | m==9| | m==11))
return 6;
if (d==30&& (m==1| | m==3| | m==5| | m==7| | m==8| |m==10| | M==12))
return 6;
if (m==2)
{
if (d==29)
if (y%4!=0| | (y%400!=0&&y%100==0))
return 6;
}
}
Else
return 1;
return 0;
}
void Main ()
{
string S;
Getline (cin,s);
Cout<<getresult (s) <<endl;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.