Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 制作人类可读的整数表示_Algorithm_Numbers - Fatal编程技术网

Algorithm 制作人类可读的整数表示

Algorithm 制作人类可读的整数表示,algorithm,numbers,Algorithm,Numbers,对于喜欢这种东西的人来说,这里有一个编码问题。让我们看一下函数的实现(当然,用您选择的语言),该函数返回指定整数的可读字符串表示形式。例如: humanReadable(1)返回“一”。 humanReadable(53)返回“五十三”。 humanReadable(723603)返回“七十二万三千六百零三”。 humanReadable(1456376562)返回“十亿、四亿五千六百万、三十七万六千、五百六十二”。 特别聪明/优雅的解决方案可获得额外积分 这似乎是一个毫无意义的练习,但这种

对于喜欢这种东西的人来说,这里有一个编码问题。让我们看一下函数的实现(当然,用您选择的语言),该函数返回指定整数的可读字符串表示形式。例如:

  • humanReadable(1)返回“一”。
  • humanReadable(53)返回“五十三”。
  • humanReadable(723603)返回“七十二万三千六百零三”。
  • humanReadable(1456376562)返回“十亿、四亿五千六百万、三十七万六千、五百六十二”。
特别聪明/优雅的解决方案可获得额外积分


这似乎是一个毫无意义的练习,但这种算法在现实世界中有很多应用程序(尽管支持高达10亿的数字可能有点过头了:-)

关于这一点,已经有一个问题了:


答案是C#,但我想你可以找到答案。

支持多达9.99亿,但没有负数:

String humanReadable(int inputNumber) {
  if (inputNumber == -1) {
    return "";
  }
  int remainder;
  int quotient;
  quotient = inputNumber / 1000000;
  remainder = inputNumber % 1000000;
  if (quotient > 0) {
    return humanReadable(quotient) + " million, " + humanReadable(remainder);
  }
  quotient = inputNumber / 1000;
  remainder = inputNumber % 1000;
  if (quotient > 0) {
    return humanReadable(quotient) + " thousand, " + humanReadable(remainder);
  }
  quotient = inputNumber / 100;
  remainder = inputNumber % 100;
  if (quotient > 0) {
    return humanReadable(quotient) + " hundred, " + humanReadable(remainder);
  }
  quotient = inputNumber / 10;
  remainder = inputNumber % 10;
  if (remainder == 0) {
    //hackish way to flag the algorithm to not output something like "twenty zero"
    remainder = -1;
  }
  if (quotient == 1) {
    switch(inputNumber) {
    case 10:
      return "ten";
    case 11:
      return "eleven";
    case 12:
      return "twelve";
    case 13:
      return "thirteen";
    case 14:
      return "fourteen";
    case 15:
      return "fifteen";
    case 16:
      return "sixteen";
    case 17:
      return "seventeen";
    case 18:
      return "eighteen";
    case 19:
      return "nineteen";
    }
  }
  switch(quotient) {
  case 2:
    return "twenty " + humanReadable(remainder);
  case 3:
    return "thirty " + humanReadable(remainder);
  case 4:
    return "forty " + humanReadable(remainder);
  case 5:
    return "fifty " + humanReadable(remainder);
  case 6:
    return "sixty " + humanReadable(remainder);
  case 7:
    return "seventy " + humanReadable(remainder);
  case 8:
    return "eighty " + humanReadable(remainder);
  case 9:
    return "ninety " + humanReadable(remainder);
  }
  switch(inputNumber) {
  case 0:
    return "zero";
  case 1:
    return "one";
  case 2:
    return "two";
  case 3:
    return "three";
  case 4:
    return "four";
  case 5:
    return "five";
  case 6:
    return "six";
  case 7:
    return "seven";
  case 8:
    return "eight";
  case 9:
    return "nine";
  }
}
导入数学
def encodeOnesDigit(数字):
返回['''1','2','3','4','5','6','7','8','9'][num]
数字(num):
返回值['二十','三十','四十','五十','六十','七十','八十','九十'][num-2]
定义编码(num):
如果num<10:
返回encodeOnesDigit(num)
其他:
返回['十'、'十一'、'十二'、'十三'、'十四'、'十五'、'十六'、'十七'、'十八'、'十九'][num-10]
def编码三元组(num):
如果num==0:返回“”
str=''
如果num>=100:
str=encodeOnesDigit(num/100)+“百”
十位数=num%100
如果tens>=20:
如果str!='':str+=''
str+=数字(十位数/10位数)
如果10%10>0:
str+='-'+encodeOnesDigit(十分之十)
伊里夫十0:
如果str!='':str+=''
str+=编码十几(十)
返回str
def ZipNumber(numList):
如果len(numList)==1:
返回numList[0]
strList=[''000'、'000000'、'十亿']#根据需要添加更多
strList=strList[:len(numList)]
strList.reverse()
joinedList=zip(numList,strList)
joinedList=[如果项目[0]!='',则joinedList中的项目对应项目]
返回“,”.join(“”.join(项目)用于joinedList中的项目)
def人类可读(num):
如果num==0:返回'zero'
否定=错误
如果num<0:
num*=-1
负=真
numString=str(num)
tripletCount=int(数学单元(len(numString)/3.0))
numString=numString.zfill(tripletCount*3)
TripleList=[int(numString[i*3:i*3+3])表示范围内的i(tripletCount)]
readableList=[encodeTriplet(num)表示三元组列表中的num]
readableStr=zipNumbers(readableList)
如果为负,则返回“负”+readableStr,否则返回readableStr
使用系统;
命名空间可读
{
公共静态类HumanReadableExt
{
专用静态只读字符串[]\u位={
一、二、三、四、五、,
“六”、“七”、“八”、“九”、“十一”、“十二”,
“十三”,“十四”,“十五”,“十六”,“十七”,
“十八”,“十九”
};
专用静态只读字符串[]\u={
二十、三十、四十、五十、,
“六十”、“七十”、“八十”、“九十”
};
私有静态只读字符串[]\u百万={
“千”、“百万”、“十亿”、“万亿”
};
专用静态字符串Seg(整数)
{
var work=string.Empty;
如果(数字>=100)
工作+=\u位数[数字/100]+“百”;
如果((数字%100)<20)
工时+=_位数[数字%100];
其他的
工作+=_青少年[(数字%100)/10]+“-”+_数字[数字%10];
返回工作;
}
公共静态字符串可读(此整数)
{
如果(数字==0)
返回“零”;
var work=string.Empty;
var parts=新字符串[_millions.Length];
对于(var ind=0;ind=0)
work=work.Substring(0,lastSpace)+”和“+work.Substring(lastSpace);
返回工作;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(1.HumanReadable());
Console.WriteLine(53.HumanReadable());
Console.WriteLine(723603.HumanReadable());
Console.WriteLine(1456376562.HumanReadable());
Console.ReadLine();
}
}
}

这个函数的实现有一个很大的问题。这是未来的本地化。这个函数是由英语母语人士编写的,很可能对英语以外的任何其他语言都不起作用。要为世界上任何一种人类语言方言编写通用的、易于本地化的函数几乎是不可能的,除非您确实需要保持它的通用性。实际上,在现实世界中,您不需要对大整数进行运算,所以您可以将所有数字保持在一个大的(或)整数中
import math

def encodeOnesDigit(num):
   return ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'][num]

def encodeTensDigit(num):
   return ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'][num-2]

def encodeTeens(num):
   if num < 10:
      return encodeOnesDigit(num)
   else:
      return ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'][num-10]

def encodeTriplet(num):
   if num == 0: return ''
   str = ''
   if num >= 100:
      str = encodeOnesDigit(num / 100) + ' hundred'
   tens = num % 100
   if tens >= 20:
      if str != '': str += ' '
      str += encodeTensDigit(tens / 10)
      if tens % 10 > 0:
         str += '-' + encodeOnesDigit(tens % 10)
   elif tens != 0:
      if str != '': str += ' '
      str += encodeTeens(tens)
   return str

def zipNumbers(numList):
   if len(numList) == 1:
      return numList[0]
   strList = ['', ' thousand', ' million', ' billion'] # Add more as needed
   strList = strList[:len(numList)]
   strList.reverse()
   joinedList = zip(numList, strList)
   joinedList = [item for item in joinedList if item[0] != '']
   return ', '.join(''.join(item) for item in joinedList)

def humanReadable(num):
   if num == 0: return 'zero'
   negative = False
   if num < 0:
      num *= -1
      negative = True
   numString = str(num)
   tripletCount = int(math.ceil(len(numString) / 3.0))
   numString = numString.zfill(tripletCount * 3)
   tripletList = [int(numString[i*3:i*3+3]) for i in range(tripletCount)]
   readableList = [encodeTriplet(num) for num in tripletList]
   readableStr = zipNumbers(readableList)
   return 'negative ' + readableStr if negative else readableStr
using System;

namespace HumanReadable
{
    public static class HumanReadableExt
    {
        private static readonly string[] _digits = {
                                                       "", "one", "two", "three", "four", "five",
                                                       "six", "seven", "eight", "nine", "eleven", "twelve",
                                                       "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
                                                       "eighteen", "nineteen"
                                                   };

        private static readonly string[] _teens = {
                                                      "", "", "twenty", "thirty", "forty", "fifty",
                                                      "sixty", "seventy", "eighty", "ninety"
                                                  };

        private static readonly string[] _illions = {
                                                        "", "thousand", "million", "billion", "trillion"
                                                    };

        private static string Seg(int number)
        {
            var work = string.Empty;

            if (number >= 100) 
                work += _digits[number / 100] + " hundred ";

            if ((number % 100) < 20)
                work += _digits[number % 100];
            else
                work += _teens[(number % 100) / 10] + "-" + _digits[number % 10];

            return work;
        }

        public static string HumanReadable(this int number)
        {
            if (number == 0)
                return "zero";
            var work = string.Empty;

            var parts = new string[_illions.Length];

            for (var ind = 0; ind < parts.Length; ind++)
                parts[ind] = Seg((int) (number % Math.Pow(1000, ind + 1) / Math.Pow(1000, ind)));

            for (var ind = 0; ind < parts.Length; ind++)
                if (!string.IsNullOrEmpty(parts[ind]))
                    work = parts[ind] + " " + _illions[ind] + ", " + work;

            work = work.TrimEnd(',', ' ');

            var lastSpace = work.LastIndexOf(' ');
            if (lastSpace >= 0)
                work = work.Substring(0, lastSpace) + " and" + work.Substring(lastSpace);

            return work;
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine(1.HumanReadable());
            Console.WriteLine(53.HumanReadable());
            Console.WriteLine(723603.HumanReadable());
            Console.WriteLine(1456376562.HumanReadable());
            Console.ReadLine();
        }
    }
}
> man number