转自: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 ;
}
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. void pr_arr(int *s,char len)
    4. {
    5. while(len--)
    6. {
    7. printf("%d \n",*s++);
    8. }
    9. }
    10. int main()
    11. {
    12. int a[]={2,3,7,23,6,5,1,23,89,23};
    13. int *b=(int*)malloc(sizeof(a));
    14. b[0]=1;
    15. int len=sizeof(a)/sizeof(*a);
    16. int j,i;
    17. for( i=1;i<len;i++)
    18. {
    19. b[i]=b[i-1]*a[i-1];
    20. }
    21. for(j=len-2;j>=1;j--)
    22. {
    23. b[0]*=a[j+1];
    24. b[j]*=b[0];
    25. }
    26. b[0]*=a[1];
    27. pr_arr(b,len);
    28. return 0;
    29. }

两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];的更多相关文章

  1. js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的

    题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...

  2. OpenCL入门:(二:用GPU计算两个数组和)

    本文编写一个计算两个数组和的程序,用CPU和GPU分别运算,计算运算时间,并且校验最后的运算结果.文中代码偏多,原理建议阅读下面文章,文中介绍了OpenCL相关名词概念. http://opencl. ...

  3. 函数指针的返回值是指针数组,数组里放的是int;函数指针的返回值是指针数组,数组里放的是int指针

    函数指针的返回值是指针数组,数组里放的是int 函数指针的返回值是指针数组,数组里放的是int指针 #include <stdio.h> #include <stdlib.h> ...

  4. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  6. [C++]for同时遍历两个数组

    C++11同时遍历两个数组 #define for2array(x,y,xArray,yArray) \ for(auto x=std::begin(xArray), x##_end=std::end ...

  7. PHP两个数组相加

    在PHP中,当两个数组相加时,会把第二个数组的取值添加到第一个数组上,同时覆盖掉下标相同的值: <?php $a = array("a" => "apple& ...

  8. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  9. array_intersect() php筛选两个数组共有的元素

    我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...

随机推荐

  1. gulp-css-spriter 雪碧图合成

    一.配置 gulp的雪碧图功能没有grunt那么强大,但是类似功能也支持,功能稍微弱一些,但是也很棒 npm地址:https://www.npmjs.com/package/gulp-css-spri ...

  2. C Primer Plus 第5章 运算符、表达式和语句 编程练习

    1. #include <stdio.h> ; int main(void) { int min, hour, lmin; printf("请输入分钟数: \n"); ...

  3. linux设置和查看环境变量的方法

    1.    显示环境变量HOME $ echo $HOME /home/redbooks 2.    设置一个新的环境变量hello $ export HELLO="Hello!" ...

  4. TortoiseSVN文件夹及文件图标不显示解决方法(转发)

    地址:http://blog.csdn.net/lishehe/article/details/8257545 由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能 ...

  5. MIT算法导论——第二讲.Solving Recurrence

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  6. PowerDesigner中转换物理模型时的命名转换

    原文:PowerDesigner中转换物理模型时的命名转换 最近在使用PowerDesigner建模数据库,在使用中积累了一些遇到的问题和解决办法,记录下来,希望对遇到同样问题的朋友有所帮助. 早 期 ...

  7. VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)

    原文地址: VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)-软件开发-鸡啄米 http://www.jizhuomi.com/software/139.html   上一讲中鸡 ...

  8. java 调用 phantomjs

    java 调用 phantomjs 2014-11-21 13:55 2034人阅读 评论(2) 收藏 举报  分类: phantomjs(2)  日前有采集需求,当我把所有的对应页面的链接都拿到手, ...

  9. 海明距离hamming distance

    仔细阅读ORB的代码,发现有很多细节不是很明白,其中就有用暴力方式测试Keypoints的距离,用的是HammingLUT,上网查了才知道,hamming距离是相差位数.这样就好理解了. 我理解的Ha ...

  10. 即时通信Spark安装和配置

    spark:Cross-platform real-time collaboration client optimized for business and organizations.Spark i ...