如何解决while 循环的结尾非常有问题显示不正确的迭代
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main()
{
time_t program_time;
time(&program_time); //Time variable displays time
string name; //name variable,displays the name enter into cin
int a;
int b;
int c;
cout << "This is a C++ version of the Euclidean algorithm\n";
cout << "it is named after the Greek mathematician Euclid.\n";
cout << "What is your first name?\n";
cin >> name;
cout << "Hello "<< name <<",Please enter two numbers.\n";
cout << "Entry (1): "<< endl;
cin >> a;
cout << "You entered: " << a << endl;
cout << "Entry (2): " << endl;
cin >> b;
cout << "You entered: " << b << endl;
if(a < b) { //swaps entries
c = a;
a = b;
b = c;
}
while(a != b) {//control
int z = a;
do {
if(a != 1 && (!(a < b))) {
cout << "\n" << a << " minus "; //displays calculation
}
if(a < b) {
b -= a;
} else {
a -= b;
}
if(a != 1 && (!(a < b))) {
cout << b << " equals:\n" << " " << a << endl; //displays result of each calculation
}
} while(a < b);
if(a == 1) {
cout << " " << a << endl;
}
}
cout << "\n" << name << ",this is the greatest common divisor: " << a << endl;
cout << "\nWould you like to know the time?(yes/no) if not,this program is complete (case sensitive).\t";
string yes = "yes";
cin >> yes;
if(yes == "yes") {
cout << ctime(&program_time) << endl; //displays time
cout << "Good Bye!";
} else {
cout << "Good Bye!";
}
return 0;
}
例如,如果我输入 234 和 23 那么这就是最终输出显示的内容:
50 minus 23 equals:
27
27 minus 3 equals:
4
4 minus 1
stackoverflow,this is the greatest common divisor: 1
我无法弄清楚,我已经研究了几个小时。
我需要包括每次迭代的计算和结果。如果有更简单的方法来做到这一点,我会很感激并对其进行更改,但我希望有一些建议不会彻底改变我的程序
解决方法
你让这件事变得比你需要的要复杂得多。 Euclid 的算法完全是关于减法,并在结果为负时翻转顺序。下面演示了一个非常精简的版本:
#include <iostream>
#include <ctime>
#include <string>
int main()
{
unsigned int a,b;
if (std::cin >> a >> b && a > 0 && b > 0)
{
while (a != b && a > 1 && b > 1)
{
if (b < a)
{
std::cout << a << " - " << b << " = ";
a -= b;
std::cout << a << '\n';
}
else
{
std::cout << b << " - " << a << " = ";
b -= a;
std::cout << b << '\n';
}
}
std::cout << "Ans: " << a << '\n';
}
return 0;
}
一些事情。我们读取无符号整数(所以......没有负输入)。其次,不要费心在循环之前检测两者中的较大者;没关系。代码已经通过反转减法的参数来说明这一点。事实上,这就是算法的指针。输入测试值为 234 23 的示例运行:
样本(第一行是输入)
234
23
234 - 23 = 211
211 - 23 = 188
188 - 23 = 165
165 - 23 = 142
142 - 23 = 119
119 - 23 = 96
96 - 23 = 73
73 - 23 = 50
50 - 23 = 27
27 - 23 = 4
23 - 4 = 19
19 - 4 = 15
15 - 4 = 11
11 - 4 = 7
7 - 4 = 3
4 - 3 = 1
参考替代
您可以通过初始化对 a
和 b
的两个引用来进一步压缩它,较大的绑定到 lhs
引用,较小的绑定到 rhs
引用。从图形上看,它是有道理的,并且具有将代码减少到仅一个减法和报告部分的好处:
#include <iostream>
int main()
{
unsigned int a,b;
if (std::cin >> a >> b && a > 0 && b > 0)
{
while (a != b && a > 1 && b > 1)
{
unsigned int &lhs = (a < b) ? b : a;
unsigned int &rhs = (a < b) ? a : b;
std::cout << lhs << " - " << rhs << " = ";
lhs -= rhs;
std::cout << lhs << '\n';
}
std::cout << "Ans: " << a << '\n';
}
return 0;
}
,
关于信息的错误显示,以下代码是错误的,因为您正在减少值,然后进行与上述相同的检查。在某些时候(当 b 大于 a 时),您将开始跳过 'cout
if(a != 1 && (!(a < b))) {
cout << "\n" << a << " minus "; //displays calculation
}
if(a < b) {
b -= a;
} else {
a -= b;
}
if(a != 1 && (!(a < b))) {
cout << b << " equals:\n" << " " << a << endl; //displays result of each calculation
}
这里有一个小解决方案
if(a != 1 && (!(a < b))) {
cout << "\n" << a << " minus " << b << " equals:\n" << " " << a-b << endl;; //displays calculation
}
else
if(a != 1 && (a < b))
{
cout << "\n" << b << " minus " << a << " equals:\n" << " " << b-a << endl;; //displays calculation
}
if(a < b) {
b -= a;
} else {
a -= b;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。