问题描述
这里是问题
找到side1,side2和hypotenuse的所有Pythagorean Triples都不超过500.使用三重嵌套for循环来尝试可能性。
#include >
#include< iomanip>
使用namespace std;
int main()
{
int side1 = 0;
int side2 = 0;
int rightSide = 0;
cout<< 右侧<< (10)< Side1<< (10)< Side2<< ENDL;
(int i = 1; i {
side1 ++;
// cout<< side1<< ENDL;
(int a = 1; a {
side2 ++;
// cout<< 边2<< side2<< ENDL;
(int c = 1; c {
rightSide ++;
int rightSideSqr = rightSide * rightSide;
int side1Sqr = side1 * side1;
int side2Sqr = side2 * side2;
if(rightSideSqr == side1Sqr + side2Sqr)
{
cout<< rightSideSqr<< (15)< side1<< (10)< side2<< ENDL;
}
}
}
}
}
但它没有成功,好像是一个无限循环。请帮助。
请注意:我是C ++的新手,我自己研究它。而且,这不是一项家庭作业,我提出了问题陈述,因为这是表达问题的最好方式。
编辑
右侧Side1 Side2
运行成功(总时间:1s)
编辑2
工作代码
#include< iostream>
#include< iomanip>
使用namespace std;
int main()
{
// int side1 = 0;
// int side2 = 0;
// int rightSide = 0;
cout<< 右侧<< (10)< Side1<< (10)< Side2<< ENDL;
for(int i = 1; i {
// side1 ++;
// cout<< side1<< ENDL;
for(int a = 1; a {
// side2 ++;
// cout<< 边2<< side2<< ENDL;
(int c = 1; c {
// rightSide ++;
int rightSideSqr = c * c;
int side1Sqr = i * i;
int side2Sqr = a * a;
if(rightSideSqr ==(side1Sqr + side2Sqr))
{
cout<< rightSideSqr<< (15)<我 (10)<一个< ENDL;
}
}
}
}
}
这不是一个无限循环,它只是一个非常缓慢的有限循环。 I / O很慢 - 在中间循环的 cout
语句中打印500 * 500 = 250,000行文本,并打印出250,000行文本控制台非常非常慢。如果你删除了这个打印语句,它会执行得更快。其次,你的逻辑有一个错误。变量 side1
, side2
和 rightSide
永远不会重置在适当的时候为0,所以他们只是增加超出其预期的价值。尝试将它们重置为0,或只使用循环计数器,而不是像这样的额外变量。
Here is the question
Find all Pythagorean Triples for side1, side2 and hypotenuse all no longer than 500. Use a triple nested for loops that tries possibilities.
The below is my attempt
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int side1 = 0;
int side2 = 0;
int rightSide = 0;
cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;
for(int i=1;i<=500;i++)
{
side1++;
//cout << side1 << endl;
for(int a=1;a<=500;a++)
{
side2++;
//cout << "side 2 " << side2 << endl;
for(int c=1;c<=500;c++)
{
rightSide++;
int rightSideSqr = rightSide*rightSide;
int side1Sqr = side1*side1;
int side2Sqr = side2*side2;
if(rightSideSqr == side1Sqr+side2Sqr)
{
cout << rightSideSqr << setw(15) << side1 << setw(10) << side2 << endl;
}
}
}
}
}
But it gives no success, seems like an infinite loop. Please help.
Please Note: I am new to C++, I study it by my self. And, this is not an homework, I made the problem statement because it is the best way to express the issue.
EDIT
Right Side Side1 Side2
RUN SUCCESSFUL (total time: 1s)
EDIT 2
Working code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//int side1 = 0;
//int side2 = 0;
//int rightSide = 0;
cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;
for(int i=1;i<=500;i++)
{
//side1++;
//cout << side1 << endl;
for(int a=1;a<=500;a++)
{
//side2++;
//cout << "side 2 " << side2 << endl;
for(int c=1;c<=500;c++)
{
//rightSide++;
int rightSideSqr = c*c;
int side1Sqr = i*i;
int side2Sqr = a*a;
if(rightSideSqr == (side1Sqr+side2Sqr))
{
cout << rightSideSqr << setw(15) << i << setw(10) << a << endl;
}
}
}
}
}
It's not an infinite loop, it's just a very slow finite loop. I/O is slow -- you're printing out 500*500 = 250,000 lines of text in the cout
statement in the middle loop, and printing out 250,000 lines of text to the console is very, very slow. If you remove that print statement, it will execute much more quickly.
Secondly, you have an error in your logic. The variables side1
, side2
, and rightSide
never get reset to 0 at the appropriate times, so they just keep incrementing beyond their intended values. Try resetting them to 0, or just use the loop counters instead of extra variables like that.
这篇关于所有可能的毕达哥拉斯三元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!