首页 > 其他 > 详细

#001#人人笔试题 输入数字转化为汉字

时间:2014-07-07 18:22:22      阅读:362      评论:0      收藏:0      [点我收藏+]

Java实现

 

bubuko.com,布布扣
 1 package test001;
 2 import java.util.Scanner;
 3 
 4 //输入530014 输出五十三万零一十四
 5 //int 型数据最大值为 21 4748 3647   二十一亿  四千七百四十八万  三千六百四十七 
 6 //int 型数据最小值为-2147483647
 7 public class file1 {
 8     static String[] numArray={"零","一","二","三","四","五","六","七","八","九"};
 9     static String[] numName={"十","亿","千","百","十","万","千","百","十",""};
10     public static void main(String[] args){
11         int a=0;
12         Scanner sc=new Scanner(System.in);
13         while(a!=-1){
14             System.out.println("输入:");
15             a=sc.nextInt();
16             GetString(a);
17         }
18     }
19     protected static String GetString(int Num){
20         StringBuilder sb=new StringBuilder();    //存储结果
21         Integer iNum=new Integer(Num);           //int装箱
22         String source=iNum.toString();           //装箱后的String
23         int iLength=source.length();             //装箱后的String的长度
24         for(int i=0;i<iLength;i++){
25             if(Character.getNumericValue(source.charAt(i))!=0){
26             sb.append(numArray[Character.getNumericValue(source.charAt(i))]);
27             sb.append(numName[10-iLength+i]);
28             }
29             else{
30                 if(sb.toString().endsWith("零")==true)
31                     continue;
32                 else
33                     sb.append("零");
34             }
35         }
36         System.out.println(sb.toString());
37         return sb.toString();
38     }
39 }
View Code

 

 

 

  

C实现

 

bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 char* NUMArray[10]={"","","","","","","","","",""};
 5 char* NUMName[10]={"","亿","","","","","","",""," "};
 6 //输入:530014 输出:五十三万零十四
 7 //int型数据4个字节  最大值2147483647
 8 int GetLength(int source)
 9 {
10     int length=0;
11     while(source!=0)
12     {
13         source=source/10;
14         length++;
15     }
16     return length;
17 }
18 
19 void GetValue(int* p,int source,int length)
20 {
21     for(int i=length-1;source!=0;i--)
22     {
23         p[i]=source%10;
24         source=source/10;
25     }
26 }
27 void main()
28 {    
29     int target=0;
30     while(target!=-1)
31     {
32         int Length=0;
33         printf("输入:");
34         scanf("%d",&target);
35         Length=GetLength(target);                    //得到长度
36         int* p=(int *)malloc(sizeof(int)*Length);    //分开得到数组
37         char** cp=(char **)malloc(sizeof(char *)*Length*2);
38         GetValue(p,target,Length);
39         int sum=0;
40         for(int i=0;i<Length;i++)
41         {
42             if(p[i]!=0)
43             {
44                 cp[sum]=NUMArray[p[i]];
45                 sum++;
46                 cp[sum]=NUMName[10-Length+i];
47                 sum++;
48             }
49             else
50             {
51                 if(strcmp(cp[sum-1],"")==0)
52                 {
53                     continue;
54                 }
55                 else
56                 {
57                     cp[sum]="";
58                     sum++;
59                 }
60             }
61         }
62         for(int i=0;i<sum;i++)
63         {
64             printf("%s",cp[i]);
65         }
66         printf("\n");
67         free(p);
68         free(cp);
69     }
70 }
View Code

 

 

 

#001#人人笔试题 输入数字转化为汉字,布布扣,bubuko.com

#001#人人笔试题 输入数字转化为汉字

原文:http://www.cnblogs.com/DouBiBlog/p/3813503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!