编码挑战:将整数转换为句子。 [英] Coding challenge: convert an integer to a sentence.
本文介绍了编码挑战:将整数转换为句子。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
本周的挑战一如既往地是一个简单的挑战。
给定一个64位整数创建一个方法,将该值转换为它的口语等价物。
例如:1,203会产生一千零二三
范围使这种不必要的错综复杂是很高的。去吧!
我尝试过:
简化我的生活。
上周的获胜者是Graeme_Grant,尽管excel解决方案非常接近。发送Sean您的详细信息并赢得奖品!
The challenge for this week is, as always, a simple one.
Given a 64 bit integer create a method that will convert the value to it's spoken equivalent.
For example: 1,203 would result in "One thousand, two hundred and three"
The scope of making this needlessly convoluted is high. Go at it!.
What I have tried:
Simplifying my life.
The winner of last week's is Graeme_Grant, though the excel solutions came damn close. Send Sean your details and win a prize!
推荐答案
真的!?你现在应该知道如何使用自己的网站! :)
Really!? you should know how to use your own site by now! :)
using System;
using System.Speech.Synthesis;
namespace SampleSynthesis
{
class Program
{
static void Main(string[] args)
{
// Initialize a new instance of the SpeechSynthesizer.
SpeechSynthesizer synth = new SpeechSynthesizer();
// Configure the audio output.
synth.SetOutputToDefaultAudioDevice();
// Speak a string.
synth.Speak(NumberToWords(1234567890123));
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
public static string NumberToWords(long number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000000000) > 0)
{
words += NumberToWords(number / 1000000000000) + " trillion ";
number %= 1000000000000;
}
if ((number / 1000000000) > 0)
{
words += NumberToWords(number / 1000000000) + " billion ";
number %= 1000000000;
}
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}
}
}
您没有指定整数范围待测试或比例模式 [ ^ ]。回到我上学的日子,十亿 [ ^ ]与世界其他地方不一样但幸运的是,世界正在使用美国标准。
所以这是我的版本,支持从Int16
到<的正/负整数code> UInt64 (现在还BigInteger
);简而言之,传统的英国人和长期的数字;完全可扩展的价值大小独立;正式的&非正式的舍入;并且可以支持任何语言而不修改核心。
更新:增加了对BigIntegers& amp; 传统英国英语数字......还有4级正常&非正式四舍五入。
You didn't specify the range of ints to be tested or the Scale Mode[^]. Back in my days of school, a billion[^] was not the same as some other parts of the world however luckily, the world is using the US standard.
So here is my version that supports both positive/negative ints fromInt16
right through toUInt64
(now alsoBigInteger
); short, 'traditional' British, and long scale numbers; totally scalable as value size indepenant; formal & informal rounding; and can be made to suport any language with no modification to the core.
UPDATE: Added support for BigIntegers & "Tradition" British english numbers... Also 4 levels of normal & informal rounding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace NumToWords
{
class Program
{
static void Main(string[] args)
{
Int64 testMin = Int64.MinValue;
Int64 testMax = Int64.MaxValue;
UInt64 testUMax = UInt64.MaxValue;
Int64 testQMin = testMin / 1000;
Int64 testQMax = testMax / 1000;
UInt64 testQUMax = testUMax / 1000;
BigInteger testNegBig = -BigInteger.Pow(testQUMax, 13);
BigInteger testBig = BigInteger.Pow(testQUMax, 13);
int[] tests =
{
-1,
0,
1,
11,
101,
1234,
10001,
100001,
1100101,
10110011,
100011011,
1001010101,
-1001010101
};
Console.WriteLine("SHORT SCALE NUMBERS");
Console.WriteLine("===================\r\n");
for (int i = 0; i < tests.Length; i++)
ShowResult(tests[i]);
ShowResult(testQMin);
ShowResult(testMin);
ShowResult(testQMax);
ShowResult(testMax);
ShowResult(testQUMax);
ShowResult(testUMax);
ShowResult(testNegBig);
ShowResult(testBig);
Console.WriteLine();
Console.WriteLine("TRADITIONAL BRITISH SCALE NUMBERS");
Console.WriteLine("=================================\r\n");
for (int i = 0; i < tests.Length; i++)
ShowResult(tests[i], ScaleType.TraditionalBritish);
ShowResult(testQMin, ScaleType.TraditionalBritish);
ShowResult(testMin, ScaleType.TraditionalBritish);
ShowResult(testQMax, ScaleType.TraditionalBritish);
ShowResult(testMax, ScaleType.TraditionalBritish);
ShowResult(testQUMax, ScaleType.TraditionalBritish);
ShowResult(testUMax, ScaleType.TraditionalBritish);
ShowResult(testNegBig, ScaleType.TraditionalBritish);
ShowResult(testBig, ScaleType.TraditionalBritish);
Console.WriteLine();
Console.WriteLine("LONG SCALE NUMBERS");
Console.WriteLine("==================\r\n");
for (int i = 0; i < tests.Length; i++)
ShowResult(tests[i], ScaleType.Long);
ShowResult(testQMin, ScaleType.Long);
ShowResult(testMin, ScaleType.Long);
ShowResult(testQMax, ScaleType.Long);
ShowResult(testMax, ScaleType.Long);
ShowResult(testQUMax, ScaleType.Long);
ShowResult(testUMax, ScaleType.Long);
ShowResult(testNegBig, ScaleType.Long);
ShowResult(testBig, ScaleType.Long);
Console.WriteLine("-- Press any key to exit --");
Console.ReadKey();
}
private static void ShowResult(Int16 value, ScaleType numberScale = ScaleType.Short)
{
Console.WriteLine(seperatorLine);
Console.WriteLine(
VALUE:{value:N0 } \\\\ nTEXT:{value.ToWords(numberScale)} \\\\ n);
}
private static void ShowResult( UInt16 value ,ScaleType numberScale = ScaleType.Short)
{
Console.WriteLine(seperatorLine);
Console.WriteLine(
"VALUE: {value:N0}\r\nTEXT: {value.ToWords(numberScale)}\r\n"); } private static void ShowResult(UInt16 value, ScaleType numberScale = ScaleType.Short) { Console.WriteLine(seperatorLine); Console.WriteLine(
这篇关于编码挑战:将整数转换为句子。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文