首页 > 试题广场 >

写出这个数 (20)

[编程题]写出这个数 (20)
  • 热度指数:4768 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入描述:
每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100


输出描述:
在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
示例1

输入

1234567890987654321123456789

输出

yi san wu

python只需要两行代码:

dic = {"0": "ling", "1": "yi", "2": "er", "3": "san", "4": "si", "5": "wu", "6": "liu", "7": "qi", "8": "ba", "9": "jiu"}
print(" ".join(map(lambda c: dic[c], str(sum(map(int, input()))))))
发表于 2017-10-11 07:03:23 回复(1)
#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;
}

发表于 2018-01-24 14:00:32 回复(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']);
		}
	}
}

发表于 2017-03-01 22:37:35 回复(1)

#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;
}
发表于 2018-07-31 00:35:01 回复(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++);
}

发表于 2018-01-28 15:49:29 回复(1)
//将各位数字累加保存到sum,由于从高位输出,现将sum各位数保存到r_sum中,然后后向输出;
//注意特殊情况,0。因此r_sum初始化为0,即可解决
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string n;
    string dic[10]={"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    while (cin>>n)
    {
        int sum = 0;
        int r_sum[10] = {0};
        int tmp;
        int i=0;
        for (int i=0; i<n.length(); i++)
        {
            sum += n[i] - '0';
        }
        while (sum)
        {
            r_sum[i++] = sum % 10;
            sum = sum / 10;
        }        
        while (i>1)
        {
            tmp = r_sum[--i];
            cout<<dic[tmp]<<" ";
        }
        cout<<dic[r_sum[0]]<<endl;
    }
    system("pause");
    return 0;
}
发表于 2018-01-12 13:13:38 回复(0)
需要留意当和为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;
}

发表于 2017-04-19 00:05:23 回复(0)
啥头像
dict1 = {'0':'ling', '1':'yi', '2':'er', '3':'san', '4':'si', '5':'wu'\
         , '6':'liu', '7':'qi', '8':'ba', '9':'jiu'}
def int2pinyin(i):
    return dict1[i]

string = raw_input()
num = map(int, string)
num = str(sum(num))
num = map(int2pinyin, num)
print(' '.join(num))


发表于 2016-01-15 13:52:52 回复(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;
}

发表于 2022-11-05 21:21:10 回复(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());
    }        
}

发表于 2022-02-10 15:37:37 回复(0)
#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
发表于 2020-03-08 21:37:12 回复(0)
#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字符串映射
发表于 2020-03-07 12:23:08 回复(0)
#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;
}

编辑于 2020-03-04 15:53:23 回复(0)
1、数字位数过大,只能用字符串处理。
2、char转int,减去'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;
}


发表于 2020-02-01 09:33:38 回复(0)

one line

print(' '.join(['ling','yi','er','san','si','wu','liu','qi','ba','jiu'][int(i)] for i in str(sum(map(int,input())))))
编辑于 2020-01-09 22:17:39 回复(0)
#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;
}

发表于 2019-11-01 18:51:39 回复(0)
longlong也保存不了100位, 所以用string.
#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;
}

发表于 2019-04-02 17:25:22 回复(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;
            }
            
        }
    }
}

发表于 2018-08-13 21:44:35 回复(0)
numbers = input()
sum = 0
for number in numbers:
    sum = sum + int(number)
strsums = str(sum)
info = []
for strsum in strsums:
    if strsum == '1':
        info.append("yi")
    elif strsum == '2':
        info.append("er")
    elif strsum == '3':
        info.append("san")
    elif strsum == '4':
        info.append("si")
    elif strsum == '5':
        info.append("wu")
    elif strsum == '6':
        info.append("liu")
    elif strsum == '7':
        info.append("qi")
    elif strsum == '8':
        info.append("ba")
    elif strsum == '9':
        info.append("jiu")
    else:
        info.append("ling")
result = ''
i = 0
while i < len(info) - 1:
    result = result + info[i] + ' '
    i = i + 1
result = result + info[-1]
print(result)
发表于 2018-08-09 23:08:28 回复(0)
 //可以说是非常简单的思想了
#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;

}

编辑于 2018-02-06 18:55:45 回复(0)