#include <stdio.h> #include <string.h> int main(void){ char num[105]; scanf("%s", num); int len = strlen(num); int sum = 0; for(int i = 0; i < len; i++){ sum += num[i] - '0'; } int bai = sum / 100; int shi = sum / 10 % 10; int ge = sum % 10; char shu[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; if(bai != 0){ printf("%s ", shu[bai]); printf("%s ", shu[shi]); printf("%s", shu[ge]); } else if(shi != 0){ printf("%s ", shu[shi]); printf("%s", shu[ge]); } else printf("%s", shu[ge]); return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in=new Scanner(System.in); String[] str={"ling","yi","er","san","si","wu","liu","qi","ba","qiu"}; char[] ch=in.next().toCharArray(); int sum=0; for(int i=0;i<ch.length;i++){ sum+=ch[i]-'0'; } String s=String.valueOf(sum); System.out.print(str[s.charAt(0)-'0']); for(int i=1;i<s.length();i++){ System.out.print(" "+str[s.charAt(i)-'0']); } } }
#include <stdio.h>
#include <string.h>
const char numberPinyinArray[10][4] = {
{'l', 'i', 'n', 'g'},
{'y', 'i'},
{'e', 'r'},
{'s', 'a', 'n'},
{'s', 'i'},
{'w', 'u'},
{'l', 'i', 'u'},
{'q', 'i'},
{'b', 'a'},
{'j', 'i', 'u'}
};
int main() {
// 读入字符串
char c[100];
scanf("%s", &c);
int result = 0;
// 加出有数字的结果
for (int i = 0; i < strlen(c); i++) {
if (c[i] == '\n')
break;
result = c[i] - '0' + result;
}
if (result == 0) {
printf("ling");
return 0;
}
// 定义结果数组,因n最大不会超过10的100次方,每位数字相加最大仅仅是三位数
char resultPingyinArray[3][4];
int index = 0;
while (result != 0) {
// 得到个位上的数字
int m = result % 10;
int i = index++;
// 将结果数组赋值
for (int p = 0; p < 4; p++) {
resultPingyinArray[i][p] = numberPinyinArray[m][p];
}
// 消去个位,将十位变为个位
result = result / 10;
}
// 打印结果数组的内容,倒序打印
for (int j = index - 1; j >= 0; j--) {
for (int k = 0; k < 4; ++k) {
// 屏蔽空白字符输出
if (resultPingyinArray[j][k] == '\0')
continue;
printf("%c", resultPingyinArray[j][k]);
}
// 最后一个拼音不需要空格
if (j != 0)
printf(" ");
}
return 0;
}
#include<stdio.h> #include<string.h> int main (){//the shorter,the better. int len,i,n;char s[101],*str[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; for(;~scanf("%s",s);n<100?:printf("%s ",str[n/100]),n<10?:printf("%s ",str[n/10%10]),printf("%s\n",str[n-n/10*10])) for(len=strlen(s),n=i=0;i<len;n+=s[i]%48,i++); }
//将各位数字累加保存到sum,由于从高位输出,现将sum各位数保存到r_sum中,然后后向输出;
//注意特殊情况,0。因此r_sum初始化为0,即可解决
#include <iostream> #include<string> #include<vector> using namespace std; int main() { string str; string NUM[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" }; while (cin >> str) { int sum = 0; for (auto c : str) sum += c - '0'; if (sum == 0) { cout << NUM[0] << endl; continue; } vector<int> res; while (sum) { res.push_back(sum % 10); sum /= 10; } for (int i = res.size() - 1; i >= 0; i--) { cout << NUM[res[i]]; if (i != 0) cout << " "; } cout << endl; } return 0; }
to_string 还是挺好用的🥰 #include<bits/stdc++.h> using namespace std; char num[15][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","shi"}; int main(){ string str; cin>>str; int sum=0,n=str.size(); for(int i=0;i<n;i++){ sum+=str[i]-'0'; } string s=to_string(sum); int m=s.size(); for(int i=0;i<m;i++){ cout<<num[s[i]-'0']; if(i<m-1) cout<<" "; } cout<<endl; return 0; }
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String num = sc.nextLine(); String[] arr = new String[]{"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; int sum = 0; for(int i = 0; i < num.length(); i++){ sum+=Integer.valueOf(num.charAt(i)); } StringBuilder sb = new StringBuilder(); String result = String.valueOf(sum); for(int i = 0; i < result.length();i++){ sb.append(arr[Integer.valueOf(result.charAt(i))]); sb.append(" "); } System.out.println(sb.toString().trim()); } }
#include <iostream> (720)#include <cstring> #include <string> using namespace std; int main() { char inputStr[101] = {'\0'}; //将数字与拼音对应 string numPinYin[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; //获取输入的字符 while(scanf("%s", inputStr) != EOF) { int resNum = 0; for (int i = (int)strlen(inputStr) - 1; i >= 0; --i) { //各个数字求和 resNum += inputStr[i] - '0'; } //然后把各个位数字和转为字符串 string numStr = to_string(resNum); //从左->右将数字翻译成拼音 for (int i = 0; i < numStr.size(); ++i) { if (i == numStr.size() - 1) { printf("%s\n", numPinYin[numStr[i] - '0'].c_str()); } else { printf("%s ", numPinYin[numStr[i] - '0'].c_str()); } } } return 0; } ———————————————— 版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://hestyle.blog.csdn.net/article/details/104741175
#include<bits/stdc++.h> using namespace std; string res[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; int main(){ string temp; cin>>temp; int sum=0; for(int i=0;i<temp.size();i++){ int num=temp[i]-'0'; sum+=num; // cout<<res[num]<<" "; } string str=to_string(sum); for(int i=0;i<str.size();i++){ if(i==str.size()-1){ cout<<res[str[i]-'0']; } else cout<<res[str[i]-'0']<<" "; } cout<<endl; }string字符串映射
#include <stdio.h> #include <stdlib.h> #include <string.h> char pinyin[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; int main() { char str[100]; scanf("%s", str);//一个测试用例 int sum = 0; int i = 0; while(str[i] != '\0') { sum += str[i] - '0'; i++; } int temp = 0; int rev[5] = {0}; i = 0; if(sum == 0) { printf("%s", pinyin[sum]); exit(0); } while(sum) { rev[i++] = sum % 10; sum /= 10; } while(i--) { if(!temp) { printf("%s", pinyin[rev[i]]); temp = 1; } else printf(" %s", pinyin[rev[i]]); } printf("\n"); return 0; }
#include<iostream> using namespace std; int main() { string n,py[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; int sum=0; cin>>n; for(auto it=n.begin();it<n.end();it++){ sum+=*it-'0'; } string ssum=to_string(sum); cout<<py[ssum[0]-'0']; for(auto it=ssum.begin()+1;it<ssum.end();it++){ cout<<" "<<py[*it-'0']; } return 0; }
print(' '.join(['ling','yi','er','san','si','wu','liu','qi','ba','jiu'][int(i)] for i in str(sum(map(int,input())))))
#include<iostream> #include<string> using namespace std; int main() { string s1; getline(cin,s1); if(s1=="0") cout<<"ling"; int len=s1.length(); int sum=0;//存放所有数位之和 for(int i=0;i<len;i++){ sum+=s1[i]-'0';//累加每一位 } int num=0,ans[10];//num表示sum的位数 while(sum!=0){ //将sum中的每一位存到数组中,sum的低位存到ans[]的低位 ans[num]=sum%10; num++; sum/=10; } char change[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; for(int i=num-1;i>=0;i--){ cout<<change[ans[i]]; if(i!=0) cout<<" "; else cout<<endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { string num; cin >> num; int sum = 0; //len for (int i = 0; i<num.length(); ++i) sum += (num[i]-'0');//一开始写的sum+=num[i];错了...... string pnyy[10] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" }; string strsum = to_string(sum); //len //多次调用.length()和调用一次.length()好像时间没差. for (int i = 0; i<strsum.length(); ++i) { cout << pnyy[strsum[i] - '0']; if (i != strsum.length() - 1)cout << " "; } return 0; }
思路: 反转数组,对于特殊化的0特殊考虑。 #include <iostream> #include <string> using namespace std; int main() { string s; string key[] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" }; while (cin >> s) { int count = 0; for (int i = 0; i < s.size(); i++) { count += s[i] - '0'; } int temp = 0; int countSize = 0; if (count == 0) { cout << key[0] << endl; continue; } while (count != 0) { temp = temp * 10 + count % 10; count = count / 10; countSize++; } while (temp != 0 || countSize !=0 ) { count = temp % 10; temp = temp / 10; countSize--; if (countSize != 0) { cout << key[count] << " "; } else { cout << key[count] << endl; } } } }
//可以说是非常简单的思想了 #include<iostream> #include<string> using namespace std; void read(int n) { if (n == 1) { cout << "yi"; } else if (n == 2) { cout << "er"; } else if (n == 3) { cout << "san"; } else if (n == 4) { cout << "si"; } else if (n == 5) { cout << "wu"; } else if (n == 6) { cout << "liu"; } else if (n == 7) { cout << "qi"; } else if (n ==8) { cout << "ba"; } else if (n == 9) { cout << "jiu"; } else if (n == 0) { cout << "ling"; } } int main() { string number; int sum=0; cin >> number; //求和 for (int i = 0; i < number.size(); i++) { sum = sum+number[i]-'0' ; } int bai = sum / 100; int shi = sum % 100 / 10; int ge = sum % 10; if (bai != 0) { read(bai); cout << " "; } if (shi != 0) { read(shi); cout << " "; } read(ge); return 0; }