原创作品 转载请注明出处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;
}