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. HDU 3625 Examining the Rooms【第一类斯特灵数】

    <题目链接> <转载于 >>> > 题目大意:有n个锁着的房间和对应n扇门的n把钥匙,每个房间内有一把钥匙.你可以破坏一扇门,取出其中的钥匙,然后用取出钥匙打 ...

  2. 洛谷 P1057 传球游戏 【dp】(经典)

    题目链接:https://www.luogu.org/problemnew/show/P1057 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游 ...

  3. ubuntu10.4搭建eclipse for c++环境

    以下操作:经过验证,安装完成后,使用eclipse建立C++工程,直接编译就能运行了:说明:(1)不建议使用下载eclipse for java版本和CDT的方式来搭建环境,因为我没有安装成功:(2) ...

  4. android studio 代码模板

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com android studio 代码模板 an ...

  5. BZOJ.1010.[HNOI2008]玩具装箱toy(DP 斜率优化/单调队列 决策单调性)

    题目链接 斜率优化 不说了 网上很多 这的比较详细->Click Here or Here //1700kb 60ms #include<cstdio> #include<cc ...

  6. 潭州课堂25班:Ph201805201 WEB 之 CSS 第二课 (课堂笔记)

    CSS 的引入方法: 第一种 : <!--直接在标签仙设置--><p style="color: yellow">CSS的第一种引入方法</p> ...

  7. [POI2011]Śmieci

    [POI2011]Śmieci 题目大意: 一个\(n(n\le10^5)\)个点\(m(m\le10^6)\)条边的无向图,每条边有边权\(0/1\),试找出若干个环,使得每次翻转环上所有边的权值, ...

  8. spring源码分析系列 (2) spring拓展接口BeanPostProcessor

    Spring更多分析--spring源码分析系列 主要分析内容: 一.BeanPostProcessor简述与demo示例 二.BeanPostProcessor源码分析:注册时机和触发点 (源码基于 ...

  9. 利用gsoap工具,通过wsdl文件生成webservice的C++工程文件

    一.下载gsoap文件,下载地址:https://zh.osdn.net/projects/sfnet_gsoap2/releases/ 二.以gsoap-2.8为例,解压到D盘,在D:/gsoap- ...

  10. java内部类(三)

    内部类之方法内部类 方法内部类就是内部类定义在外部类方法中,方法内部类只在该方法内部可见,即只在该方法内部使用. 注意:由于方法内部类不能在外部类的方法以外的地方使用,因此方法内部类不能使用访问控制符 ...