C++ 以2的幂增加一个循环
我知道这是一件非常简单的事情,但我不知道如何通过2的幂增加循环。该程序正在比较插入排序和合并排序,n应以ie 2、4、8、16、32的幂递增。。。。这就是我所拥有的C++ 以2的幂增加一个循环,c++,C++,我知道这是一件非常简单的事情,但我不知道如何通过2的幂增加循环。该程序正在比较插入排序和合并排序,n应以ie 2、4、8、16、32的幂递增。。。。这就是我所拥有的 #include <iostream> #include <math.h> using namespace std; int main() { int a,n,b; double mergesort; double insertionsort; cout <<
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a,n,b;
double mergesort;
double insertionsort;
cout << "Enter a and b values ";
cin >> a >> b;
do
{
for(n=2; n=pow(n,++);
{
insertionsort = a*pow(n,2);
mergesort = b*n*log2(n);
}
cout <<"insertion sort= " << insertionsort << "merge sort= " <<mergesort;
cout <<"n" << n;
}while(insertionsort < mergesort);
cout << "Value of n that insertion beat merge sort is " << " n ";
}
该循环将在每次迭代中增加n的2次方,直至达到某个n_max:
谢谢,但这不起作用,计算不准确right@josh你在其他地方有个错误,因为我提供的循环肯定会按照你需要的顺序进行,2,4,8,16,…是的,对不起,你是对的,它是正确的,问题是我如何实现它,使其在插入排序小于合并排序时停止。@josh在循环中添加一个简单的条件,例如if insertionsort
for (int n = 2; n <= n_max; n *= 2) {
//...
}