Java实现
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 }
C实现
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 }
#001#人人笔试题 输入数字转化为汉字,布布扣,bubuko.com
原文:http://www.cnblogs.com/DouBiBlog/p/3813503.html