如何在其索引中添加数字 [英] How to add a number to its index
本文介绍了如何在其索引中添加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字,让我们假设345624354,因为该数字的索引是012345678,所以我想将它们添加为3 + 0、4 + 1 5 + 2,依此类推.
I am having a number let us assume i.e 345624354 and as the index of this number is 012345678 i would like to add them as 3+0, 4+1 5+2 and so on any idea please
推荐答案
/// <summary>
/// Gives Number with each digit added with it''s according index
/// </summary>
/// <param name="num">Number you want to have Index adding with each digit i.e 345624354</param>
/// <returns>returns number with index added i.e 35796991212</returns>
private long GetMyNo(int num)
{
string input = num.ToString();
StringBuilder output = new StringBuilder();
int i = 0;
foreach (char c in input)
{
output.Append(int.Parse(c.ToString()) + (i++));
}
return long.Parse(output.ToString());
}
如果有帮助,请 投票 和 接受答案 .
Please vote and Accept Answer if it Helped.
可以拆分使用循环将数字转换成各个数字,然后除/余,或者将其转换为字符串,然后对字符使用foreach循环:
Either split the number into the various digits using a loop and divide / remainder, or convert it to a string and use a foreach loop on the characters:
while (myInt > 0)
{
myDigit = myInt % 10;
myInt /= 10;
... deal with myDigit ...
}
string myNumber = myInt.ToString();
int index = 0;
foreach (character c in myNumber)
{
... deal with each digit ...
}
这篇关于如何在其索引中添加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文