C-找出a^2+;b^2
我手上有一个我无法解决的问题,因为我对C编程非常陌生。问题是找出100到999之间可以用(a^2+b^2)表示的所有整数 以下是我尝试过的:C-找出a^2+;b^2,c,numbers,C,Numbers,我手上有一个我无法解决的问题,因为我对C编程非常陌生。问题是找出100到999之间可以用(a^2+b^2)表示的所有整数 以下是我尝试过的: #include <stdio.h> #include <stdlib.h> int main(void) { int n,i,j,soln; for (n=100;n<1000;++n) { soln=0; for (i=0;i<100;++i) { for (j=i;j<
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n,i,j,soln;
for (n=100;n<1000;++n) {
soln=0;
for (i=0;i<100;++i) {
for (j=i;j<100;++j) {
if ((i^2 + j^2)==n) {
soln=1;
} else {
soln=0;
}
}
}
if (soln==1) printf("%d is a solution.\n",n);
}
return EXIT_SUCCESS;
}
#包括
#包括
内部主(空){
int n,i,j,soln;
对于(n=100;n代码中的两个问题:
1.使用*
将数字平方,而不是^
若要在C中对数字进行平方运算,请不要使用^
运算符(这是一个按位异或运算符)。请改用*
运算符:
if ((i*i + j*j)==n) {
soln=1;
} else {
soln=0;
}
2.在内部循环内移动if
条件
代码的另一个问题是覆盖了soln
值。应将条件移动到内部循环中:
for (n=100;n<1000;++n) {
soln=0;
for (i=0;i<100;++i) {
for (j=i;j<100;++j) {
if ((i*i + j*j)==n) {
soln=1;
} else {
soln=0;
}
// Condition here. When it was in the outer loop level,
// the soln=1 would be overwritten to the 0 in the next iteration
// and printf() wouldn't be called.
if (soln==1) {
printf("%d is a solution.\n",n);
// To avoid printing multiple times for the same n,
// break from the loop (loop for 'j').
// If you would want to print for every i,j pair that meets
// the criteria, remove this 'if' block, get rid of 'soln'
// and print in the if above (where you square the numbers).
break;
}
}
// We need to break the two loops, for i and j. This one is for
// the outer loop ('i').
if (soln == 1) break;
}
}
for(n=100;n您的代码有两个问题:
1.使用*
将数字平方,而不是^
若要在C中对数字进行平方运算,请不要使用^
运算符(这是一个按位异或运算符)。请改用*
运算符:
if ((i*i + j*j)==n) {
soln=1;
} else {
soln=0;
}
2.在内部循环内移动if
条件
代码的另一个问题是覆盖了soln
值。应将条件移动到内部循环中:
for (n=100;n<1000;++n) {
soln=0;
for (i=0;i<100;++i) {
for (j=i;j<100;++j) {
if ((i*i + j*j)==n) {
soln=1;
} else {
soln=0;
}
// Condition here. When it was in the outer loop level,
// the soln=1 would be overwritten to the 0 in the next iteration
// and printf() wouldn't be called.
if (soln==1) {
printf("%d is a solution.\n",n);
// To avoid printing multiple times for the same n,
// break from the loop (loop for 'j').
// If you would want to print for every i,j pair that meets
// the criteria, remove this 'if' block, get rid of 'soln'
// and print in the if above (where you square the numbers).
break;
}
}
// We need to break the two loops, for i and j. This one is for
// the outer loop ('i').
if (soln == 1) break;
}
}
<代码>(n=100;n < P>运算符>代码> ^ < /COD>指C、C++、C中的“强> XOR <强>(异或),不提升幂。
使用乘法*代替:
if ((i * i + j * j) == n) {
...
运算符>代码> ^ 指C、C++、C中的“强> > XOR <强>(异或),不提升幂。
使用乘法*代替:
if ((i * i + j * j) == n) {
...
我认为,如果找到解决方案,您需要退出循环:
for (i=0;i<100;++i) {
for (j=i;j<100;++j) {
if ((i*i + j*j)==n) {
soln=1;
break;
} else {
soln=0;
}
}
if (soln=1) {
break;
}
}
for(i=0;i如果找到解决方案,我认为您需要退出循环:
for (i=0;i<100;++i) {
for (j=i;j<100;++j) {
if ((i*i + j*j)==n) {
soln=1;
break;
} else {
soln=0;
}
}
if (soln=1) {
break;
}
}
用于(i=0;i到底是什么问题?另外,为了确保将一个数字平方,请使用i*i
而不是i^2
。由于某种原因,它不起作用。循环中似乎有漏洞。你们认为代码正确吗?@antikbd您的printf位于错误的位置。我在回答中修改了代码和输出。到底是什么问题?Al所以,为了确定一个数字的平方,请使用i*i
而不是i^2
。由于某种原因,它不起作用。循环中似乎有漏洞。你们认为代码正确吗?@antikbd您的printf位于错误的位置。我在回答中修改了代码和输出。