Fox and rabbit problems

Source: Internet
Author: User
Just read the programming question:

There are 10 holes around the top of the hill. A rabbit and a fox each have a hole. The fox wants to eat rabbits. The rabbit said to the fox, "you can eat me, but you must find me. I am hiding in these ten holes. If you start from the 10th hole, you will first go to the 1st hole to find me. The second time you go to the second hole to find me, and the third time you go to the second hole to find me, ......, And so on. If you can find me, you can have a full meal .". The Fox agreed, but the fox entered 1000 times from morning till night and still could not find the rabbit. Ask the rabbit to find out which hole to hide.


Below is my C #Algorithm:


Using system;

Namespace consoleapplication3

{

/// <Summary>

/// Summary of class1.

/// </Summary>

Class class1

{

/// <Summary>

/// Application Program .

/// </Summary>

// [Stathread]

Static bool [] rabbitincave = new bool [10]; // There Are No rabbits in 10 holes

Static void setrabbitincave (INT cavenumber) // you can specify which hole contains rabbits.

{

For (INT I = 0; I <10; I ++)

{

Rabbitincave [I] = false;

}

Rabbitincave [cavenumber] = true;

}

Static int getcavenumber (INT biggercavenumber) // get the correct hole number

{

Return biggercavenumber % 10;

}

Static bool testcave () // test whether rabbit can be found within 1000 times

{

Int stepadd = 0;

Int cavenumber = 9;

For (INT I = 0; I <= 1000; I ++)

{

If (rabbitincave [cavenumber])

Return true;

Stepadd + = 1;

Cavenumber = getcavenumber (cavenumber + stepadd );

}

Return false;

}

Static void main (string [] ARGs)

{

//

// Todo: add Code To start the application

//

/* There are 10 holes around the top of the hill. A rabbit and a fox each have a hole.

* The Fox wants to eat rabbits. The rabbit said to the fox, "you want to eat me.

* You must find me. I am hiding in these ten holes. You set out from the 10th hole,

* First, find me in the No. 1 hole, second, and third, and second

* I ,......, And so on. If you can find me, you can have a full meal .".

* The Fox agreed, but the fox made 1000 attempts from morning till night.

* To rabbit. Ask the rabbit to find out which hole to hide.

**/

For (INT I = 0; I <10; I ++)

{

Setrabbitincave (I );

If (! Testcave ())

Console. writeline ("Rabbit may be in the hole {0. ", I + 1 );

}

}

}

}

Run and get the result. The rabbit may be located in holes 2, 4, 7, and 9.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.