两个数组的求和\\增加(尤其是) [英] Summation\ Addition of two arrays (particular)
问题描述
任何长度的正数重新psented作为数字字符之间的'0'和'9的阵列,ERGO $ P $。我们知道,最显著密码是在数组的索引0的位置。
例如:
- 数字是10282
- 阵列将数= [1,0,2,8,2]
此考虑,建立2阵列的功能重新其计算两者的总和\\ ADDITION \\求和presenting两个正数,并将其设置在第三阵列,包含第一2的总和。
这是怎么锻炼是从我自己的语言翻译,意大利语。
这是我的解决方案,但它并不完全工作。我试图像基本的东西
A = [1,4]和B = [4,7]。结果应该是C = [6,1],但它给了我[5,1],因为它认为在这里我采用模块化,但不是那个,我说,-1索引位置应采取++就行了。
帮助3;
警报(插入一个长');
变种K = asknum();
警报(插入B长度');
VAR H = asknum();
VAR A =新的Array(K);
变种B =新阵列(H);
// asknum()只在这个特定的环境,我们定义
//使用在大学。我猜的转变将是-prompt-功能readVet(VET){//在索引位置插入值
对于(i = 0; I< vet.length;我++)
兽医[I] = asknum();
}
readVet(A); //填充阵列
readVet(B); //填充阵列
功能sumArray(VET1,VET2){
变种C =新的Array();
为(ⅰ= vet1.length-1; I>( - 1); I - ){
为(N = vet2.length-1; N>( - 1); N - ){
C [i] = VET1 [I] + VET2 [I]
如果(C [Ⅰ]≥9){
C [i] = C [I]%10;
C [I-1] = C [I-1] ++;
}
}
}
返回℃;
}打印(sumArray(A,B));
我不知道你有一个嵌套的在这里做什么
循环。你只需要一个。另外,为了使所述环路非常简单,第一归一化的阵列,使得两者都较大阵列+1元件的长度(在进位的情况下)。然后在出路功能的纠正结果。
函数normalizeArray(数组,数字){
VAR zeroCnt =数字 - array.length,
零= [];
而(zeroCnt--){
zeroes.push(0);
}
返回zeroes.concat(数组);
}功能sumArrays(A1,A2){
变种maxResultLength = Math.max(a1.length,a2.length)+ 1; A1 = normalizeArray(A1,maxResultLength);
A2 = normalizeArray(A2,maxResultLength);
VAR的结果= normalizeArray([],maxResultLength); VAR I = maxResultLength - 1,//工作指数
数字= 0,//工作结果的数字
C = 0; //进位(0或1) 而(ⅰ> = 0){
数字= A1 [I] + A2 [I] +℃;
如果(数字> 9){
C = 1;
数字 - = 10;
}其他{
C = 0;
}
结果[我 - ] =数字;
} / *如果没有进位最显著的数字,砍掉多余的0 * /
/ *如果主叫方给我们的阵列和一帮前导零,砍那些关* /
/ *但不要像sqykly每个数字白痴和片= D * /
对于(i = 0; I< result.length&放大器;&安培;结果[I] === 0;我++){
/ *结果= result.slice(1);不这样做,或任何* /
}
返回result.slice(ⅰ);
}
这给出了预期的输出。
"A positive number of whatever length is represented as an array of numerical characters, ergo between '0's and '9's. We know that the most significant cypher is in position of index 0 of the array.
Example: - Number is 10282 - Array will be number = [1,0,2,8,2]
This considered, create a function of 2 arrays representing two positive numbers that calculates the SUM\ADDITION\SUMMATION of both of them and set it in a third array, containing the sum of the first 2."
This is how the exercise is translated from my own language, italian.
This is my solution but it doesnt entirely work. I have tried with basic stuff like A=[1,4] and B=[4,7]. The results should be C=[6,1] but it gives me [5,1] as it considers the line where I use the modular but not the one where I say that the -1 index position should take a ++.
Help <3
alert('Insert A length');
var k=asknum();
alert('Insert B length');
var h=asknum();
var A = new Array(k);
var B = new Array(h);
// asknum() is only defined in this particular environment we are
// using at the university. I guess the turnaround would be -prompt-
function readVet(vet){//inserts values in index positions
for(i=0;i<vet.length;i++)
vet[i]=asknum();
}
readVet(A);//fills array
readVet(B);//fills array
function sumArray(vet1,vet2){
var C = new Array();
for(i=vet1.length-1;i>(-1);i--){
for(n=vet2.length-1;n>(-1);n--){
C[i]=vet1[i]+vet2[i];
if(C[i]>9){
C[i]=C[i]%10;
C[i-1]=C[i-1]++;
}
}
}
return C;
}
print(sumArray(A,B));
I'm not sure what you're doing with a nested for
loop here. You just need one. Also, to make said loop really simple, normalize the arrays first so that both are the length of the larger array + 1 element (in case of carry). Then correct the result on the way out of the function.
function normalizeArray(array, digits) {
var zeroCnt = digits - array.length,
zeroes = [];
while (zeroCnt--) {
zeroes.push(0);
}
return zeroes.concat(array);
}
function sumArrays(a1, a2) {
var maxResultLength = Math.max(a1.length, a2.length) + 1;
a1 = normalizeArray(a1, maxResultLength);
a2 = normalizeArray(a2, maxResultLength);
var result = normalizeArray([], maxResultLength);
var i = maxResultLength - 1, // working index
digit = 0, // working result digit
c = 0; // carry (0 or 1)
while (i >= 0) {
digit = a1[i] + a2[i] + c;
if (digit > 9) {
c = 1;
digit -= 10;
} else {
c = 0;
}
result[i--] = digit;
}
/* If there was no carry into the most significant digit, chop off the extra 0 */
/* If the caller gave us arrays with a bunch of leading zeroes, chop those off */
/* but don't be an idiot and slice for every digit like sqykly =D */
for (i = 0 ; i < result.length && result[i] === 0 ; i++) {
/* result = result.slice(1); don't do that, or anything */
}
return result.slice(i);
}
That gives the expected output.
这篇关于两个数组的求和\\增加(尤其是)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!