没有进入while循环
Not entering while loop?
[EDIT] -没关系,这是一个非常愚蠢的问题
我目前正在尝试创建一个程序,它基本上会告诉我,对于给定的a
, b
和n
(在这种情况下,连续的2次方),插入排序需要比归并排序更长的时间。这是我到目前为止所拥有的:
int comparisonSort()
{
//prompt user for values of a and b
int a = 0;
int b = 0;
cout << "Enter the value for a" << endl;
cin >> a;
cout << "Enter a value for b" << endl;
cin >> b;
double insertionSortTime = 0;
double mergeSortTime = 0;
double n = 2;
cout << outside while loop" << endl; //check
while (insertionSortTime < mergeSortTime)
{
cout << "inside while loop" << endl; //check
//compute the insertion and merge sort execution times
insertionSortTime = a*n*n;
mergeSortTime = b*n*log2(n);
cout << "is = " << insertionSortTime << " ms = " << mergeSortTime << endl;
n = pow(n, 2); // n^2
}
cout << "value of n that insertion sort beat merge sort is: " << n << endl;
return 0;
}
当我运行这个时,我得到:
Enter the value for a
8
Enter a value for b
64
outside while loop
value of n that insertion sort beat merge sort is: 2
我不知道为什么while循环没有被执行…抱歉,如果这看起来像一个非常简单的问题,但我是新的c++和任何帮助将非常感激,谢谢!!
中的条件
while (insertionSortTime < mergeSortTime)
当insertionSortTime
和mergeSortTime
均为零时,第一次迭代为false
。这就解释了为什么循环永远不会被执行。
也许你想用:
while (insertionSortTime <= mergeSortTime)
这是因为你有insertionSortTime = 0
和mergeSortTime = 0
,而循环的条件是insertionSortTime < mergeSortTime
。
当然0
不是< 0
,所以它永远不会进入循环。
更改为<=
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 在 while 循环中使用 pow() 函数 C++
- C++ 制作/命名不初始化的向量(在 while 循环中)
- std::getline没有在while循环中重新请求用户输入