给定一个整数数组 arr 的大小 n ,任务是打印数组的所有子数组的产品。
示例:
输入: arr[] = {2,4 } t3】输出: 64 这里,子阵是【2】、【2,4】、【4】 积是所有子阵的 2,8,4 积= 64
输入: arr[] = {10,3,7} 输出: 27783000 这里,子阵是[10],[10,3],[10,3,7],[3,7],[7] 产品是所有子阵的 10,30,210,3,21,7 产品= 27783000
天真方法:一个简单的pg电子试玩链接的解决方案是生成所有子阵列并计算它们的乘积。
c
// c program to find product
// of all subarray of an array
#include
using namespace std;
// function to find product of all subarrays
void product_subarrays(int arr[], int n)
{
// variable to store the product
int product = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
for (int j = i; j < n; j ) {
for (int k = i; k <= j; k )
product *= arr[k];
}
}
// printing product of all subarray
cout << product << "\n";
}
// driver code
int main()
{
int arr[] = { 10, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// function call
product_subarrays(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find product
// of all subarray of an array
import java.util.*;
class gfg {
// function to find product of all subarrays
static void product_subarrays(int arr[], int n)
{
// variable to store the product
int product = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
for (int j = i; j < n; j ) {
for (int k = i; k <= j; k )
product *= arr[k];
}
}
// printing product of all subarray
system.out.print(product "\n");
}
// driver code
public static void main(string args[])
{
int arr[] = { 10, 3, 7 };
int n = arr.length;
// function call
product_subarrays(arr, n);
}
}
// this code is contributed by shivanisinghss2110
python 3
# python3 program to find product
# of all subarray of an array
# function to find product of all subarrays
def product_subarrays(arr, n):
# variable to store the product
product = 1;
# compute the product while
# traversing for subarrays
for i in range(0, n):
for j in range(i, n):
for k in range(i, j 1):
product *= arr[k];
# printing product of all subarray
print(product, "\n");
# driver code
arr = [ 10, 3, 7 ];
n = len(arr);
# function call
product_subarrays(arr, n);
# this code is contributed by code_mech
c
// c# program to find product
// of all subarray of an array
using system;
class gfg {
// function to find product of all subarrays
static void product_subarrays(int[] arr, int n)
{
// variable to store the product
int product = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
for (int j = i; j < n; j ) {
for (int k = i; k <= j; k )
product *= arr[k];
}
}
// printing product of all subarray
console.write(product "\n");
}
// driver code
public static void main(string[] args)
{
int[] arr = { 10, 3, 7 };
int n = arr.length;
// function call
product_subarrays(arr, n);
}
}
// this code is contributed by shivanisinghss2110
java 描述语言
output:
27783000
时间复杂度:o(n 3 )
辅助空间:0(1)
有效方法:一种有效方法是使用两个循环并在遍历子阵列时计算乘积。 以下是上述办法的实施情况:
c
// c program to find product
// of all subarray of an array
#include
using namespace std;
// function to find product of all subarrays
void product_subarrays(long long int arr[], int n)
{
// variable to store the product
long long int res = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
long long int product = 1;
for (int j = i; j < n; j ) {
product = product * arr[j];
res *= product;
}
}
// printing product of all subarray
cout << res << "\n";
}
// driver code
int main()
{
long long int arr[] = { 10, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// function call
product_subarrays(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find product
// of all subarray of an array
import java.util.*;
class gfg {
// function to find product of all subarrays
static void product_subarrays(int arr[], int n)
{
// variable to store the product
int res = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
int product = 1;
for (int j = i; j < n; j ) {
product = product * arr[j];
res *= product;
}
}
// printing product of all subarray
system.out.println(res "\n");
}
// driver code
public static void main(string args[])
{
int arr[] = { 10, 3, 7 };
int n = arr.length;
// function call
product_subarrays(arr, n);
}
}
// this code is contributed by abhithakur
python 3
# python3 program to find product
# of all subarray of an array
# function to find product of all subarrays
def product_subarrays(arr, n):
# variable to store the product
res = 1;
# compute the product while
# traversing for subarrays
for i in range(n):
product = 1
for j in range(i, n):
product *= arr[j];
res = res * product
# printing product of all subarray
print(res);
# driver code
if __name__ == '__main__':
arr = [ 10, 3, 7 ];
n = len(arr);
# function call
product_subarrays(arr, n);
# this code is contributed by princi singh
c
// c# program to find product
// of all subarray of an array
using system;
class gfg {
// function to find product of all subarrays
static void product_subarrays(int[] arr, int n)
{
// variable to store the product
int res = 1;
// compute the product while
// traversing for subarrays
for (int i = 0; i < n; i ) {
int product = 1;
for (int j = i; j < n; j ) {
product *= arr[j];
res = res * product;
}
}
// printing product of all subarray
console.writeline(res "\n");
}
// driver code
public static void main(string[] args)
{
int[] arr = { 10, 3, 7 };
int n = arr.length;
// function call
product_subarrays(arr, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
27783000
时间复杂度:o(n 2 )
辅助空间:0(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处