两个数组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(): 调用方 ...
随机推荐
- 在实体注解OneToMany时,要加上mappedby,避免产生中间表。
在实体注解OneToMany时,要加上mappedby,避免产生中间表.
- **CI两种方式查询所返回的结果数量
区别:第一个是有条件的:第二个没有条件 $this->db->count_all_results(); 允许你获得某个特定的Active Record查询所返回的结果数量.可以使用Acti ...
- Android AlarmManager的取消
取消alarm使用AlarmManager.cancel()函数,传入参数是个PendingIntent实例. 该函数会将所有跟这个PendingIntent相同的Alarm全部取消,怎么判断两者是否 ...
- Qt4升级Qt5注意问题
Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...
- Ubuntu下编译运行Kamailio
kamailio----配置没有成功,这个文档过几天删除,因为这个项目的文档非常少,而且qq群里的人也不活跃,现在正在研究Freeswitch,如果能够满足,就不研究这个了,这篇文档会删除. Kama ...
- HTML5入门4---HTML5 与 HTML4 同一网页的不同写法
HTML4写法 css: body { font-family: "Lucida Sans Unicode", "Lucida Grande", Verdana ...
- Photoshop CS4序列号过期的问题
1)在网络上搜寻一些PS CS4序列号: 如1330-1221-6824-4838-0308-6823,1330-1283-7461-4574-7002-2504,1330-1795-2880-537 ...
- java c# 加密与解密对照
原文 java c# 加密与解密对照 最近一直烦恼,java , c# 加密的不同,然后整理了一下,留个备份的轮子: 其中在 java.c#加密转换时,最重要的是 IV 的确定,我常常用如下方法使得j ...
- Quartz任务调度快速入门
Quartz任务调度快速入门 概述 了解Quartz体系结构 Quartz对任务调度的领域问题进行了高度的抽象,提出了调度器.任务和触发器这3个核心的概念,并在org.quartz通过接口和类对重要的 ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...