1. 程式人生 > >杭電2629 Identity Card

杭電2629 Identity Card

input 比較 題目 開始 ret targe str 部分 還要

  題目意思很簡單,就是根據身份證號碼來確定一個人的籍貫和生日,(然而我開始腦子抽了還以為還要根據奇數偶數判斷男女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

杭電2629 Identity Card