1083: 字符串数字置换
时间限制: 1 Sec 内存限制: 128 MB提交: 0 解决: 0
[ 提交][ 状态][ 讨论版]
题目描述
从键盘接收用户输入的字符串, 对用户输入的每个字符串的处理是:将字符串内的每一个十进制数字字符置换成下列表格中右边所对应的一个字符串(所有其他字符不变),然后将转换的结果显示在屏幕上;并分别计算每个数字的置换次数。
十进制数字字符
|
置换成
|
0
|
(Zero)
|
1
|
(One)
|
2
|
(Two)
|
3
|
(Three)
|
4
|
(Four)
|
5
|
(Five)
|
6
|
(Six)
|
7
|
(Seven)
|
8
|
(Eight)
|
9
|
(Nine)
|
例如,若用户输入的字符串为
Page112-Line3,
则程序5的输出是:
Page(One) (One) (Two)-Line(Three)
数字0到9的置换次数分别是 0 2 1 1 0 0 0 0 0 0
输入
输入一行字符串,其中可包含字母、数字或其他符号
输出
第一行为将字符串中的数字转换为表格中的内容后输出
第二行为数字0~9被转换的次数
样例输入
Page112-Line3
样例输出
Page(One) (One) (Two)-Line(Three)0 2 1 1 0 0 0 0 0 0
很简单的题目~
昨天没传是因为有个字符串匹配问题没写出来=。=今天刚好碰到两个简单的题目就做了两道。
啊,不知道昨天那道题目啥时候能搞出来=。=
#include <iostream> #include <string> using namespace std; int main(){string a;cin >> a;int count[10];memset(count, 0, sizeof(count));int len = a.length();int i;for (i = 0; i < len; i++){if (a[i]<'0' || a[i]>'9')cout << a[i];else {switch (a[i]){case '0':cout << "(Zero)"; count[0]++; break;case '1':cout << "(One)"; count[1]++; break;case '2':cout << "(Two)"; count[2]++; break;case '3':cout << "(Three)"; count[3]++; break;case '4':cout << "(Four)"; count[4]++; break;case '5':cout << "(Five)"; count[5]++; break;case '6':cout << "(Six)"; count[6]++; break;case '7':cout << "(Seven)"; count[7]++; break;case '8':cout << "(Eight)"; count[8]++; break;case '9':cout << "(Nine)"; count[9]++; break;}}}cout << endl;for (i = 0; i < 10; i++)cout << count[i] << " ";system("pause");return 0; }