【PAT 甲級】1005 Spell It Right (20分)(水)

1005 Spell It Right (20分)

題目

給一個 100 位以內的數,計算所有數字之和,並將結果的每一位用英文表示。

分析

直接模擬即可,注意特判 0。

#include <bits/stdc++.h>
using namespace std;
#define db(x) cout<<x<<endl

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e2 + 10;
const ll mod = 2147483648;

string dir[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main() {
	string n;
	cin >> n;
	int ans = 0;
	for(int i = 0; i < n.size(); i++) {
		ans += (n[i] - '0');
	}
	vector<string> v;
	if (ans == 0) v.push_back("zero");
	while (ans) {
		int t = ans % 10;
		ans /= 10;
		v.push_back(dir[t]);
	}
	for(int i = v.size() - 1; i >= 0; i--) {
		cout << v[i]; printf("%c", " \n"[i==0]);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章