《剑指offer》第六十六题(构建乘积数组)
// 面试题66:构建乘积数组
// 题目:给定一个数组A[0, 1, …, n-1],请构建一个数组B[0, 1, …, n-1],其
// 中B中的元素B[i] =A[0]×A[1]×… ×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。 #include <iostream>
#include <vector> using namespace std;
//把B[i]看成[=A[0],A[1],… ,A[i-1],1,A[i+1],…,A[n-1]]
//对于B,就成了二维数组,对于1左面是上三角矩阵,右面是下三角矩阵
//三角矩阵的每行乘积值计算可以从顶向下
void BuildProductionArray(const vector<double>& input, vector<double>& output)
{
int length1 = input.size();
int length2 = output.size(); if (length1 == length2 && length2 > )//还是要边界判断一下
{
output[] = ;
for (int i = ; i < length1; ++i)//计算左面上三角矩阵的每行乘积值
{
output[i] = output[i - ] * input[i - ];
} double temp = ;
for (int i = length1 - ; i >= ; --i)//注意两个循环的i初始化值
{
temp *= input[i + ];//计算下三角矩阵的每行乘积值
output[i] *= temp;//上下三角的同行乘机值再相乘,就是满足题意的B[i]值了
}
}
} //================= Test Code =================
static bool EqualArrays(const vector<double>& input, const vector<double>& output)
{
int length1 = input.size();
int length2 = output.size(); if (length1 != length2)
return false; for (int i = ; i < length1; ++i)
{
if (abs(input[i] - output[i]) > 0.0000001)
return false;
} return true;
} static void test(const char* testName, const vector<double>& input, vector<double>& output, const vector<double>& expected)
{
printf("%s Begins: ", testName); BuildProductionArray(input, output);
if (EqualArrays(output, expected))
printf("Passed.\n");
else
printf("FAILED.\n");
} static void test1()
{
// 输入数组中没有0
double input[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
vector<double> output1= vector<double>(output, output + sizeof(output) / sizeof(double)); test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
output1,
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
} static void test2()
{
// 输入数组中有一个0
double input[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
vector<double> output1 = vector<double>(output, output + sizeof(output) / sizeof(double)); test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
output1,
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
} static void test3()
{
// 输入数组中有两个0
double input[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
vector<double> output1 = vector<double>(output, output + sizeof(output) / sizeof(double)); test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
output1,
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
} static void test4()
{
// 输入数组中有正、负数
double input[] = { , -, , -, };
double output[] = { , , , , };
double expected[] = { , -, , -, };
vector<double> output1 = vector<double>(output, output + sizeof(output) / sizeof(double)); test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
output1,
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
} static void test5()
{
// 输入输入中只有两个数字
double input[] = { , - };
double output[] = { , };
double expected[] = { -, };
vector<double> output1 = vector<double>(output, output + sizeof(output) / sizeof(double)); test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
output1,
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
} int main(int argc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
system("pause");
return ;
}
《剑指offer》第六十六题(构建乘积数组)的更多相关文章
- (剑指Offer)面试题52:构建乘积数组
题目: 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不能 ...
- 【剑指offer】不使用除法,构建乘积数组,C++实现
# 题目 # 思路 设C[i] = A[0] * A[1] * - * A[i-1],D[i] = A[i+1] * - * A[n-1],则C[i]按照从上到下的顺序计算,即C[i] = C[i- ...
- 剑指Offer(二十六):二叉搜索树与双向链表
剑指Offer(二十六):二叉搜索树与双向链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- 剑指Offer(三十六):两个链表的第一个公共结点
剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 剑指Offer(三十二):把数组排成最小的数
剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 《剑指offer》第二十六题(树的子结构)
// 面试题26:树的子结构 // 题目:输入两棵二叉树A和B,判断B是不是A的子结构. #include <iostream> struct BinaryTreeNode { doubl ...
- 《剑指offer》第十六题(数值的整数次方)
// 面试题:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需要考 ...
- 《剑指offer》第十九题(正则表达式匹配)
// 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...
- 《剑指offer》第二十九题(顺时针打印矩阵)
// 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
随机推荐
- intelliJ IDEA之使用svn或git管理代码
intelliJ IDEA之使用svn管理代码 1.VCS—>import into Version Control—>Share Project(Subversion) 2.点击+ ...
- JDK源码之ThreadLocal
1.定义 ThreadLocal是线程变量,就是说每一个线程都有对应的该变量副本,线程修改该变量时,线程与线程之间的变量是相互隔离的,互相并看不见.这个结构附带在线程上,一个线程可以根据ThreadL ...
- div容器中内容垂直居中
#box{ width:200px; height:200px; line-height: 200px; vertical-align: middle; margin: 5px; background ...
- numpy 学习笔记
numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...
- 07:vue定义路由
1.1 定义路由 1.说明 1. 路由是单页面应用程序(SPA)的关键,Vue提供过来路由插件,使用这个路由就要安装这个插件 2. 安装: npm install vue-router 3. 依赖于v ...
- SqlServer字符串拼接
ID Name 2 小红 2 小明 2 小青 3 大红 3 大明 3 大青 有一张这样的表,现在要达到 把ID为2的Name合拼成一行显示出来: step1:建函数 CREATE FUNCTI ...
- thiniphp tp5 使用缓存
在应用或者模块配置文件中配置好所用缓存的类型及相关参数: 如果是文件类型可以用 'cache' => [ 'type' => 'File', 'path' => CACHE_PATH ...
- FJUT 聪明的商人(树上倍增)题解
思路:求树上两点的距离,显然是dep[u] + dep[v] - 2 * dep[lca],用树上倍增去写. 参考:树上倍增的写法和应用(详细讲解,新手秒懂) 代码: #include<set& ...
- Many Website Of WallPaper
我在这里给大家推荐几个不错的壁纸网站 毕竟一张赏心悦目的壁纸能让你的工作效率提高不少 注意前方高能 一大波网站即将来袭 一系列 如你所见 alphacoders wallpaperdj Wallhav ...
- MyEclipse代码编辑器中汉字太小的解决办法(中文看不清)
问题描述:新安装的myeclipse 2014,代码编辑器中汉字很小看不清 解决办法:调整字体即可.通过菜单Windows——Preferences,输入font过滤选择Colors and Font ...