#include "stdafx.h"
#include
#include
#include
using namespace std;
const int N =6;
void ptr(int *a, int n, int x)
{
int i;
for(i=n-1; i>=0&&x!=0; i--)
{
a[i] = x%2;
x /= 2;
}
for(; i>=0; i--)
{
a[i] = 0;
}
}
template
void subset(T *s, int n)
{
int *e=new int[n];
long t= pow(2.0,n);
for(int j=1; j {
ptr(e, n, j);
for(int k=0; k {
if(e[k]==1)
cout << s[k];
}
cout << '\t';
}
cout << "empty";
delete[]e;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t start,finish;
char arr[N];
char g;
cout << "please enter " << N << "number:";
for(int i=0; i {
cin >> g;
arr[i] = g;
}
start=clock();
subset(arr,N);
finish=clock();
cout << endl;
cout << "It has took "<<(finish-start)/1000 << "seconds"<< " to executive the program" < return 0;
}
--------------------next---------------------
楼上的代码超强,楼上的同学一定是个高人。
事实上我看不懂那篇代码,但我测试了,可以。不仅可以,效率是绝对一流的。
不过如果楼主看不懂的话,可以参考我写的,呵呵。
#include
#include
#include
#define _TCHAR char
#define _tmain main
using namespace std;
const int N =6;
void ptr(int *a, int n, int x)
{
int i;
for(i=n-1; i>=0&&x!=0; i--)
{
a[i] = x%2;
x /= 2;
}
for(; i>=0; i--)
{
a[i] = 0;
}
}
template
void subset(T *s, int n)
{
int *e=new int[n];
long t= pow(2.0,n);
for(int j=1; j{
ptr(e, n, j);
for(int k=0; k{
if(e[k]==1)
cout << s[k];
}
cout << '\t';
}
cout << "empty";
delete[]e;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t start,finish;
char arr[N];
char g;
cout << "please enter " << N << "number:";
for(int i=0; i{
cin >> g;
arr[i] = g;
}
start=clock();
subset(arr,N);
finish=clock();
cout << endl;
cout << "It has took "<<(finish-start) << "minisecond"<< " to executive the program" <return 0;
}
我的代码执行效率跟楼上比起来可差得多了,平均50多毫秒吧。楼上的可是15毫秒呵。
--------------------next---------------------
晕,上面贴错代码了。那是楼上同学的。刚才把它放进自己的编译器里调试。。。忘了,怎么修改回来,晕@@@
下面才是我写的:
#include
#include
using namespace std;
#include
#define N 6
template
void Interpretation (T psubset,int total)
{
cout << " { ";
for(int i=0;i<=total;i++)
{
if(psubset[i]!=-1)
cout << psubset[i] << "," ;
}
cout << " } ";
}
template
void OneStep(T* psubset,int total,int level)
{
static bool First=true;
static T* pT;
if(First)
{
pT=new T[total];
First=false;
}
pT[level]=-1;
if(level==total-1)
Interpretation(pT,level);
else
OneStep(psubset,total,level+1);
pT[level]=psubset[level];
if(level==total-1)
Interpretation(pT,level);
else
OneStep(psubset,total,level+1);
if(level==0)
delete [] pT;
}
int main()
{
clock_t begin,end;
int num[N];
cout << "Input " << N << " numbers: " << endl;
for(int i=0;i cin >> num[i];
begin=clock();
OneStep(num,N,0);
end=clock();
cout << "\n";
cout << "It executed in " << end - begin << " miniseconds. " << endl;
return 0;
}
--------------------next---------------------
阅读(1031) | 评论(0) | 转发(0) |