PAT (Advanced) 1005. Spell It Right (20)
2017-09-09 20:14
381 查看
原题:1005. Spell It Right (20)
思路如下:
1.将英文字符串与数字对应做好表格
2.求出sum, 再分解sum, 最后根据表格输出
3.注意 0 的特殊处理
c++代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char a[110];
char table[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};//打表
int main()
{
while(scanf("%s", a) != EOF)
{
int sum = 0;
int len = strlen(a);
for(int i = 0; i < len; i++)
{
sum += a[i] - '0';
}
int temp[10];
int cnt = 0;
if(sum == 0)// 0 的特殊处理
{
temp[0] = 0;
cnt = 1;
}
while(sum > 0) // sum 分解
{
temp[cnt++] = sum % 10;
sum /= 10;
}
for(int i = cnt - 1; i >= 0; i--)
{
if(i == cnt - 1)
printf("%s", table[temp[i]]);
else
printf(" %s", table[temp[i]]);
}
printf("\n");
}
return 0;
}
思路如下:
1.将英文字符串与数字对应做好表格
2.求出sum, 再分解sum, 最后根据表格输出
3.注意 0 的特殊处理
c++代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char a[110];
char table[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};//打表
int main()
{
while(scanf("%s", a) != EOF)
{
int sum = 0;
int len = strlen(a);
for(int i = 0; i < len; i++)
{
sum += a[i] - '0';
}
int temp[10];
int cnt = 0;
if(sum == 0)// 0 的特殊处理
{
temp[0] = 0;
cnt = 1;
}
while(sum > 0) // sum 分解
{
temp[cnt++] = sum % 10;
sum /= 10;
}
for(int i = cnt - 1; i >= 0; i--)
{
if(i == cnt - 1)
printf("%s", table[temp[i]]);
else
printf(" %s", table[temp[i]]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- PAT (Advanced Level) 1005. Spell It Right (20) string转int,哈希
- 【PAT (Advanced Level)】1005. Spell It Right (20)
- PAT (Advanced Level) Practise 1005. Spell It Right (20)
- PAT (Advanced Level) 1005. Spell It Right (20) 解题报告
- PAT (Advanced Level) 1005. Spell It Right (20)
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- 【c++】PAT (Advanced Level) 1005. Spell It Right (20)
- 1005. Spell It Right (20)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise - 1005. Spell It Right (20)
- PAT-PAT (Advanced Level) Practise 1005. Spell It Right (20) (简单模拟题)【一星级】
- PAT (Advanced Level) Practise 1005 Spell It Right (20)
- 【PAT Advanced Level】1005. Spell It Right (20)
- PAT Advanced 1005. Spell It Right (20) (C语言实现)
- 【PAT】【Advanced Level】1005. Spell It Right (20)
- PAT (Advanced Level) 1005. Spell It Right (20)
- PAT (Advanced Level) Practise 1005. Spell It Right (20)
- PAT(Advanced Level)1005. Spell It Right (20) 栈 int转string
- PAT_A 1005. Spell It Right (20)
- [PAT甲级]1005. Spell It Right (20)(求数字各个位上的和,英文输出)
- PAT 1005 Spell It Right (20)(字符转换)