题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12610

这道题比较有意思,估计是中国人出的吧,以前都不知道身份证还这么麻烦,不过程序不难写。

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <string>
#include <cmath>
#include <limits>
#include <cstdlib> using namespace std;
class IDNumberVerification
{
public:
string verify(string id, vector <string> regionCodes);
}; string IDNumberVerification::verify(string id, vector<string> regionCodes)
{
string region;
string year;
string monday;
string month, day;
string seq;
string checksum;
string gender;
int nyear, nmonth, nday, nchecksum, nseq;
int sum;
int days_notleap[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days_leap[] = {-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int *days; region = id.substr(0, 6);
year = id.substr(6, 4);
monday = id.substr(10, 4);
month = id.substr(10, 2);
day = id.substr(12, 2);
seq = id.substr(14, 3);
checksum = id.substr(17, 1);
gender = id.substr(14, 3);
bool flag = false;
/* seq */
if (seq == "000") {
return "Invalid";
}
/* region */
for (int i = 0; i < regionCodes.size(); i++) {
if (region == regionCodes[i]) {
flag = true;
}
}
if (!flag) {
return "Invalid";
}
/* data */
bool leap = false;
nyear = atoi(year.c_str()); if (nyear < 1900 || nyear > 2011) {
return "Invalid";
} if ( (nyear % 4 == 0 && nyear % 100 != 0) ||
(nyear % 400 == 0) ) {
leap = true;
}
if ("0229" == monday && !leap) {
return "Invalid";
}
days = days_notleap;
if (leap) {
days = days_leap;
} nmonth = atoi(month.c_str());
nday = atoi(day.c_str());
if (nmonth > 12 || nmonth < 1) {
return "Invalid";
} if (nday > days[nmonth] || nday < 1) {
return "Invalid";
} /* checksum */
sum = 0;
for (int i = 0; i < 17; i++) {
sum = (sum * 2) + id[i] - '0';
}
sum = 2 * sum; nchecksum = checksum[0] - '0';
if (checksum[0] == 'X') {
nchecksum = 10;
}
int rchecksum = 12 - sum % 11;
if (rchecksum == 11) {
rchecksum = 0;
}
if ( nchecksum != rchecksum ) {
return "Invalid";
} /* gender */
nseq = atoi(seq.c_str());
if (nseq % 2 != 0) {
return "Male";
} else {
return "Female";
}
}

SRM 583 Div Level Two:IDNumberVerification的更多相关文章

  1. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  2. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  3. SRM 587 Div II L3:ThreeColorabilityEasyy

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12699 这道题目是第一次在比赛的时候做出来的,开始还想用bru ...

  4. TC SRM 583 DIV 2

    做了俩,rating涨了80.第二个题是关于身份证的模拟题,写的时间比较长,但是我认真检查了... 第三个题是最短路,今天写了写,写的很繁琐,写的很多错. #include <cstring&g ...

  5. Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1

    据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...

  6. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  7. 竞赛图的得分序列 (SRM 717 div 1 250)

    SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, ...

  8. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  9. SRM 583 DIV1

    A 裸最短路. class TravelOnMars { public: int minTimes(vector <int>, int, int); }; vector<int> ...

随机推荐

  1. Javascript 学习 笔记一

    1.操作 HTML 元素        如需从 JavaScript 訪问某个 HTML 元素,您能够使用 document.getElementById(id) 方法.        请使用 &qu ...

  2. docker 学习笔记21:docker连接网络的设置

    1.如果docker主机不需要通过代理连接外网 则docker的相关命令(如docker search)或docker容器与网络相关的操作都可以正常进行,不需要特殊设置. 2.当docker主机 是通 ...

  3. xcode生成的IOS安装文件的位置

    通过xcode生成可以在IOS系统下运行的文件的具体设置: 1.首先,需要有相应的程序,并且在mac下的xcode编译后,能够在模拟器中完美运行. 2.单击xcode,打开Xcode > Pre ...

  4. 【Leetcod】Unique Binary Search Trees II

    给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...

  5. 使用storyboard实现页面跳转,简单的数据传递

    由于最近才接触到IOS,苹果已经建议storyboard来搭建所有界面了,于是我 也追随时尚,直接开始使用storyboard.(不料在涉及到页面跳转的时候,遇到的问题是:点击后没有任何反应)众所周知 ...

  6. 设置Ubuntu 10.10版本的软件源

    设置Ubuntu 10.10版本的软件源 http://blog.csdn.net/xie1xiao1jun/article/details/49911189   网上有很多关于软件源信息的更新,每次 ...

  7. 为什么我的outlook只能收信不能发信,发送测试电子邮件消息: 无法发送此邮件。请在帐户属性中验证电子邮件

    链接地址:http://zhidao.baidu.com/link?url=aVIFo2aNLuHIZGZuEUataHkZp4XApHqyvbEK8ACHPhi3jwhGhM0GBAtm72AnsP ...

  8. 为学Linux,我看了这些书

    为学Linux,我看了这些书   去年开始,抱着学习的态度开始了我的Linux学习,到现在,差不多一年了,收获很多,不敢说精通Linux,但是,还是对得起"略懂"这两个字的.这一年 ...

  9. java--类继承和实现的接口中含有相同的方法

    首先,说一下,当某一个类实现了两个接口的时候,两个接口中存在两个相同的方法,在实现的类中只需实现一个方法的方法体. 当一个类继承一个类,并且实现一个或者多个接口的时候,其中,父类和父接口中存在相同的方 ...

  10. xp对opengl的支持问题

    我在项目中遇到的xp显示问题是因为xp对opengl的支持问题,是通过void QCoreApplication::setAttribute(Qt::ApplicationAttribute attr ...