華為面試題數字年夜小寫轉換。本站提示廣大學習愛好者:(華為面試題數字年夜小寫轉換)文章只能為提供參考,不一定能成為您想要的結果。以下是華為面試題數字年夜小寫轉換正文
這題由於限制了4位數,所以只斟酌了4位數的情形,吃點分享一個年夜小寫轉換的源碼,外面有不限位數的情形,其時調試的很苦楚,思惟差不多。
void iConvert(int digit)
{
char a[5][10] = {"千","百","十","","零"};
char b[11][10] = {"零","一","二","三","四","五","六","七","八","九","十"};
char result[50] = {'\0'};
int A[4] = {};
for(int i=3;i>=0;i--)
{
A[i] = digit % 10;
digit = int(digit/10);
}
printf("%d,%d,%d,%d\n",A[0],A[1],A[2],A[3]);
int foundZero = 0;
for(int i = 0 ;i<4;i++)
{
if(A[i]>0)
{
strcat(result,b[A[i]]);
strcat(result,a[i]);
}
if(A[i]==0 && foundZero == 0)
{
if(i!=3)//假如不是最初一名,則不追加零
{
strcat(result,a[4]);
foundZero = 1;
}
}
}
puts(result);
}
運轉成果: