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> ...
随机推荐
- C#_会员管理系统:开发五(用户注册)
创建一个新的用户注册窗体(VIPRegistration.cs): 用户注册窗体(VIPRegistration.cs)详细代码如下: using System; using System.Colle ...
- grub2的/etc/default/grub文件详解
# If you change this file, run 'update-grub' afterwards to update# /boot/grub/grub.cfg.GRUB_DEFAULT= ...
- JSONP跨域的原理解析[转]
转自 http://www.nowamagic.net/librarys/veda/detail/224 JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中, ...
- 如何借助Motion操控Linux监控摄像头
介绍 本文介绍如何使用motion来操控Linux下的摄像头. 安装 apt-get install motion 配置文件 输入命令后面的命令编辑配置文件, vim /etc/motion/moti ...
- 将时间显示为“刚刚”“n分钟/小时前”等
在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...
- 有什么很好的软件是用 Qt 编写的?
作者:尘中远链接:http://www.zhihu.com/question/19630324/answer/19365369来源:知乎 一些出名的例子如下:(wiki搬运) 3DSlicer, a ...
- 设计模式 ( 十七) 状态模式State(对象行为型)
设计模式 ( 十七) 状态模式State(对象行为型) 1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ...
- 深入探究VC —— 链接器link.exe(4)
在程序编译完成后,生成的文件是以.obj为扩展名的对象文件,link.exe是将这些对象文件与库链接起来以创建可执行文件或动态链接库文件的工具. link.exe的输入文件包括obj文件.lib文件. ...
- mailcore -- Mail port
以163为例的各个MailserverSSL协议port号和非SSL协议port号
- Vmware Briged方式使虚拟机上网
1.禁用掉在网络连接VMware Network Adapter VMnet1和VMware Network Adapter VMnet8 (在bridged这种方式下不需要这两个连接,如下图) 2. ...