题目来源: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. css中的定位

    上一篇博客,我大概介绍了下浮动的使用及行为.其实在整个文档布局中,定位也对我们整个的页面排版有非常好的帮助,当然前提是使用得当. 一.定位分类: a.静态定位  position:static;   ...

  2. java实现文件夹(包括其中的子文件夹、子文件)的复制——递归

    这是学校java课的一道实验题,题目如下:编程,根据指定的源和目标位置,完成指定文件或文件夹(包括其中的子文件夹.子文件)的复制. 以下是我的实现,使用了递归: package com.simon.m ...

  3. ASP.NET MVC 5 学习教程:添加视图

    原文 ASP.NET MVC 5 学习教程:添加视图 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控 ...

  4. 引用 xp系统引导修复(转载)

    引用 3592wangxiaoxi 的 xp系统引导修复(转载) 原文来自百度知道a12424106关于“急需xp系统引导方面的知识!”的回复. XP系统的引导过程 如果想学习排除计算机系统故障,首先 ...

  5. larbin是一种开源的网络爬虫/网络蜘

    larbin是一种开源的网络爬虫/网络蜘蛛,由法国的年轻人 Sébastien Ailleret独立开发.larbin目的是能够跟踪页面的url进行扩展的抓取,最后为搜索引擎提供广泛的数据来源.Lar ...

  6. c语言中scanf()、printf()函数

    函数调用scanf(“%d”,  &weight) 包含两个参数:“%d” 和&weight.C用逗号来隔开函数调用中的多个参数: 但是printf()和scanf()函数比较特殊,其 ...

  7. stm32之watchdog

    在嵌入式系统中,由于MCU的工作常常受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个系统陷入停滞状态,发送不可预料的后果,所以出 ...

  8. 第一次PS练习

    嘿嘿,自己第一次的PS,虽然把在大神眼里是小KS,但是了,对我来说值得劲纪念.加油,我会努力的.

  9. parquet 合并元数据

    合并元数据:两个数据集,有着一部分相同的列,将他们合并成一个数据集时merge的过程. 合并的规则:相同的列,在新的数据集中,是通用的列, 各自不同的列,也作为新的数据集的列. Spark将数据写入到 ...

  10. 通过jQuery的attr修改onclick

    var js = "alert('B:' + this.id); return false;"; // creates a function from the "js&q ...