c++ 字符串
/*
一個合法的身份證號碼由17位地區、日期編號和順序編號加1位校驗碼組成。校驗碼的計算規則如下:
首先對前17位數字加權求和,權重分配為:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后將計算的和對11取模得
到值Z;最后按照以下關系對應Z值與校驗碼M的值:
Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
現在給定一些身份證號碼,請你驗證校驗碼的有效性,并輸出有問題的號碼。
輸入描述:
輸入第一行給出正整數N(<= 100)是輸入的身份證號碼的個數。隨后N行,每行給出1個18位身份證號碼。
輸出描述:
按照輸入的順序每行輸出1個有問題的身份證號碼。這里并不檢驗前17位是否合理,只檢查前17位是否全為數字且最后1位校驗碼計算準確。如果所有號碼都正常,
則輸出“All passed”。
解題思路:分割字符串
輸入例子:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
*/
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int quan[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char mo[11]={'1','0','x','9','8','7','6','5','4','3','2'};
bool isdigit_str(const string str){
??? int count=0;
??? for(int i=0;i<str.length();i++){
??????? if(isdigit(str[i])) count++;
??? }
??? if(count==str.length())return true;
??? else return false;
}
char qu_mo(const string str){
??? int sum=0;
??? for(int i=0;i<str.length();i++){
??????? int temp = str[i]-'0';
??????? sum+=temp*quan[i];
??? }
??? sum=sum%11;
??? return mo[sum];
}
int main()
{
??? int N;
??? int count=0;
??? cin>>N;
??? vector<string> res;
??? for(int i=0;i<N;i++){
??????? string str;
??????? cin>>str;
??????? string str2=str.substr(0,17);
??????? if(isdigit_str(str2)){
??????????? if(qu_mo(str2)==str[17]) count++;
??????????? else res.push_back(str);
??????? }else res.push_back(str);
??? }
??? if(count==N)cout<<"All passed"<<endl;
??? else for(vector<string>::iterator it=res.begin();it!=res.end();it++){
??????????? cout<<*it<<endl;
??? }
?? ?return 0;
}
轉載于:https://www.cnblogs.com/qingtianBKY/p/6758905.html
總結
- 上一篇: windows下默认以管理员身份运行程序
- 下一篇: 设计模式——策略模式(C++实现)