给定 n 个元素的整数数组。任务是打印所有成对连续元素的乘积。 大小为 n 的数组的成对连续对是 (a[i],a[i 1]) 对于所有的范围从 0 到 n-2 示例 :
input : arr[] = {8, 5, 4, 3, 15, 20}
output : 40, 20, 12, 45, 300
input : arr[] = {5, 10, 15, 20}
output : 50, 150, 300
pg电子试玩链接的解决方案是遍历数组,计算并打印每对的乘积(arr[i],arr[i 1])。 以下是该方法的实现:
c
// c program to print the
// product of the consecutive elements.
#include
using namespace std;
// function to print pairwise
// consecutive product
void pairwiseproduct(int arr[], int n)
{
int prod = 1;
for (int i = 0; i < n - 1; i ) {
// multiply the alternate numbers
prod = arr[i] * arr[i 1];
printf(" %d ", prod);
}
}
// driver code
int main()
{
int arr[] = { 4, 10, 15, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
pairwiseproduct(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print the product
// of the consecutive elements.
import java .io.*;
class gfg
{
// function to print pairwise
// consecutive product
static void pairwiseproduct(int[] arr,
int n)
{
int prod = 1;
for (int i = 0; i < n - 1; i )
{
// multiply the alternate numbers
prod = arr[i] * arr[i 1];
system.out.print(prod " ");
}
}
// driver code
public static void main(string[] args)
{
int[] arr = { 4, 10, 15, 5, 6 };
int n = arr.length;
pairwiseproduct(arr, n);
}
}
// this code is contributed
// by anuj_67..
python 3
# python 3 program to print the
# product of the consecutive elements.
# function to print pairwise
# consecutive product
def pairwiseproduct( arr, n):
prod = 1
for i in range(n - 1) :
# multiply the alternate numbers
prod = arr[i] * arr[i 1]
print(prod, end = " ")
# driver code
if __name__=="__main__":
arr = [ 4, 10, 15, 5, 6 ]
n = len(arr)
pairwiseproduct(arr, n)
# this code is contributed
# by chitranayal
c
// c# program to print the product
// of the consecutive elements.
using system;
class gfg
{
// function to print pairwise
// consecutive product
static void pairwiseproduct(int[] arr,
int n)
{
int prod = 1;
for (int i = 0; i < n - 1; i )
{
// multiply the alternate numbers
prod = arr[i] * arr[i 1];
console.write(prod " ");
}
}
// driver code
public static void main()
{
int[] arr = { 4, 10, 15, 5, 6 };
int n = arr.length;
pairwiseproduct(arr, n);
}
}
// this code is contributed
// by akanksha rai(abby_akku)
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
output:
40 150 75 30
时间复杂度: o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处