tanha2013
شنبه 04 آبان 1392, 12:55 عصر
سلام.من یه سوال داشتم
چطوری میشه زمان اجرای یه برنامه رو در همون کد محاسبه کنیم و بعد از اینکه برنامه اجراش تموم شد زمان دقیقش رو به ما اعلام کنه:
مثلا واسه برج هانوی
include <iostream.h>
#include <conio.h>
void Move(int n, char source, char destination, char spare);
int count = 0;
int main()
{
const char PEG1 = 'A', // the three pegs
PEG2 = 'B',
PEG3 = 'C';
cout << "This program solves the Hanoi Towers puzzle.\n\n";
cout << "Enter the number of disks: ";
int numDisks; // the number of disks to be moved
cin >> numDisks;
cout << endl;
Move(numDisks, PEG1, PEG2, PEG3); // the solution
// getch();
return 0;
}
/
void Move(int n,
char source, char destination, char spare)
{
if (n <= 1) // anchor
cout << "Move the top disk from " << source
<< " to " << destination << endl;
else
{ // inductive case
Move(n-1, source, spare, destination);
Move(1, source, destination, spare);
Move(n-1, spare, destination, source);
}
}
چطوری میشه زمان اجرای یه برنامه رو در همون کد محاسبه کنیم و بعد از اینکه برنامه اجراش تموم شد زمان دقیقش رو به ما اعلام کنه:
مثلا واسه برج هانوی
include <iostream.h>
#include <conio.h>
void Move(int n, char source, char destination, char spare);
int count = 0;
int main()
{
const char PEG1 = 'A', // the three pegs
PEG2 = 'B',
PEG3 = 'C';
cout << "This program solves the Hanoi Towers puzzle.\n\n";
cout << "Enter the number of disks: ";
int numDisks; // the number of disks to be moved
cin >> numDisks;
cout << endl;
Move(numDisks, PEG1, PEG2, PEG3); // the solution
// getch();
return 0;
}
/
void Move(int n,
char source, char destination, char spare)
{
if (n <= 1) // anchor
cout << "Move the top disk from " << source
<< " to " << destination << endl;
else
{ // inductive case
Move(n-1, source, spare, destination);
Move(1, source, destination, spare);
Move(n-1, spare, destination, source);
}
}