![](https://i-blog.csdnimg.cn/blog_migrate/dcdbc533658f68e04c46727c97f9eca6.png)
模拟、字符串
本题题意是十分好理解的,只需要按题目要求模拟就可以AC;
本题数据量比较大,所以最好用字符串做。
总体思路如下:
输入字符串
遍历字符串得到答案
将答案转换为字符串
switch语句写出答案
#include <bits/stdc++.h>//我是懒人我用万能头
using namespace std;
#define MAXN 100000//宏定义一个大小,后面要用
int main()
{
typedef unsigned long long llu; //我是懒人++
string s;
getline(cin,s);//读取输入的所有数据
llu ans = 0;// 定义一个答案,不知道大不大,反正开的足够大
for(int i = 0 ; i < s.size() ; i++)
{
ans+=s[i]-48;//这里ans递加的是ASCII码,也用不着转换,直接减去48就当整型用了
}
char fin[MAXN];
sprintf(fin,"%llu",ans);//将ans整型转换成字符数组->字符串
string num = fin;
for(int i = 0 ; i < num.size() ; i++)//遍历判断最终答案,输出答案
{
switch(num[i])
{
case '0':
printf("zero ");continue;
case '1':
printf("one ");continue;
case '2':
printf("two ");continue;
case '3':
printf("three ");continue;
case '4':
printf("four ");continue;
case '5':
printf("five ");continue;
case '6'
printf("six ");continue;
case '7':
printf("seven ");continue;
case '8':
printf("eight ");continue;
case '9':
printf("nine ");continue;
}
}
return 0;
}
这里要注意用的是unsigned long long,占位符要用llu,所以在typedef的时候直接用llu了
几个常用的占位符
%d 整型 int
%ld 长整型 long int = long
%lld 长长整型 long long
%hd 短整型 short int
%u 无符号整型 unsigned int
%hu 无符号短整型 unsigned short int
%lu 无符号长整形 unsigned long int
%llu 无符号长长整型 unsigned long long