SRM 583 Div Level Two:IDNumberVerification
2013-06-23 16:40
375 查看
题目来源: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 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 579 Div II Level Two: UndoHistory
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 219 Div II Level Two: HealthFood
- SRM 578 Div II Level Three: WolfInZooDivTwo,Dynamic Programming,求教!
- SRM 514 MagicalGirlLevelThreeDivTwo
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
- SRM 233 Div II Level Two: PipeCuts
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 299 Div II Level Two: Projections
- SRM 583 Div II Level One:SwappingDigits
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法