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> ...
随机推荐
- Aqua Data Studio 查询结果中文乱码
菜单条里[文件]-[选项]会弹出个选项对话框 然后选[一般] 在右边的[外观]以下找到[网格结果字体] 将字体类型改成宋体即可了 watermark/2/text/aHR0cDovL2Jsb2cuY ...
- 内联函数 inline
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- Redis 突然报错 NOAUTH Authentication required
查找相关资料,说是添加了密码 只需要在redis的配置文件redis.conf中开启requirepass就可以了,比如我设置我的访问密码是mypassword requirepass mypassw ...
- SolrCloud Hello Word
Solr Cloud 设计出来的目的是使你的搜索服务具有更高的可用性,提高容错.容灾能力.下面我们在一台电脑上建立2个solr服务,作为一个solrCloud分片(shard),初步认识一下solrC ...
- nginx使用小记
中文wiki社区:http://wiki.codemongers.com/NginxChs 一 . nginx安装 1. 下载nginx : http://sysoev.ru/nginx/downlo ...
- myeclipse自动生成注释
myeclipse自动生成注释 在使用Eclipse编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author的属性值. 我们可以在Eclipse中进行设置自己希望显示 ...
- Shell脚本编程具体解释
第12章 Shell脚本编程 l Shell命令行的执行 l 编写.改动权限和运行Shell程序的步骤 l 在Shell程序中使用參数和变量 l 表达式比較.循环结构语句和条件结构语句 l ...
- 正态分布(Normal distribution)又名高斯分布(Gaussian distribution)
正态分布(Normal distribution)又名高斯分布(Gaussian distribution),是一个在数学.物理及project等领域都很重要的概率分布,在统计学的很多方面有着重大的影 ...
- Sqrt(x) 牛顿迭代法
为了实现sqrt(x),可以将问题看成是求解\(x^2-y=0\) ,即sqrt(y)=x: 牛顿法是求解方程的近似方法,给定初始点\((x0,f(x0))\),迭代公式为: #include < ...
- linux下的二进制文件的编辑和查看
linux下的二进制文件的编辑和查看 http://blog.csdn.net/wangxiaoqin00007/article/details/6618003 一.在Linux下查看二进制文件的软件 ...