两个数组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(): 调用方 ...
随机推荐
- hdu 4315 Climbing the Hill 博弈论
题意:有n个人爬山,山顶坐标为0,其他人按升序给出,不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面的人,且不能和前面一个人在相同的位置.现在有一个 ...
- 2013 Multi-University Training Contest 1 I-number
水题,注意不要去掉前导0…… ;}
- 深入浅出Java并发包—指令重排序
前面大致提到了JDK中的一些个原子类,也提到原子类是并发的基础,更提到所谓的线程安全,其实这些类或者并发包中的这么一些类,都是为了保证系统在运行时是线程安全的,那到底怎么样才算是线程安全呢? Java ...
- linux入门教程(七) linux系统用户以及用户组管理
关于这部分内容,笔者在日常的linux系统管理工作中用到的并不多,但这并不代表该内容不重要.毕竟linux系统是一个多用户的系统,每个账号都干什么用,你必须了如指掌.因为这涉及到一个安全的问题. [认 ...
- HTML5入门十一---Canvas画布实现画图(二)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 对原生态jdbc程序中问题总结
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- 231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. 链接: http://leetcode.com ...
- 【转载】git/github初级运用自如
之前了解过github,并在上面看了一些项目的源代码,于是自己也在github上创建了账户,希望以后有机会也把自己的项目托管在上面去.但是前提你要先了解git/github,下面的内容是从我的好基友虫 ...
- js问题总结
.removeClass(" ") 移除一个css样式 $("#popUpBox i").removeClass("channel_icon" ...
- .net MVC APi调用
常用的调用方法为Get/Post Get方法: 服务器 public string Get(int id) { return "value"; } 这个直接在网页就可以测试,用 h ...