SRM 583 Div Level Two:IDNumberVerification
题目来源: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的更多相关文章
- SRM 583 Div II Level One:SwappingDigits
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...
- SRM 587 Div II L3:ThreeColorabilityEasyy
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12699 这道题目是第一次在比赛的时候做出来的,开始还想用bru ...
- TC SRM 583 DIV 2
做了俩,rating涨了80.第二个题是关于身份证的模拟题,写的时间比较长,但是我认真检查了... 第三个题是最短路,今天写了写,写的很繁琐,写的很多错. #include <cstring&g ...
- Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1
据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- 竞赛图的得分序列 (SRM 717 div 1 250)
SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, ...
- TopCoder SRM 667 Div.2题解
概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...
- SRM 583 DIV1
A 裸最短路. class TravelOnMars { public: int minTimes(vector <int>, int, int); }; vector<int> ...
随机推荐
- if语句之有房么?有钱么?有能力么?
思路:1.如果有房,可以谈谈 2.如果没有房,问第二个条件有钱么,如果有,可以谈谈 3.如果没有房没有钱,则问第三个条件有能力么,如果有,可以谈谈 4.如果以上三个条件都没有,则拜拜 Console. ...
- linux 修改IP, DNS 命令
linux 修改IP, DNS 命令 http://www.cnblogs.com/fighter/archive/2010/03/04/1678007.html 修改DNS [root@localh ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- windows下apache+wsgi+web.py环境搭建
首先安装好wsgi模块并启用:1.下载地址:我本机是python2.7 http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi- ...
- 一天一个类--NIO 之Buffer
java.nio --- 定义了 Buffer 及其数据类型相关的子类.其中被 java.nio.channels 中的类用来进行 IO 操作的 ByteBuffer 的作用非常重要. java.n ...
- docker学习笔记14:Dockerfile 指令 ENV介绍
ENV指令用来在镜像构建过程中设置环境变量.我们来看一个Dockerfile的例子: #test FROM ubuntu MAINTAINER hello ENV MYDIR /mydir RUN m ...
- 在 vb中 "end","unload me","exit sub" 之间的区别
之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End” 跟 “Unload Me” 在敲程序 的时候经常敲到,“exit sub” 更是熟悉,下面,解析: End ...
- UVa 121 - Pipe Fitters
称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...
- 将EC2里的实例导出到RAW文件并进行修改
你可能有自己的instance在amazon云环境里面,或者是你想深度修改一下marketplace里面提供的那些系统又估计运行中的instance改动不方便 亚马逊作为云计算领域的大哥大,我不得不说 ...
- hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...