杭电ACM 2629: Identity Card

原创作品 转载请注明出处http://blog.csdn.net/always2015/article/details/45391791

这里写图片描述

整个题目难点就是获取字符串的各位的值,很容易想到利用下标就可以做出来,但是我在这里用到了一个截取字符串的函数substr(),觉得比较方便,该函数的用法见下面一个示例代码,一看便知:

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

输出结果为:
1234

然而对于题目中各个城市对应的号码,在程序中可以用if语句,也可以用switch语句,我使用的是if语句,我的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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值