PTA乙级1002【写出这个数】代码

我写的代码:

#include <iostream>
#include <math.h>
using namespace std;

int main(){
    char a;
    int sum=0;
    string b[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    while(a=cin.get()){
        if(a==EOF) break;
        else if(0<=a-'0' && a-'0'<=9)
        {
            sum += (int)(a-'0');
        }
    }
    int temp = sum;
    int count_ = 0;
    while(temp){
        temp = temp/10;
        count_++;
    }
    temp = sum;
    for(int i=count_-1;i>0;i--){
        cout<<b[temp/(int)pow(10,i)]<<" ";
        temp = temp%(int)pow(10,i);
    }
    cout<<b[temp]<<endl;
    return 0;
}

测评:

 

在网上查到: https://blog.csdn.net/mlm5678/article/details/82357012

代码:

#include <iostream>
#include<algorithm>
#include<string.h>
using namespace std;
 
int main() 
{
	int sum=0;int count=0;int temp=0;
	char ch;string s;//ch作为临时变量保存输入的每一个字符,s保存sum的每一个数字                      
	 string b[10] =  {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
	while((ch=getchar())!='\n')	//如果一直输入字符,不按回车的话 
    sum += (ch - '0');
    
  count = 0;
  temp = sum;
  while (temp)            //判断和一共有多少位
  {
    temp /= 10;
    count++;  
  }
 for (int i = count-1; i >=0; i--)
  { s[i] = sum % 10+'0'; //加上0所对应的Ascll码转化为字符表示的数字
    sum /= 10;
  }
 for (int i = 0; s[i] != 0; i++){
        if (i > 0) 
            printf(" ");
        cout<<b[s[i]-'0'];
    }
    return 0;
 
}

测评:

 

不得不承认自己的代码比别人菜点.... 继续加油

优质代码是完成了 string -> int -> string的过程

而我是string -> int -> 计算 的过程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值