如何在C ++(ofstream)中输出编译结果

时间:2013-06-18 11:25:51

标签: c++ ofstream

我的程序正在使用大量数字,我应该等待很多例如计算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。 怎么做?

0 个答案:

没有答案