试题1:
<span style="font-size:18px;">/*
第一题:拼音转数字
输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:
描述:
拼音 yi er san si wu liu qi ba jiu
阿拉伯数字 1 2 3 4 5 6 7 8 9
输入字符只包含小写字母,所有字符都可以正好匹配
运行时间限制:无限制
内存限制: 无限制
输入: 一行字符串,长度小于1000
输出: 一行字符(数字)串
样例输入: yiersansi
样例输出: 1234
*/
//难点:中间没有空格
//解决方法:输入字符只包含小写字母,所有字符都可以正好匹配
#include<iostream>
#include<string>
#include<vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::size_t;
using std::vector;
int transfer(size_t &i, string str)
{
switch (str[i])
{
case 'y':
return 1;
break;
case 'e':
return 2;
break;
case 's':
if (str[i + 1] == 'a')
{
i += 1;
return 3;
}
else
return 4;
break;
case 'w':
return 5;
break;
case 'l':
i += 1;
return 6;
break;
case 'q':
return 7;
break;
case 'b':
return 8;
break;
case 'j':
i += 1;
return 9;
break;
dafault:
cout << "您输入的字符有误!" << endl;
break;
}
}
int main()
{
string str;
size_t i = 0;
vector<int> vec;
cout << "请输入一段字符,该字符只包含小写字母:" << endl;
cin >> str;
int len = str.length();
//转化过程
while (i < len)
{
int n = transfer(i, str);
vec.push_back(n);
i += 2;
}
//输出
for (auto it = vec.cbe