问题描述
这可能是一个非常简单的问题,但我需要复制在阵列两个int值[0]和阵列[1]为一个整数。
It may be a really simple question, but I need to copy two int values that are in array[0] and array[1] into a single integer.
作为实例,如果阵列[0] = 1和阵列[1] = 6,我需要的整数等于16。
阵列的任何元件具有范围从0到9。
As instance if array[0]=1 and array[1]=6, I need the integer to be equal to "16".Any element of the array has a range from 0 to 9.
什么机会做我有吗?
谢谢了很多!
What chances do I have?Thank you a lot!
推荐答案
请问阵列[0]总是重新present 10的位置和阵列[1]随时重新present 1的地方吗?将阵列[0]或阵列[1]曾经有大于9或小于0的值?会不会有永远是一个数组[2]或数组[3]重新present 3位或4位数字?
Does array[0] always represent the 10's place and array[1] always represent the 1's place? Will array[0] or array[1] ever have a value greater than 9 or less than 0? Will there ever be an array[2] or an array[3] to represent a 3 digit or a 4 digit number?
如果回答上述问题是是,否,否,那么是不是答案简单的算术题?
If the answer to the above questions are Yes, No, and No, then isn't the answer simple arithmetic?
int result = (array[0] * 10) + array[1];
如果数据不是pre范围检查的,那么你就需要补充的一步。即使数据是pre范围检查的,你应该考虑补充反正这一步,使多余的肯定。
If the data isn't pre range-checked, then you'll need to add that step. Even if the data IS pre range-checked, you should consider adding that step anyway to make extra sure.
'10'也是一个幻数在上述的例子。或许,这将是明智的,不是硬code 10,但它的基础关闭阵列的大小。考虑在有数组[2]的情况。 。 。数组[N]。然后呢?
'10' is also a magic number in the above example. It would probably be wise to not hard-code 10, but base it off of the size of the array. Consider the case where there IS an array[2] . . . array[n]. What then?
这篇关于"复制和QUOT;数组中的元素,以成" INT"? C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!