两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];
转自:http://blog.csdn.net/shandianling/article/details/8785269
问题描述:两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];
要求:
1.不准用除法运算
2.除了循环计数值,a[N],b[N]外,不准再用其他任何变量(包括局部变量,全局变量等)
3.满足时间复杂度O(n),空间复杂度O(1)
#include <stdio.h>
#include <stdlib.h> void pr_arr(int s[],int len)
{
for(int i = ; i <= len - ; i++)
{
printf("%d \n",s[i]);
}
}
int main()
{
int a[]={,,,,,,,,,};
int *b=(int*)malloc(sizeof(a));
b[]=;
int len=sizeof(a)/sizeof(int);
int j,i;
for( i=;i<len;i++)
{
b[i]=b[i-]*a[i-];
}
for(j=len-;j>=;j--)
{
b[]*=a[j+];
b[j]*=b[];
}
b[]*=a[];
pr_arr(b,len);
return ;
}
- #include <stdio.h>
- #include <stdlib.h>
- void pr_arr(int *s,char len)
- {
- while(len--)
- {
- printf("%d \n",*s++);
- }
- }
- int main()
- {
- int a[]={2,3,7,23,6,5,1,23,89,23};
- int *b=(int*)malloc(sizeof(a));
- b[0]=1;
- int len=sizeof(a)/sizeof(*a);
- int j,i;
- for( i=1;i<len;i++)
- {
- b[i]=b[i-1]*a[i-1];
- }
- for(j=len-2;j>=1;j--)
- {
- b[0]*=a[j+1];
- b[j]*=b[0];
- }
- b[0]*=a[1];
- pr_arr(b,len);
- return 0;
- }
两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];的更多相关文章
- js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的
题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...
- OpenCL入门:(二:用GPU计算两个数组和)
本文编写一个计算两个数组和的程序,用CPU和GPU分别运算,计算运算时间,并且校验最后的运算结果.文中代码偏多,原理建议阅读下面文章,文中介绍了OpenCL相关名词概念. http://opencl. ...
- 函数指针的返回值是指针数组,数组里放的是int;函数指针的返回值是指针数组,数组里放的是int指针
函数指针的返回值是指针数组,数组里放的是int 函数指针的返回值是指针数组,数组里放的是int指针 #include <stdio.h> #include <stdlib.h> ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [C++]for同时遍历两个数组
C++11同时遍历两个数组 #define for2array(x,y,xArray,yArray) \ for(auto x=std::begin(xArray), x##_end=std::end ...
- PHP两个数组相加
在PHP中,当两个数组相加时,会把第二个数组的取值添加到第一个数组上,同时覆盖掉下标相同的值: <?php $a = array("a" => "apple& ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- array_intersect() php筛选两个数组共有的元素
我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...
随机推荐
- POJ1789Truck History
题意 : 说实话,题意我没看懂,后来让人给我讲的样例..... 4 aaaaaaa baaaaaa abaaaaa aabaaaa 0 这个样例的话,就是输入n下面n行,每行7个字母,让你依次选两行进 ...
- Jmeter 快速入门教程(二)--创建简单web测试
[版权所有: whoistester.com & jmeter.cf] http://wenku.baidu.com/linkurl=9zc4VHe6vUUeMdDZPpNsRehkazZFw ...
- *[topcoder]LongWordsDiv2
http://community.topcoder.com/stat?c=problem_statement&pm=13147 此题关键在于发现ABAB的组合最多有26*26种,可以穷举,然后 ...
- Linux资源监控命令/工具(综合)
目录: ps pstree pidof top free uptime ifuser lsof mpstat vmstst pidstat iostat iotop watch sar 1.ps 1) ...
- 使用 Spring 3 来创建 RESTful Web Services(转)
使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...
- Java文件解压之TGZ解压
package com.alibaba.intl.batch.dependency; import java.io.File; import java.io.FileInputStream; impo ...
- 在Eclipse下debug 出现Source not found for ...
在Eclipse下debug 出现Source not found for ... 在Eclipse下调试Servlet出现了Source not found for XxxAction.execut ...
- Intellij Idea 创建Web项目入门(一)
相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...
- 什么是HttpOnly
1.什么是HttpOnly? 如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索 2.ja ...
- Restful api测试插件Swagger-UI
现在多数的项目开发中,网站和移动端都需要进行数据交互和对接,这少不了使用REST编写API接口这种场景.例如我目前的工作,移动端交由了另一团队开发,不同开发小组之间就需要以规范和文档作为标准和协作基础 ...