欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > C语言 | Leetcode C语言题解之第273题整数转换英文表示

C语言 | Leetcode C语言题解之第273题整数转换英文表示

2024/10/24 7:28:17 来源:https://blog.csdn.net/m0_59237910/article/details/140598563  浏览:    关键词:C语言 | Leetcode C语言题解之第273题整数转换英文表示

题目:

题解:

char* singles[] = {"", "One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
char* teens[] = {"Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
char* tys[] = {"","Ten ","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};char * toEnglish(char* res, int num){				//转英文函数int curNum = num;int hundred = curNum / 100;    					//百位以上if(hundred != 0){								//是否大于百位strcat(res, singles[hundred]);strcat(res, "Hundred ");}curNum %= 100;									//百位以下int ty = curNum / 10;							//十位~百位if(ty >= 2){									//是否大于20strcat(res, tys[ty]);curNum %= 10;}if(curNum >= 10){								//10~20strcat(res, teens[curNum-10]);}else if(curNum > 0 && curNum < 10){			//个位strcat(res, singles[curNum]);}return res;
}char * numberToWords(int num){if(num == 0)return "Zero";char* res = (char*)malloc(sizeof(char) * 200);	//储存结果res[0] = '\0';int curNum = num;int billion = curNum / 1000000000;				//十亿以上if(billion != 0){								//是否大于十亿toEnglish(res, billion);					strcat(res, "Billion ");}curNum %= 1000000000;							//十亿以下int million = curNum / 1000000;					//百万~十亿if(million != 0){								//是否大于百万toEnglish(res, million);strcat(res, "Million ");}curNum %= 1000000;								//百万以下int thousand = curNum / 1000;					//千~百万if(thousand != 0){								//是否大于千位toEnglish(res, thousand);strcat(res, "Thousand ");}curNum %= 1000;									//千位以下toEnglish(res, curNum);int len = strlen(res);res[len-1] = '\0';								//字符串结束return res;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com