pta写出这个数


题目

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10 ^100 。
输出格式
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。
输入样例

1234567890987654321123456789

输出样例

yi san wu

code(C++):

//万能头文件
//#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int main(int argc, char** argv)
{
    string n;
    //输入
    cin>>n;
    //length()计算数字长度
    int m = n.length();
    //cout<<m<<endl;
    int sum = 0;
    string k = "";//存放获取到的单个字符
    int a[m];//存放每位数字
    
//计算sum
    for(int i=0; i<m; i++)
    {
        //cout<<n[i]<<endl;
        k = n[i];
        //string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。否则报错。
        a[i] = atoi(k.c_str());
        sum += a[i];
    }
    //cout<<sum<<endl;

    int d = 0;
    //和是d位数
    for(int i=1; sum/i > 0; i*=10)
    {
        d += 1;
    }
    //cout<<"d="<<d<<endl;

	//和的每位数字,测试12345,d=2,re=1,re=5
    int re;
    for(int i=1; i<=d; i++)
    {
        int c=pow(10,d-i);
        re = (sum/c)%10;//(15/10)%10=1%10=1 //(15/1)%10=5
        //if判断,也可以用switch语句
        if(re == 0){
            cout<<"ling";
        }
        if(re == 1){
            cout<<"yi";
        }
        if(re == 2)
        {
            cout<<"er";
        }
        if(re == 3)
        {
            cout<<"san";
        }
        if(re == 4)
        {
            cout<<"si";
        }
        if(re == 5)
        {
            cout<<"wu";
        }
        if(re == 6)
        {
            cout<<"liu";
        }
        if(re == 7)
        {
            cout<<"qi";
        }
        if(re == 8)
        {
            cout<<"ba";
        }
        if(re == 9)
        {
            cout<<"jiu";
        }
        //cout<<"i="<<i<<endl;
        //cout<<"re="<<re<<endl;
        if(i<d)
        {
            cout<<" ";
        }
    }

	return 0;
}

总结

基本类型转换

https://www.cnblogs.com/xiaodangxiansheng/p/12697278.html

1、整型和浮点型之间的转换

(1)普通的强制转换

    double d_Temp = 1000;
    int i_Temp;
    i_Temp = (int)d_Temp;
    cout<<i_Temp<<endl;

(2)使用标准的强制转换

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast
    double d_Temp;
    int i_Temp = 323;
    d_Temp = static_cast<double>(i_Temp);

2、字符串和数字之间的转换

(1)用sprintf_s函数将数字转换成字符串

    int i_temp = 2020;
    std::string s_temp;
    char c_temp[20];
    sprintf_s(c_temp, "%d", i_temp);
    s_temp = c_temp;
    std::cout << s_temp << std::endl;

(2)用sscanf函数将字符串转换成数字

   double i_temp;
   char c_temp[20] = "15.234";
   sscanf_s(c_temp, "%lf", &i_temp);
   std::cout << i_temp << std::endl;

(3)atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, long, long long 型

   std::string s_temp;
   char c_temp[20] = "1234";
   int i_temp = atoi(c_temp);
   s_temp = c_temp;
   std::string s_temp;
   char c_temp[20] = "1234.1234";
   double i_temp = atof(c_temp);
   s_temp = c_temp;

(4)strtol, strtod, strtof, strtoll,strtold 函数将字符串转换成int,double,float, long long,long double 型

   std::string s_temp;
   char c_temp[20] = "4.1234";
   double a = strtod(c_temp, nullptr);  //后面的参数是如果遇到字符串则是指向字符串的引用

(5)用to_string把数字转化成字符串

   double d_temp = 1.567;
   std::string s_temp = to_string(d_temp);

3、字符串和字符之间的转换

(1)用c_str从字符串转换char*

   string s_temp = "this is string";
   const char* c_temp;
   c_temp = s_temp.c_str();

(2)char数组转换成字符串

   string s_temp;
   char c_temp[20];
   c_temp[0] = 'H';
   c_temp[1] = 'L';
   c_temp[2] = 'L';
   c_temp[3] = 'O';
   c_temp[4] = '\0';
   s_temp = c_temp;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值