PAT甲级第五题--1005 Spell It Right


题目


OJ平台

这是一道光从题目名字就清楚如何写的题目:Spell It Right,正确的拼写它。

题目翻译

单词积累

consecutive 连续的、连贯的

题目描述的翻译:

Given a non-negative integer N, 
your task is to compute the sum of all the digits of N,
and output every digit of the sum in English.
# 给定一个非负数N,你的任务是计算出N所有数字位的和,
# 并且用英语输出这个数字和的所有位的数据。

题目解析

超级简单–也就是输入后算出和,然后转字符串输出就行了。

Input函数

void Input(){
    ios::sync_with_stdio(false);
    cin>>ss;
}

init函数

void init(){
    to[0] = "zero";
    to[1] = "one";
    to[2] = "two";
    to[3] = "three";
    to[4] = "four";
    to[5] = "five";
    to[6] = "six";
    to[7] = "seven";
    to[8] = "eight";
    to[9] = "nine";
}

sum函数

ll sum(){
	ll sum = 0;
	for(int i=0;i<ss.size();i++){
		sum += (ss[i]-'0');
	}
	return sum;
}

print函数

void print(){
	ll sum_num = sum();
	string s = to_string(sum_num);
    int sz = s.size();
    for(int i=0;i<sz-1;i++){
        cout<<to[s[i]-'0']<<' ';
    }
    cout<<to[s[sz-1]-'0'];
}

整合函数输出结果

效率一般,还行,毕竟N最多为100位。

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string ss;
const char* to[10];
void Input(){
    ios::sync_with_stdio(false);
    cin>>ss;
}
void init(){
    to[0] = "zero";
    to[1] = "one";
    to[2] = "two";
    to[3] = "three";
    to[4] = "four";
    to[5] = "five";
    to[6] = "six";
    to[7] = "seven";
    to[8] = "eight";
    to[9] = "nine";
}
ll sum(){
	ll sum = 0;
	for(int i=0;i<ss.size();i++){
		sum += (ss[i]-'0');
	}
	return sum;
}
void print(){
	ll sum_num = sum();
	string s = to_string(sum_num);
    int sz = s.size();
    for(int i=0;i<sz-1;i++){
        cout<<to[s[i]-'0']<<' ';
    }
    cout<<to[s[sz-1]-'0'];
}
int main(){
    Input();
    init();
    print();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值