PAT 1002. 写出这个数 (20)
c++实现
#include <iostream> using namespace std; char result[][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; void cal() { string n; int sum = 0; string temp; int tmp = 0; //cout<< "please enter the value of n: "; cin>>n; //cout<< "n = " << n << endl; for(char ch : n) { sum += int(ch-48); } //cout << "sum = " << sum << endl; temp = to_string(sum); //cout << "temp = " << temp << endl; for(int i = 0; i< temp.length(); i++) { //cout << temp[i] << endl; tmp = int(temp[i]-48); //cout << tmp << endl; cout << result[tmp]; //cout << i; if(i != temp.length()-1) cout << " "; } } int main() { cal(); return 0; }

浙公网安备 33010602011771号