JAVA程式將整數數組的元素替換為其他元素的乘積
整數陣列是指所有元素都是整數類型的陣列。它也被稱為整數數組。
As per the problem statement we have to create an Integer array and display the array elements where all the array elements are the product of other elements of the array.
In this article, we will see how to replace array elements by the product of other array elements by using Java programming language.
To show you some instances −
Instance-1
int arr[] = { 2, 3, 1, 4, 6 } At Index-0 value will be = 3 * 1 * 4 * 6 = 72 At Index-1 value will be = 2 * 1 * 4 * 6 = 48 At Index-2 value will be = 2 * 3 * 4 * 6 = 144 At Index-3 value will be = 2 * 3 * 1 * 6 = 36 At Index-4 value will be = 2 * 3 * 1 * 4 = 24 So, the updated array elements are {72, 48, 144, 36, 24}
Instance-2
int arr[] = { 1, 3, 2, 4 } At Index-0 value will be = 3 * 2 * 4 = 24 At Index-1 value will be = 1 * 2 * 4 = 8 At Index-2 value will be = 1 * 3 * 4 = 12 At Index-3 value will be = 1 * 3 * 2 = 6 So, the updated array elements are {24, 8, 12, 6}
Algorithm
Algorithm-1
Step-1 − Declare and initialize an integer array.
第二步 - 找出所有陣列元素的乘積。
Step-3 − Divide the product value with the value of the respective index and replace the result. Continue this step till you reach the last array element.
步驟-4 − 列印更新後的陣列。
Algorithm-2
Step-1 − Declare and initialize an integer array.
Step-2 − Find the product of left sub array elements. The elements before the respective element to be replaced.
步驟-3 − 找出右子陣列元素的乘積。在要替換的元素之後的元素。
步驟-4 − 找出左子數組和右子數組的乘積值的和,並替換為結果值。
Step-5 − Continue Step-2, 3 and 4 till you reach the last array element.
Step-6 − 列印更新後的陣列。
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
To get the String representation of the respective array we can use the toString() method of Arrays class in Java.
Arrays.toString(A)
多種方法−
We have provided the solution in different approaches.
將各自的指數元素除以總產品值
透過找到左子數組和右子數組的產品值
讓我們逐一查看程式及其輸出。
Approach-1 − By Dividing the respective Index Element with the Total Product Value
#在這種方法中,我們將透過將總產品值除以要替換的對應元素來取代陣列元素。
Example
import java.util.Arrays; public class Main { public static void main(String args[]) { //Declare and initialize the array elements int arr[] = { 2, 3, 1, 4, 6 }; // Print the array elements System.out.println("Array elements are: "+Arrays.toString(arr)); //Declare an int variable 'product' to hold the product value int product = 1; //iterate the array elements and find the product of all elements for(int i=0;i<arr.length;i++) { product*=arr[i]; } //Divide the total product value with the array element to be replaced for(int i=0;i<arr.length;i++) { arr[i] = product/arr[i]; } //print the new array System.out.println("Updated array: "+Arrays.toString(arr)); } }
Output
#Array elements are: [2, 3, 1, 4, 6] Updated array: [72, 48, 144, 36, 24]
Approach-2 − By Finding the Product Values of Left and Right Sub Array
In this approach, we will replace the array element finding the sum of product of left subarray elements and product of right sub array elements.
Example
import java.util.Arrays; public class Main{ public static void main(String args[]) { int arr[] = {4,2,3,1}; int size=arr.length; int temp[]= new int[size]; int sum=1; System.out.println("Array elements are: "+Arrays.toString(arr)); for(int index=0; index<arr.length; index++) { int leftProduct=1; int rightProduct=1; int i=0; if(i<index) { for(i=0;i<index;i++) { leftProduct*=arr[i]; } } for(i=index+1;i<arr.length;i++){ rightProduct*=arr[i]; } sum=leftProduct*rightProduct; temp[index]=sum; } arr=temp; System.out.println("Updated array: "+Arrays.toString(arr)); } }
Output
#Array elements are: [4, 2, 3, 1] Updated array: [6, 12, 8, 24]
在本文中,我們探討如何使用不同的方法在Java中將陣列元素替換為其他陣列元素的乘積。
以上是JAVA程式將整數數組的元素替換為其他元素的乘積的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

集合X={a,b,c}的成對乘積可以定義為所有可能的集合對乘積的和。集合的成對為Y={a*a,a*b,a*c,b*b,b*c,c*c},其中乘積是可交換的。因此,集合X的成對乘積是集合Y的元素總和,即aa+ab+ac+bb+bc+cc。在數學術語中,可能的配對乘積的總和可以表示為:$$\mathrm{\displaystyle\sum\limits_{i=1,j=i}^{i\leqn,j\leqn}\:(i, j)=i\timej}$$問題陳述給定一個數字n。在範圍(1,n)內,包括n和1,找到成

給定一個數字N,我們必須將這個數字與其最大的數字相乘。如果沒有數字,則列印-1。就像我們用“153”初始化N,並且該數字中的最大的數字是5,否則結果將是153與5的乘積,即153 * 5 = 765,如果該數字有

在本文中,我們旨在探討關於多種程式語言中數組的最大公約數(GCD)的一個引人入勝的問題,並將重點放在C++上。我們將展示一種演算法方法,利用成對元素交換以及它們的乘積數量來驗證是否可以將GCD提高到1以上。此外,我們還將提供解決這個問題的其他方法,每種方法都有其語法定義。除了這些解決方案,我們還將呈現兩個完整的可執行程式碼,其中包含了這些方法。語法為了確保對後續程式碼範例有清晰的理解,我們必須在此之前評估和理解所使用的語法。 #include<iostream>#include<vecto

Introduction在Python中有不同類型的資料結構。元組是一種資料結構,它是一個有序的元素集合。元組也被稱為不可變的。不可變基本上意味著一旦創建,元組就無法修改。在下面的文章中,我們將了解在Python中尋找選擇元組鍵的乘積的程序。在需要對元組中特定元素進行乘法運算的問題中,這個程式非常有用。 UnderstandingtheProblemTupleisinitializedinasimilarwayweintializethelistinpython.Tuplestoresaseque

如何使用Go語言中的排序函數對整數陣列進行升序排序? Go語言是一門簡潔且有效率的程式語言,提供了一系列強大的標準函式庫函數來滿足不同的程式需求。其中,排序函數是Go語言中常用的功能之一。本文將介紹如何使用Go語言中的排序函數對整數陣列進行升序排序,並附帶相關的程式碼範例。在Go語言中,我們可以使用sort套件中的函數來進行排序運算。 sort套件提供了多種排序演算法,包括快速

資料結構操作現在已成為現代程式設計和計算中成功解決方案開發的重要方面。這是由於隨著時間的推移,這些結構所呈現的複雜性不斷增加。一個例子是執行交換操作以最小化包含在兩個數組中的最大數的總和,從而降低它們的整體值。在這篇文章中,我們討論了兩種使用C++完成這些任務的方法,同時根據不同觀點承認了這兩種方法的優點和缺點。文法為了有效地理解C++程式語言中的方法和程式碼,我們需要對基本語法有紮實的理解。這意味著要仔細研究與我們手頭上的主題相關的組件。 Arrays:intarrayName[size];Sort

簡介在數學中,我們看到了需要將數字乘以一定冪的問題。我們將透過一個例子來理解這個問題。想像一下,我們有一個數字列表,我們想要將相同的元素與其自身相乘一定次數。這就是「在清單中找出i^k的乘積」程式有用的地方。在數學領域,我們將這個過程稱為求冪。它有助於解決大多數數學計算和問題。我們將用python編寫程式碼。使用python更容易解決此類問題。 Python與所有程式語言不同,它包含特殊的工具,例如使程式設計世界變得更輕鬆且節省時間的程式庫。 Python中的函式庫包含有助於快速解決數學問題的函數。意味著無需從頭

遞歸是一種從同一個函數本身呼叫函數的技術。必須有一些基本或終止條件來結束遞歸呼叫。遞歸過程對於用更少的程式碼執行複雜的迭代求解非常有幫助,並且透過子操作尋找更容易的求解方法。在本文中,我們將討論在C++中執行兩個數字之間的乘積(乘法)的遞歸方法。首先我們了解基本原理、遞歸函數呼叫語法、演算法和原始碼。使用遞歸的乘法在高階語言中,有乘法運算子可以直接執行乘法。然而我們知道,乘法其實是重複的加法。所以A*B的結果就是A、B的重複相加次數,或是可以說B、A的重複相加次數。每當有重複時,我們可以使用遞歸來做
