栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode 273. 整数转换英文表示[困难]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode 273. 整数转换英文表示[困难]

  1. 整数转换英文表示
    将非负整数 num 转换为其对应的英文表示。

示例 1:
输入:num = 123
输出:“One Hundred Twenty Three”

示例 2:
输入:num = 12345
输出:“Twelve Thousand Three Hundred Forty Five”

示例 3:
输入:num = 1234567
输出:“One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”

示例 4:
输入:num = 1234567891
输出:“One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”

提示:
0 <= num <= 231 - 1
通过次数22,116提交次数61,811

第一次在leetcode上面看到击败100%,虽然我知道我的代码水平很菜,(因为很菜所以想找个代码博客记录一下,本来想放在wordpress个人博客上,想想CSDN刚好没有写过东西,就放CSDN咯,但如果是大佬,就没啥好开心的)但是非常高兴,想要记录一下这个题目!!!

这个题目解题思路
1.十亿,百万,千,个是四个分组,除了刚开始的十亿,因为int范围,所以十亿只有1位,其他都是3位1组,每组定义4个变量,方便留空格,不然你要判断每位都不等于0,把3位整体再定义一个变量少写点哈哈哈
2.自定义百位int转string,输入int,return string;自定义十位,输入int个位和int十位,因为十位为1的时候个位需要和十位合并;自定义个位
3.留空格,在每个单词后面考虑要不要留空格,一个考虑后面是不是都等于0,一个考虑是不是有十位等于1的情况
4.特殊情况0——zero

下面附上代码,代码对齐有点乱,没好好对齐,我直接在leetcode上面写的,如果有强迫症可以在编译软件里对齐哈哈哈:

class Solution {
public:
    string gez(int num){
        string s="";
         if(num==1){
             s="One";
         }
         else if(num==2){
             s="Two";
         }
         else if(num==3){
             s="Three";
         }
         else if(num==4){
             s="Four";
         }
         else if(num==5)
         {
             s="Five";
         }
         else if(num==6){
             s="Six";
         }
         else if(num==7){
             s="Seven";
         }
         else if(num==8){
             s="Eight";
         }
         else if(num==9){
             s="Nine";
         }
         return s;
    }
        string tenz(int num,int num2){
                string s="";
         if(num==1){



         if(num2==0){
           s="Ten";
         }
         else if(num2==1){
             s="Eleven";
         }
         else if(num2==2){
             s="Twelve";
         }
         else if(num2==3){
             s="Thirteen";
         }
         else if(num2==4){
             s="Fourteen";
         }
         else if(num2==5)
         {
             s="Fifteen";
         }
         else if(num2==6){
             s="Sixteen";
         }
         else if(num2==7){
             s="Seventeen";
         }
         else if(num2==8){
             s="Eighteen";
         }
         else if(num2==9){
             s="Nineteen";
         }

         }





         else if(num==2){
             s="Twenty";
         }
         else if(num==3){
             s="Thirty";
         }
         else if(num==4){
             s="Forty";
         }
         else if(num==5)
         {
             s="Fifty";
         }
         else if(num==6){
             s="Sixty";
         }
         else if(num==7){
             s="Seventy";
         }
         else if(num==8){
             s="Eighty";
         }
         else if(num==9){
             s="Ninety";
         }
        //  if(num!=0){
        //      s.append(" ");
        //  }
         return s;
    }
    string hunz(int num){
        string s="";
         if(num==1){
             s="One Hundred";
         }
         else if(num==2){
             s="Two Hundred";
         }
         else if(num==3){
             s="Three Hundred";
         }
         else if(num==4){
             s="Four Hundred";
         }
         else if(num==5)
         {
             s="Five Hundred";
         }
         else if(num==6){
             s="Six Hundred";
         }
         else if(num==7){
             s="Seven Hundred";
         }
         else if(num==8){
             s="Eight Hundred";
         }
         else if(num==9){
             s="Nine Hundred";
         }
        //  if(num!=0){
        //      s.append(" ");
        //  }
         return s;
    }
    string numberToWords(int num) {
       string result="";
       int ge=num % 10;
       int ten=num/10 %10;
       int hun=num/100 %10;
       int th=num/1000 %1000;
       int thg=num/1000 %10;
       int thw=num/10000%10;
       int thsw=num/100000%10;
       int mi=num/1000000%1000;
       int mig=mi%10;
       int mis=mi/10%10;
       int mib=mi/100%10;
       int bi=num/1000000000%10;
       cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/314598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号