bioshark Posted April 26, 2010 Report Share Posted April 26, 2010 Помогите пожалуста необходимо переделать прогаму из паскаля на С++. Вдолгу неостанусь. Вот код Procedure Effect( Var z:Vector; n:Integer ); Const k :LongInt=0; {Количество перестановок} Var p,d :Vector; pm, dm,zpm : Integer; i,m,w : Integer; begin; for i:=1 to n do begin z:=i; p:=i; d:=-1; end; d[1]:=0; m:=n+1; z[0]:=m; z[n+1]:=m; while m<>1 do begin { Печать перестановки } k:=k+1; Write(f,k,' ) `); for i:=l to n do Write(f,z,' `); WriteLn(f); m: =n ; while z [p [m] +d [m] ] >m do begin d[m]:=-d[m]; m:=m-l; end; pm:=p[m]; dm:=pm+d[m]; w:=z[pm]; z [pm] : =z [dm] ; z[dm]:=w; zpm:=z[pm]; w:=p[zpm]; p[zpm]:=pm; p[m]:=w; end; end;{Effect} Var {Main} z :Vector; n :Integer; {Длина перестановки} Hour,Minute,Second,SeclOO :Word; rHour,rMinute,rSecond,rSec100 :Word; delta :LongInt; begin Assign(f,'Effect.in' ) ; Reset(f); {Файл открыт для чтения} ReadLn(f,n); {Чтение длины перестановки} Close(f); Assign(f,'Effect.out' ) ; Rewrite(f); {Файл открыт для записи} GetTime(Hour,Minute,Second,Sec100); Effect(z,n); GetTime(rHour,rMinute,rSecond,rSec1 00); delta:=rHour-Hour; delta:=delta*60+rMinute-Minute; delta:=delta*60+rSecond-Second; delta:=delta*100+rSec100-Sec100; WriteLn(f,'Время счета=',delta div 100,'.', delta mod 100,' сек') ; Close(f); end. Link to comment Share on other sites More sharing options...
Тролль Posted April 27, 2010 Report Share Posted April 27, 2010 bioshark: #include<iostream>#include<fstream> #include<ctime>using namespace std;ifstream infile("effect.in");ofstream outfile("effect.out", ios_base::out); void effect(int z[],int n){long k=0; int *p=new int[30],*d=new int[30]; int pm,dm,zpm,i,m,w; for(i=1;i<=n;i++)d[z[i]=p[i]=i]=-1; d[1]=0; z[0]=z[n+1]=m=n+1; while(m!=1){ k++; outfile<<k<<" ) "; for(i=1;i<=n;i++)outfile<<z[i]<<" "; outfile<<endl; m=n; while(z[p[m]+d[m]]>m){d[m]=-d[m];m--;} pm=p[m]; dm=pm+d[m]; swap(z[pm],z[dm]);zpm=z[pm]; w=p[zpm]; p[zpm]=pm; p[m]=w;}delete[]p; delete[]d; }main(){int n; int *z=new int[30]; float start=clock(); if(!infile){cout<<"Error of opening the file!"; getchar(); exit(1);} infile>>n; infile.close(); effect(z,n); delete[]z; float end=clock(); cout<<(end-start)/CLOCKS_PER_SEC<<endl; getchar();} В файл effect.in для пробы советую для времени работы в несколько секунд записать число 9 или 10 (в зависимости от мощности компьютера). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now