杭电2629 Identity Card

  题目意思很简单,就是根据身份证号码来确定一个人的籍贯和生日,(然而我开始脑子抽了还以为还要根据奇数偶数判断男女233333)。

  然后我的暴力ac代码:

  

 1 #include <iostream>
 2 #include<math.h>
 3 #include <iomanip>
 4 #include<cstdio>
 5 #include<string>
 6 #include<map>
 7 #include<vector>
 8 #include<algorithm>
 9 #include<stdlib.h>
10 using namespace std;
11 
12 
13 
14 int main()
15 {
16     int n;
17     string id;
18     cin>>n;
19 
20     while(n--){
21         cin>>id;
22         if(id[0]=='3'&&id[1]=='3'){
23             cout<<"He/She is from Zhejiang,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
24         }
25 
26         else if(id[0]=='1'&&id[1]=='1'){
27              cout<<"He/She is from Beijing,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
28         }
29 
30         else if(id[0]=='7'&&id[1]=='1'){
31              cout<<"He/She is from Taiwan,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
32         }
33 
34         else if(id[0]=='8'&&id[1]=='1'){
35              cout<<"He/She is from Hong Kong,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
36         }
37 
38          else if(id[0]=='8'&&id[1]=='2'){
39              cout<<"He/She is from Macao,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
40         }
41 
42          else if(id[0]=='5'&&id[1]=='4'){
43              cout<<"He/She is from Tibet,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
44         }
45 
46          else if(id[0]=='2'&&id[1]=='1'){
47              cout<<"He/She is from Liaoning,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
48         }
49 
50          else {
51              cout<<"He/She is from Shanghai,and his/her birthday is on "<<id[10]<<id[11]<<","<<id[12]<<id[13]<<","<<id[6]<<id[7]<<id[8]<<id[9]<<" based on the table."<<endl;
52         }
53 
54     }
55     return 0;
56 }

  想表示下,我开始并没有顺利ac因为,我最后一行的输出多了一个空格,233333.

  然后就去看了下别人的代码,于是想补充下字符串的一些知识:

  一个截取字符串的函数substr(),觉得比较方便,该函数的用法见下面一个示例代码,一看便知:

#include<string>
#include<iostream>
using namespace std;
main()
{
string s("12345asdf");
string a=s.substr(0,4);       //获得字符串s中 从第0位开始的长度为4的字符串
cout<<a<<endl;
}

ac代码:

#include <iostream>
#include<string>

using namespace std;

int main(void)
{
    int n;
    string input_str,place;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>input_str;
        if(input_str.substr(0,2)=="33")
            place="Zhejiang";
        else if(input_str.substr(0,2)=="11")
            place="Beijing";
        else if(input_str.substr(0,2)=="71")
            place="Taiwan";
        else if(input_str.substr(0,2)=="81")
            place="Hong Kong";
        else if(input_str.substr(0,2)=="82")
            place="Macao";
        else if(input_str.substr(0,2)=="54")
            place="Tibet";
        else if(input_str.substr(0,2)=="21")
            place="Liaoning";
        else if(input_str.substr(0,2)=="31")
            place="Shanghai";

        cout <<"He/She is from"<<" "<< place
             << ",and his/her birthday is on"<<" "<<input_str.substr(10,2)<<","
             <<input_str.substr(12,2)<<","<<input_str.substr(6,4)<<" "
             <<"based on the table."<<endl;
    }

    return 0;
}

补充部分来自:http://blog.csdn.net/always2015/article/details/45391791

原文地址:https://www.cnblogs.com/William-xh/p/6820575.html