PAT乙级 1002 写出这个数 (20分)
核心思想:string类与int类的变换
另外,博主用了STL库中的to_string函数,使程序大大简化,不过要注意的是,
如果编译器版本较低,编译时可能报错:“to_string is not declared in this scope”,博主就遇到了这种情况,直接去codeblocks官网下载了最新版本的,问题解决。
#include<iostream>
#include<string>
using namespace std;
int main()
{
int sum=0;
string str="",s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
cin>>str;
for(int n=0;n<str.length();n++)
sum=sum+str[n]-'0';
string strnum=to_string(sum);
for(int n=0;n<strnum.length();n++)
{
cout<<s[strnum[n]-'0'];
if(n<strnum.length()-1)
cout<<' ';
}
}
本文详细解析PAT乙级1002题“写出这个数”的解题思路,利用C++中的string类与int类转换,通过to_string函数简化程序。博主分享了解决编译器版本导致的to_string未声明错误的方法,即升级CodeBlocks版本。
691

被折叠的 条评论
为什么被折叠?



