好吧,我的程序正在处理大量数字,我应该等待很多,例如计算 20!花费太多时间(计算所有排列)。如何输出前 20 个结果而不是全部 20 个?我试图在程序以图标形式运行时捕获它,但我不能。这是我的代码
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <vector>
#include <fstream>
using namespace std;
int X[100];
LARGE_INTEGER start, finish, freq;
int N;
int count_number;
vector<int> numb;
vector<int> cycle;
bool search (vector<int> source, int data)
{
for (int i=0; i<source.size(); i++)
if (source[i] == data)
return true;
return false;
}
void fill(int find)
{
find++;
int data = X[find-1];
int temp = find;
while (data != find){
temp = data;
data = X[temp-1];
numb.push_back(temp);
cycle.push_back(temp);
}
if (data == find) {
numb.push_back(find);
cycle.push_back(find);
}
}
void Swap(int a,int b)
{
int t=X[a];
X[a]=X[b];
X[b]=t;
}
void Generate(int k)
{
if (k==N)
{
for(int i=0;i<N;i++)
cout<<X[i]<<" ";
cout<<"Циклы: ";
for (int i=0; i<N; i++) {
if (!search(numb, i+1)) {
fill(i);
cout<<'(';
for (int i=0; i<cycle.size(); i++) {
cout<<cycle[i];
if(i != cycle.size()-1)
cout<<',';
}
cout<<") ";
cycle.clear();
}
}
numb.clear();
cout<<"\n";
count_number++;
}
else
{
for(int j=k;j<N;j++)
{
Swap(k,j);
Generate(k+1);
Swap(k,j);
}
}
}
int main()
{
setlocale(LC_CTYPE,"English");
cout<<"N=";
cin>>N;
QueryPerformanceFrequency( &freq );
QueryPerformanceCounter( &start );
for(int i=0;i<N;i++)
X[i]=i+1;
Generate(0);
cout<<endl;
cout<<endl;
QueryPerformanceCounter( &finish );
double time = (finish.QuadPart - start.QuadPart) / (double)freq.QuadPart;
cout<<"Algorith time:"<<time<<" \t seconds"<<endl;
cout<<"All permutation:"<<count_number<<endl;
FILE myfile;
cin.get();
getch();
return 0;
}
包括创建的文件 mmm。怎么做?