الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[مخالف - طلب حل : ]سؤال عن تمرين من تمارين How To Prog Vc# 2005

مغلق
بدأه sheneroo في 19 فبراير 2008 · 1 رد · 871 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

بعد اذنكو ياشباب فى بس تمرين من تمارين الموجوده فى كتاب how to prog واقف عليا ومش قادر اعرف هو قصده ايه فياريت تساعدونى فيه

التمرين اهه:

7.40 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems, and the Towers of Hanoi (see Fig. 7.27) is one of the most famous. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack has 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts.

Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers.

If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n 1 disks (hence the recursion) as follows:

Move n 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.

Move the last disk (the largest) from peg 1 to peg 3.

Move the n 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.

The process ends when the last task involves moving n = 1 disk (i.e., the base case). This task is accomplished by simply moving the disk, without the need for a temporary holding area.

Write an application to solve the Towers of Hanoi problem. Allow the user to enter the number of disks. Use a recursive Tower method with four parameters:

the number of disks to be moved,

the peg on which these disks are initially threaded,

the peg to which this stack of disks is to be moved, and

the peg to be used as a temporary holding area.

Your application should display the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your application should print the following series of moves:

1 --> 3 (This notation means "Move one disk from peg 1 to peg 3.")

1 --> 2

3 --> 2

1 --> 3

2 --> 1

2 --> 3

1 --> 3

#2

تأسف إدارة الفريق العربي لغلق الموضوع وذلك لمخالفته قوانين المشاركات، فقرة طلب حل الواجبات والتمارين وأعمال السنة.

قواعد طرح المشاركات

/index.php?showtopic=29343

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…