考试中的艰巨任务:如何将 "IF" 语句“ "for nested" 循环”内的两个数组的总和复制到新数组
Monstrous task from the Exam : How to copy the sum of two arrays that are inside "IF" statement" that is inside "for nested" loop " to a new array
我需要帮助解决以下问题:
创建程序计算并打印以下方程的所有可能对(您可以在解决方案中使用 Math.sqrt(double) 但不能使用 Math.square )
n>=a>1
n>=b>1
其中 (a^2 + b^2 = c^2)
例如,如果 n = 4,那么我正在打印:
System.out.println(a = 3 , b =4 or a = 4 and b =3 ; 3^2 + 4^2=25; 我们找到了一对)
因为:
4>=4>1
4>=3>1
4^2 +3^2 =25
16+9=25 ,所以这是一对 a 和 b (a= 3 and b= 4 and a= 4 和 b= 3 是一样的,所以它只给了我一对 )
另一个例子是: n =144 ,然后 144>=144>1 和 144>=17>1
144^2 + 17^2 = 21025 ,所以我们找到了第一对 /.
n = 144 ,然后 144 >=143>1 和 144 >=24>1
143^2 + 24^2 = 21025 ,我们找到了第二对 /.
(n=144 给出了所有前面的数字对,而不仅仅是我提到的那些)
所以我从 Method main 开始并完成了一些事情,但最近无法复制两个数组的总和:
(cel1 and cel2_copy , their sum is equals to ) 到另一个数组 (tempofsum1) 正确。
public static void main(String[] args)
{
Scanner inp = new Scanner (System.in);
System.out.println(" Input the value of n ");
int n = inp.nextInt();
int counter = 0 ;
//counts the possible number of digits that gonna insert into my original array
int counter2 = 0;
//counts the number of times that our sum is calculated
int i , j,newroot; // newroot of 25 = 5 , root of 25 = 5.0
int sumofsquare = 0; //(a^2 + b^2)
int defaultsum =0; // (a+b)
int [] tempofsum2,tempofsum1 ;
for (i = 2; i<=n;i++)
{
//counts number of digits that i'm gonna insert into my original array
counter ++;
}
System.out.println("We have to sort out " + counter + " numbers inside our original array \n" );
int [] cel1 = new int [counter] ;// Our Original Array:
//all numbers in our array are equal to zero at the moment
for ( j = 0 , i =2 ; j<cel1.length ;j++ ,i++ )
{
//loop that insersts the numbers that I calculated
// i = 2 is first number in our array ,becasue numbers in the range of n > 1
cel1[j] = i ;
//number at the index of 0 is now 2 , this row is sorting our array numbers in the ascending order
System.out.println("Value of the original array at the index of # " +j+ " is " +cel1[j]);
}
System.out.println(" Our N is " + n );
// I'm creating my second array,
int [ ] cel2_copy = new int [cel1.length];
for (i = 0;i< cel1.length;i++)
{
//copying values from Original Array to this new .
cel2_copy [i] = cel1 [i] ;
}
//Our For "Loop", that calculates all possible summary of cel2_copy and cel1 arrays , pythagoras triplets, their roots
for (i = 0 ; i<cel1.length ;i++)
{
for ( j = 0 ; j < cel1.length; j++ )
//cel1[i] = a 和 cel2_copy[j] = b(在我们的毕达哥拉斯方程中)
sumofsquare = cel1[i]*cel1[i] + cel2_copy[j]*cel2_copy[j];
//summing our a^2 + b^2
defaultsum = cel1[i] + cel2_copy[j];
//same as previous row but a + b , not a^2 + b^2
double root = Math.sqrt (sumofsquare);
//calcutates the root of a^2 + b^2
newroot = (int ) root;
/*
cel1[i] = a , cel2_copy[j]=b
Our If statemenet that gonna check if ( n >= a >1 && n>= b > 1 )and if (our
integer root of a^2 +b^2 equals to its double value)if it is ,that means
that we have sum that can be rooted properly.
*/
if ((((n > cel1[i] || n == cel1[i]) && ( ( cel1[i] >1 )
&& (n > cel2_copy[j] || n == cel2_copy[j])
&& ( (cel2_copy[j] >1 )))
&&( newroot * newroot == root * root ))))
{
这是我的 Originofsum 数组,它获取 "cel1" 数组和 "cel2_copy" 数组
的摘要
originofsum [i] = sumofsquare ;
counter2 ++;
//counter2 calculates the number of times that we get summary`
System.out.println("The sum is calulated " +counter2 + " times " );
System.out.println(sumofsquare + " is sumofsquare " + " at the index of i = " +i +" and the index of J = " +j );
System.out.println("Our sum of a = " +cel1[i]+ " and b = " + cel2_copy[j] + " is a+b =" +defaultsum);
System.out.println("The double root of sumofsquare =" +root );
System.out.println("The int root of sumofsquare = " +newroot );
System.out.println("\n");
}// end of If
}//end for J
}// end for I
所以我在这里尝试将我的总和值从数组 "OriginofSum" 复制到 "tempofsum1",但这不起作用,
在控制台中(如果您在程序开始时按 4(4 是 N )打印以下数字:0 25
我们得到两个数字,因为我只有两个在 2 到 4 范围内的和(n = 4 和 n >=a>1 和 n>=b>1),给我正确的平方根是 a= 3 ,b= 4 and a= 4 and b= 3 (16+9=25) and (9+16=25)
所以我的数组有一次漏掉了数字 25。
如果你按 14 ,你得到总和 8 次:
0
25
25
169
100
0
100
225
所以现在我的数组错过了一次 169 和一次 225。(在它们的位置是 0 值)。
System.out.println("Sum is calculted " +counter2 + " times " );
tempofsum2 = new int [counter2];
tempofsum1 = new int [ counter2];
for ( i = 0 ; i <tempofsum1.length;i++ )
{
tempofsum1 [i] = originofsum[i];
System.out.println(originofsum[i]);
} // end of For i Loop
}// end of Main
}// end of Class
我很高兴得到你的帮助,但请记住这是一道测试题,所以我应该坚持一个基础。
提前谢谢你。)
如果您的目标只是找到与另一个整数形成勾股三元组的数字,这对我来说似乎非常简单:
private static void printPairs(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
long sum = (i * i) + (j * j);
double root = Math.sqrt(sum);
if (root == ((int) root)) {
System.out.printf("Found pair (%d, %d): %d^2 + %d^2 = %d^2\n",
i, j, i, j, (int) root);
}
}
}
}
示例输出:
Found pair (3, 4): 3^2 + 4^2 = 5^2
Found pair (4, 3): 4^2 + 3^2 = 5^2
这不需要任何数组或任何非常复杂的东西。我错过了什么吗?
我需要帮助解决以下问题:
创建程序计算并打印以下方程的所有可能对(您可以在解决方案中使用 Math.sqrt(double) 但不能使用 Math.square )
n>=a>1
n>=b>1
其中 (a^2 + b^2 = c^2)
例如,如果 n = 4,那么我正在打印:
System.out.println(a = 3 , b =4 or a = 4 and b =3 ; 3^2 + 4^2=25; 我们找到了一对) 因为: 4>=4>1
4>=3>1
4^2 +3^2 =25
16+9=25 ,所以这是一对 a 和 b (a= 3 and b= 4 and a= 4 和 b= 3 是一样的,所以它只给了我一对 )
另一个例子是: n =144 ,然后 144>=144>1 和 144>=17>1
144^2 + 17^2 = 21025 ,所以我们找到了第一对 /.
n = 144 ,然后 144 >=143>1 和 144 >=24>1
143^2 + 24^2 = 21025 ,我们找到了第二对 /.
(n=144 给出了所有前面的数字对,而不仅仅是我提到的那些)
所以我从 Method main 开始并完成了一些事情,但最近无法复制两个数组的总和: (cel1 and cel2_copy , their sum is equals to ) 到另一个数组 (tempofsum1) 正确。
public static void main(String[] args)
{
Scanner inp = new Scanner (System.in);
System.out.println(" Input the value of n ");
int n = inp.nextInt();
int counter = 0 ;
//counts the possible number of digits that gonna insert into my original array
int counter2 = 0;
//counts the number of times that our sum is calculated
int i , j,newroot; // newroot of 25 = 5 , root of 25 = 5.0
int sumofsquare = 0; //(a^2 + b^2)
int defaultsum =0; // (a+b)
int [] tempofsum2,tempofsum1 ;
for (i = 2; i<=n;i++)
{
//counts number of digits that i'm gonna insert into my original array
counter ++;
}
System.out.println("We have to sort out " + counter + " numbers inside our original array \n" );
int [] cel1 = new int [counter] ;// Our Original Array:
//all numbers in our array are equal to zero at the moment
for ( j = 0 , i =2 ; j<cel1.length ;j++ ,i++ )
{
//loop that insersts the numbers that I calculated
// i = 2 is first number in our array ,becasue numbers in the range of n > 1
cel1[j] = i ;
//number at the index of 0 is now 2 , this row is sorting our array numbers in the ascending order
System.out.println("Value of the original array at the index of # " +j+ " is " +cel1[j]);
}
System.out.println(" Our N is " + n );
// I'm creating my second array,
int [ ] cel2_copy = new int [cel1.length];
for (i = 0;i< cel1.length;i++)
{
//copying values from Original Array to this new .
cel2_copy [i] = cel1 [i] ;
}
//Our For "Loop", that calculates all possible summary of cel2_copy and cel1 arrays , pythagoras triplets, their roots
for (i = 0 ; i<cel1.length ;i++)
{
for ( j = 0 ; j < cel1.length; j++ )
//cel1[i] = a 和 cel2_copy[j] = b(在我们的毕达哥拉斯方程中)
sumofsquare = cel1[i]*cel1[i] + cel2_copy[j]*cel2_copy[j];
//summing our a^2 + b^2
defaultsum = cel1[i] + cel2_copy[j];
//same as previous row but a + b , not a^2 + b^2
double root = Math.sqrt (sumofsquare);
//calcutates the root of a^2 + b^2
newroot = (int ) root;
/*
cel1[i] = a , cel2_copy[j]=b
Our If statemenet that gonna check if ( n >= a >1 && n>= b > 1 )and if (our
integer root of a^2 +b^2 equals to its double value)if it is ,that means
that we have sum that can be rooted properly.
*/
if ((((n > cel1[i] || n == cel1[i]) && ( ( cel1[i] >1 )
&& (n > cel2_copy[j] || n == cel2_copy[j])
&& ( (cel2_copy[j] >1 )))
&&( newroot * newroot == root * root ))))
{
这是我的 Originofsum 数组,它获取 "cel1" 数组和 "cel2_copy" 数组
的摘要 originofsum [i] = sumofsquare ;
counter2 ++;
//counter2 calculates the number of times that we get summary`
System.out.println("The sum is calulated " +counter2 + " times " );
System.out.println(sumofsquare + " is sumofsquare " + " at the index of i = " +i +" and the index of J = " +j );
System.out.println("Our sum of a = " +cel1[i]+ " and b = " + cel2_copy[j] + " is a+b =" +defaultsum);
System.out.println("The double root of sumofsquare =" +root );
System.out.println("The int root of sumofsquare = " +newroot );
System.out.println("\n");
}// end of If
}//end for J
}// end for I
所以我在这里尝试将我的总和值从数组 "OriginofSum" 复制到 "tempofsum1",但这不起作用,
在控制台中(如果您在程序开始时按 4(4 是 N )打印以下数字:0 25
我们得到两个数字,因为我只有两个在 2 到 4 范围内的和(n = 4 和 n >=a>1 和 n>=b>1),给我正确的平方根是 a= 3 ,b= 4 and a= 4 and b= 3 (16+9=25) and (9+16=25)
所以我的数组有一次漏掉了数字 25。
如果你按 14 ,你得到总和 8 次: 0 25 25 169 100 0 100 225
所以现在我的数组错过了一次 169 和一次 225。(在它们的位置是 0 值)。
System.out.println("Sum is calculted " +counter2 + " times " );
tempofsum2 = new int [counter2];
tempofsum1 = new int [ counter2];
for ( i = 0 ; i <tempofsum1.length;i++ )
{
tempofsum1 [i] = originofsum[i];
System.out.println(originofsum[i]);
} // end of For i Loop
}// end of Main
}// end of Class
我很高兴得到你的帮助,但请记住这是一道测试题,所以我应该坚持一个基础。 提前谢谢你。)
如果您的目标只是找到与另一个整数形成勾股三元组的数字,这对我来说似乎非常简单:
private static void printPairs(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
long sum = (i * i) + (j * j);
double root = Math.sqrt(sum);
if (root == ((int) root)) {
System.out.printf("Found pair (%d, %d): %d^2 + %d^2 = %d^2\n",
i, j, i, j, (int) root);
}
}
}
}
示例输出:
Found pair (3, 4): 3^2 + 4^2 = 5^2
Found pair (4, 3): 4^2 + 3^2 = 5^2
这不需要任何数组或任何非常复杂的东西。我错过了什么吗?