PAT乙级题目对应知识点分类梳理
小思路:
- 按字符串的形式输入
- 求和sum
- 把sum转换为string形式再对应每一位输出对应中文拼音
小知识点:
-
如何将数字转为字符串?
to_string()
函数
string res = to_string(sum);
stringstream ss;
定义流
#include <sstream> int sum = 123; string res; stringstream ss; // 定义流ss ss << sum; // 流 << 数字 ss >> res; // 流 >> 字符串
sprintf()
用于将数字转化为字符串
char res[N]; int sum = 123; sprintf(res, "%d", sum);
-
如何将字符串转为数字?
stringstream ss;
定义流
# include<sstream> string str = "123"; int res; stringstream ss; // 定义流ss ss << str; // 流 << 数字 ss >> res; // 流 >> 字符串
sscanf()
用于将字符串转化为数字
char res[100] = "12345"; int sum; sscanf(res, "%d", &sum);
code:
#include<iostream>
#include<string>
#include<sstream>
#include<typeinfo>
using namespace std;
string words[11] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main()
{
string str;
cin >> str;
int sum = 0;
int len = str.length();
for (int i = 0; i < len; i ++ )
{
sum += str[i] - '0';
}
string res;
stringstream ss;
ss << sum;
ss >> res;
cout << words[res[0] - '0'];
for (int i = 1; i < res.size(); i ++ )
{
cout << " " << words[res[i] - '0'];
}
return 0;
}