我知道这是一件非常简单的事情,但我无法弄清楚如何以2的幂增加循环。该程序正在比较插入排序和合并排序,n应该增加功率,即2,4,8,16,32 ....这就是我所拥有的
#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 ";
}
答案 0 :(得分:2)
此循环每次迭代的次数增加n
,最多为n_max
:
for (int n = 2; n <= n_max; n *= 2) {
//...
}