我正在尝试做点什么:
Input <= 3432
Output => List contains 7 items i.e. (3, 1000, 4, 100, 3, 10, 2)
为此我在c#中写了一个函数:
public BigInterger FromDecimal()
{
List<BigInteger> numbers = new List<BigInteger>();
BigInteger number = BigInteger.Parse(NumberString);
while (number > 0)
{
if (number >= 1000000000)
{
numbers.Add(number / 1000000000);
number = number % 1000000000;
numbers.Add(1000000000);
}
else if (number >= 100000000)
{
numbers.Add(number / 100000000);
number = number % 100000000;
numbers.Add(100000000);
}
else if (number >= 10000000)
{
numbers.Add(number / 10000000);
number = number % 10000000;
numbers.Add(10000000);
}
else if (number >= 1000000)
{
numbers.Add(number / 1000000);
number = number % 1000000;
numbers.Add(1000000);
}
else if (number >= 100000)
{
numbers.Add(number / 100000);
number = number % 100000;
numbers.Add(100000);
}
else if (number >= 10000)
{
numbers.Add(number / 10000);
number = number % 10000;
numbers.Add(10000);
}
else if (number >= 1000)
{
numbers.Add(number / 1000);
number = number % 1000;
numbers.Add(1000);
}
else if (number >= 100)
{
numbers.Add(number / 100);
number = number % 100;
numbers.Add(100);
}
else if (number >= 10)
{
numbers.Add(number / 10);
number = number % 10;
numbers.Add(10);
}
else
{
numbers.Add(number);
number = 0;
}
}
return numbers;
}
现在我觉得这个代码太大了,有没有更好的方法来实现这个目标?
答案 0 :(得分:2)
创建一个10的幂表,或者随时计算它们。这样您就可以使用常规循环将数字分成数字对和它们的位置值,而不是使用一个上升到固定数字的展开循环。
以下是一种方法:
var ten = new BigInteger(10);
var res = new List<BigInteger>();
var number = BigInteger.Parse(NumberString);
int pow = 0;
while (!BigInteger.Zero.Equals(number)) {
var lastDigit = BigInteger.Remainder(number, ten);
var placeValue = BigInteger.Pow(ten, pow++);
res.Add(placeValue);
res.Add(lastDigit);
number = BigInteger.Divide(number, ten);
}
答案 1 :(得分:0)
public List<int> ParseIntoList(string initialNumber, List<int> currentArray)
{
if (initialNumber.Length == 0){
return currentArray;
}
var digit = initialNumber[0];
initialNumber = initialNumber.Substring(1);
var power = Math.Pow(10, initialNumber.Length);
currentArray.Add(digit);
currentArray.Add(power);
return ParseIntoList(initialNumber, currentArray);
}
以下一种方式调用它:ParseIntoList("3432", new List <int>()).
您可能希望将其包装在调用中或执行算法的itterative版本
答案 2 :(得分:0)
您可以对字符串进行操作:
static void Main(string[] args)
{
var input = "3432";
var result = Foo(input);
}
private static string[] Foo(string input)
{
var output = new List<string>();
var power = input.Length;
for (int i = 0; i < input.Length; i++)
{
output.Add(input[i].ToString());
if (i < input.Length-1)
output.Add(Math.Pow(10, power).ToString());
power--;
}
return output.ToArray();
}
答案 3 :(得分:0)
string inputString = "3432";
var result = Enumerable.Range(0, inputString.Length)
.SelectMany(
i => new[]
{
Convert.ToInt32(inputString[i] - '0'),
(int) Math.Pow(10, inputString.Length - i - 1)
}, (i, g) => g).ToList();
答案 4 :(得分:0)
static IEnumerable<long> GetDigitsAndMultiplesOfTen(long value)
{
var powerOfTen = (long)Math.Ceiling(Math.Log10(value));
var divisor = (long)Math.Pow(10, powerOfTen);
while (divisor > 0)
{
var quotient = value / divisor;
if (quotient > 0)
{
yield return quotient;
if (divisor > 1)
yield return divisor;
}
value %= divisor;
divisor /= 10;
}
}
程序代码:
long value = 3432;
var list = GetDigitsAndMultiplesOfTen(value).ToList();
foreach (var item in list)
Console.WriteLine(item);
输出:
3
1000
4
100
3
10
2