[英]How to convert int array to int?
What I would like to learn how to do is to convert an int array to an int in C#. 我想学习的方法是将一个int数组转换为C#中的int。
However I want to append the int with the values from the array. 但是我想用数组中的值附加int。
Example: 例:
int[] array = {5, 6, 2, 4};
Would be converted into an int that equals 5624. 将被转换为等于5624的int。
Thanks for any help in advance. 在此先感谢您的帮助。
simply multiply each number with 10^ his place in the array. 简单地将每个数字乘以10 ^他在数组中的位置。
int[] array = { 5, 6, 2, 4 };
int finalScore = 0;
for (int i = 0; i < array.Length; i++)
{
finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1));
}
int output = array
.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
.Sum();
Another simple way: 另一个简单方法:
int[] array = {5, 6, 2, 4};
int num;
if (Int32.TryParse(string.Join("", array), out num))
{
//success - handle the number
}
else
{
//failed - too many digits in the array
}
Trick here is making the array a string of digits then parsing it as integer. 这里的诀窍是使数组成为一串数字,然后将其解析为整数。
Use this code you just want to concatenate you int array so use the following code 使用此代码您只想连接int数组,因此请使用以下代码
String a;
int output;
int[] array = {5, 6, 2, 4};
foreach(int test in array)
{
a+=test.toString();
}
output=int.parse(a);
//where output gives you desire out put
This is not an exact code. 这不是一个确切的代码。
Try the following: 请尝试以下方法:
int[] intArray = new int[] { 5, 4, 6, 1, 6, 8 };
int total = 0;
for (int i = 0; i < intArray.Length; i++)
{
int index = intArray.Length - i - 1;
total += ((int)Math.Pow(10, index)) * intArray[i];
}
int result = 0;
int[] arr = { 1, 2, 3, 4};
int multipicator = 1;
for (int i = arr.Length - 1; i >= 0; i--)
{
result += arr[i] * multipicator;
multipicator *= 10;
}
在C#3.0及以上版本中:
array.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1))).Sum();
you can use string stream (include "sstream") 你可以使用字符串流(包括“sstream”)
using namespace std; using namespace std; int main(){
int main(){
int arr[3]={3,2,4}; //your array..
stringstream ss;
ss<<arr[0]; //this can be run as a loop
ss<<arr[1];
ss<<arr[2];
int x;
ss>>x;
cout<<x; //simply the int arr[3] will be converted to int x..
This would be easy, if you have understood how the decimal system works. 如果您已了解十进制系统的工作原理,那么这很容易。
So let me explain that for you: A decimal digit contains single digits by base ten. 因此,让我为您解释一下:十进制数字包含十位数的单位数。
This means you have to iterate through this array (backwards!) and multiply by 10^ 这意味着你必须遍历这个数组(向后!)并乘以10 ^
For an example 5624 means: (5*10^3) + (6*10^2) + (2*10^1) + (4*10^0) 例如5624表示:(5 * 10 ^ 3)+(6 * 10 ^ 2)+(2 * 10 ^ 1)+(4 * 10 ^ 0)
Please consider also: http://en.wikipedia.org/wiki/Horner_scheme 请另请考虑: http : //en.wikipedia.org/wiki/Horner_scheme
This will do it: 这样做:
public int DoConvert(int[] arr)
{
int result = 0;
for (int i=0;i<arr.Length;i++)
result += arr[i] * Math.Pow(10, (arr.Length-1)-i);
return result;
}
而且只是为了好玩......
arr.Select((item, index) => new { Item = item, Power = arr.Length - (index - 1) }).ToList().ForEach(item => total += (int)(Math.Pow(10, item.Power) * item.Item));
var finalScore = int.Parse(array
.Select(x => x.ToString())
.Aggregate((prev, next) => prev + next));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.