Questin:

There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. Solve it without division operator and in O(n).

For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].

Example:

A: {4, 3, 2, 1, 2}
OUTPUT: {12, 16, 24, 48, 24}

思路:

可以使用迭代累计。把output[i]=a[i]左边的乘积 x a[i]右边的乘积,所以,我们可以分两个循环,第一次先把A[i]左边的乘积放在Output[i]中,第二次把A[i]右边的乘积算出来;也可以直接放在一个循环中,只不过需要同时计算左边的乘积和右边的乘积。(当然也可分开计算A[i]左边和右边的数据,这样容易理解!最后将左边和右边的相乘即可

代码如下:

void Multiplication_Array(int A[], int OUTPUT[], int n) {
int left = ;
int right = ;
for (int i = ; i < n; i++)
OUTPUT[i] = ;
for (int i = ; i < n; i++) {
OUTPUT[i] *= left;
OUTPUT[n - - i] *= right;
left *= A[i];
right *= A[n - - i];
}
}
void Mutiplication_Array2()
{
    int *X = new int[n];
    int *Y = new int[n];
    // Create X
    X[0] = 1;
    for(int i = 1; i < n; i++){
        X[i] = X[i-1] * A[i-1];
    }
    // Create Y
    Y[n-1] = 1;
    for(int i = n-2; i >= 0; i--){
        Y[i] = Y[i+1] * A[i+1];
    }
    // Create Out
    for(int i = 0; i < n; i++){
        out[i] = X[i] * Y[i];
    }
    // Delete X and Y
    delete[] X;
    delete[] Y;
}

Multiplication of numbers的更多相关文章

  1. [RxJS] Transformation operator: map and mapTo

    We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...

  2. POJ2505 A multiplication game[博弈论]

    A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6028   Accepted:  ...

  3. Booth Multiplication Algorithm [ASM-MIPS]

    A typical implementation Booth's algorithm can be implemented by repeatedly adding (with ordinary un ...

  4. hdu4951 Multiplication table (乘法表的奥秘)

    http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...

  5. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  6. acdeream Matrix Multiplication

    D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...

  7. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  8. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

  9. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

随机推荐

  1. presistence

    每一个神都是从弱到强的,像继科,在2011年之前,人很浮躁,球不稳,只是偶尔打出高质量而已:在输了无数场球之后,球厚了,人也定了(刘国梁评价),才抓住的机会成就了最快大满贯,并且创造了之后的辉煌,继科 ...

  2. 线程、对称多处理和微内核(OS 笔记三)

    线程.对称多处理 ​ 这一部分继续深入探讨与进程管理相关的高级概念并了解多处理机的对称多处理技术. 进程和线程 到目前为止提出的进程的概念包含两个特点: 资源所有权 存放进程映像的虚拟地址空间 调度/ ...

  3. SQLite中的运算符表达式

    SQLite中的运算符表达式 在SQLite中,运算符包括了一元运算符,二元运算符(二进制运算符)和三元元素符.图3.12中给出了这些运算符需要的参数以及优先级.其中,一元运算符的优先级最高,三元运算 ...

  4. 潭州课堂25班:Ph201805201 并发(协程) 第十五课 (课堂笔记)

    #斐波那契 def fid(n): res = [] indx = 0 a = 0 b = 1 while indx < n : res.append(b) a,b = b,a+b indx + ...

  5. [CodeVS4438]YJQ Runs Upstairs

    [CodeVS4438]YJQ Runs Upstairs 题目大意: 一个\(n(n\le50)\)个点\(m(m\le300)\)条边的DAG,保证从\(1\)到\(n\)的所有路径经过边数均小于 ...

  6. 微信企业号开发之weixin://preInjectJSBridge/fail

    最近几天遇到个奇怪的问题,目前只有在Andriod平台上出现:weixin://preInjectJSBridge/fail 不止我一个人遇到这个问题,群里也有好几个问了这个问题.这个问题直接导致我们 ...

  7. C# 论接口的意义---共享协议

    我对接口的理解: 接口是一种协议.是一种模型. 我认为接口的意义: 接口更好的实现了项目资源共享 , 指定了可共享的范围 , 允许可使用而不可篡改的项目资源 . 我认为接口和模型是一类的: 接口一般与 ...

  8. Sallen-Key Active Butterworth Low Pass Filter Calculator

    RC 2nd Order Passive Low Pass Filter The cut-off frequency of second order low pass filter is given ...

  9. ES6 js中const,var,let区别 今天第一次遇到const定义的变量

    今天第一次遇到const定义的变量,查阅了相关资料整理了这篇文章.主要内容是:js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始化. 1 ...

  10. Vert.x入门教程之Hello World

    http://blog.csdn.net/caihuangshi/article/details/51648182