参差不齐(锯齿状)数组中一行的乘积
Product of a row within a Ragged (jagged) Array
本学期我的 CS 课程中有一道挑战题,是上学期的复习题,但问题是:"Given a ragged array, find if any row within the array has a product of 48 and if so return that row number. If no rows contain a product of 48, return -1." 这是我目前的题目。
public class RaggedProducts {
public static void main(String[] args) {
int[][] a = {{3, 9},
{100},
{1, 4, 6, 2},
{23, 3, 8}};
System.out.println(product(a));
}
/*
* finds if parameter array has a row which the numbers within that row
* have a product of 48, returns row integer value. If no rows have a
* product of 48, returns -1.
*/
public static int product(int[][] ragged) {
int product = 1;
for (int i = 0; i < ragged.length; i++) {
// EDIT: I got my answer, I had to add "product = 1" here
for (int j = 0; j < ragged[i].length; j++) {
product *= ragged[i][j];
if (i > 0) { // what to do here to reset product value whenever "i" changes
// "i > 0" is just a placeholder, I know it doesn't make sense
product = 1;
}
if (product == 48) {
return i; // I want to return the value 2
}
}
}
return -1;
}
}
如果我将锯齿状数组中的第一行设置为具有 48 的乘积,则程序可以运行,但我对如何在行更改后将乘积字段重置为 1 感到困惑。如有任何帮助,我们将不胜感激!
您似乎对迭代二维数组的工作原理感到困惑。你的两个循环基本上是这样做的:
for each row i do {
//you can do something here too!
for each cell j in row i do {
...
}
//...and here!
}
因此,如果您想将 product 重置为开始迭代下一行的时间,只需在代码中的第二个 for 循环之前执行即可。
此外,如果乘积等于 或大于 48,现在您的程序 returns 一行。据我了解,这不是您想要的。因此,您应该检查是否相等 after 迭代一行中的单元格(在第二个 for 循环之后但在第一个 for 循环内)。
顺便说一句,java 支持与我的伪代码更相似的语法。你可以写
for (int[] row : ragged) {
for (int value : row) {
//do something
}
}
public static int product(int[][] ragged) {
int product = 1;
for (int i = 0; i < ragged.length; i++) {
for (int j = 0; j < ragged[i].length; j++) {
product *= ragged[i][j];
}
if (product == 48) {
return i; // I want to return the value 2
}else{
product=1;
}
}
return -1;
}
本学期我的 CS 课程中有一道挑战题,是上学期的复习题,但问题是:"Given a ragged array, find if any row within the array has a product of 48 and if so return that row number. If no rows contain a product of 48, return -1." 这是我目前的题目。
public class RaggedProducts {
public static void main(String[] args) {
int[][] a = {{3, 9},
{100},
{1, 4, 6, 2},
{23, 3, 8}};
System.out.println(product(a));
}
/*
* finds if parameter array has a row which the numbers within that row
* have a product of 48, returns row integer value. If no rows have a
* product of 48, returns -1.
*/
public static int product(int[][] ragged) {
int product = 1;
for (int i = 0; i < ragged.length; i++) {
// EDIT: I got my answer, I had to add "product = 1" here
for (int j = 0; j < ragged[i].length; j++) {
product *= ragged[i][j];
if (i > 0) { // what to do here to reset product value whenever "i" changes
// "i > 0" is just a placeholder, I know it doesn't make sense
product = 1;
}
if (product == 48) {
return i; // I want to return the value 2
}
}
}
return -1;
}
}
如果我将锯齿状数组中的第一行设置为具有 48 的乘积,则程序可以运行,但我对如何在行更改后将乘积字段重置为 1 感到困惑。如有任何帮助,我们将不胜感激!
您似乎对迭代二维数组的工作原理感到困惑。你的两个循环基本上是这样做的:
for each row i do {
//you can do something here too!
for each cell j in row i do {
...
}
//...and here!
}
因此,如果您想将 product 重置为开始迭代下一行的时间,只需在代码中的第二个 for 循环之前执行即可。
此外,如果乘积等于 或大于 48,现在您的程序 returns 一行。据我了解,这不是您想要的。因此,您应该检查是否相等 after 迭代一行中的单元格(在第二个 for 循环之后但在第一个 for 循环内)。
顺便说一句,java 支持与我的伪代码更相似的语法。你可以写
for (int[] row : ragged) {
for (int value : row) {
//do something
}
}
public static int product(int[][] ragged) {
int product = 1;
for (int i = 0; i < ragged.length; i++) {
for (int j = 0; j < ragged[i].length; j++) {
product *= ragged[i][j];
}
if (product == 48) {
return i; // I want to return the value 2
}else{
product=1;
}
}
return -1;
}