Click here to Skip to main content
15,917,638 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I write a program that show the jouzphose problem.
the in this problem we have some people that placed around a circle and killed.
for example we have 10 person (nop) and we begin from person number 2 (bp) this will happen:
5,8,1,4,9,3,10,7,2
and person number 6 will survived .

and my program is work like this:

10 =1010 (binary form of 10) and i start from 2

if i shift 1010 two times i will have 0110

and 0110 =6

my problem is here in the shift
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Roomi
{
    class Program
    {
        static void Main(string[] args)
        {
           
            int nop,bp,zero,a=0,j=0,k;
            string s;
           
            Console.WriteLine("Enter the number of persons:");
           s= Console.ReadLine();
           nop = Convert.ToInt32(s);
           Console.WriteLine("Enter the first person:");
          s= Console.ReadLine();
          bp = Convert.ToInt32(s);

          k = ((nop << bp)| (nop >> (8- bp)));  //problem is here !
       
            Console.WriteLine(k);

            
                Console.ReadKey();
    


        }
    }
}
Posted
Updated 7-Apr-15 6:08am
v4

1 solution

The issue is that you are using an int which is 16bit. That means that it's not 1010 but 0000000000001010. Your shift makes it x101000 which is 32+8 =40.

You need to create your own object that is [nop] long and rolls over. I suggest using a bool array and set it to true when the index is killed. You can shrink the circle by one by using linq to return only the people where the array item == false. Each time the counter rolls on you can use mod (%) to count on with a rollover.
Array.where(p=>!p).toarray()[counter%length]=true.

I am working on some sample code to show you but ran out of time. I wanted to give you advanced notice of my answer. I will post tomorrow (9:30 gmt)

EDIT:

Ok - I think this works.
There is another variable you can use. There is the total number of people, the starting position AND the number to move on by. Currently the example uses the same number for the starting position and the number to move on by, but you can adjust it fairly easily.

It's not the most efficient but I wanted to break it down so it was obvious what was going on. The Index set is where all the rollover happens so I have put over 5 lines what could be done on one.

The array is obviously zero indexed so I have adjusted the input and final output by 1 so you can use 'real case' numbers.

If this isn't correct then it's pretty damn close.

C#
class Program
{
    class DeathCircle
    {
        private int _moveByNumber;
        public DeathCircle(int peopleCount, int startingPosition)
            : this(peopleCount, startingPosition, startingPosition)
        {
        }

        public DeathCircle(int peopleCount, int startingPosition, int moveByNumber)
        {
            _people = new bool[peopleCount];
            Index = startingPosition-1;
            _moveByNumber = moveByNumber;

        }
        private readonly bool[] _people;

        private int _index;

        private int Index
        {
            get { return _index; }
            set
            {
                int indexOfAlive = (value) % PeopleLeftAlive;
                var setOfAllPeopleWithIndex = _people.Select((p, index) => new { p, index });
                var setOfPeopleAliveWithIndex = setOfAllPeopleWithIndex.Where(n => !n.p);
                var theNextVictim = setOfPeopleAliveWithIndex.Skip(indexOfAlive).First();
                _index = theNextVictim.index;
            }
        }

        public void Move()
        {
            Index += _moveByNumber;
        }

        public void Execute()
        {
            _people[Index] = true;
        }

        public int PeopleLeftAlive
        {
            get
            {
                return _people.Where(p => !p).Count();
            }
        }

        public int Survivor
        {
            get
            {
                return _people.Select((p, i) => new { p, i }).First(n => !n.p).i + 1;
            }
        }
    }

    static void Main(string[] args)
    {

        int nop, bp;
        string s;

        Console.WriteLine("Enter the number of persons:");
        s = Console.ReadLine();
        nop = Convert.ToInt32(s);
        Console.WriteLine("Enter the first person:");
        s = Console.ReadLine();
        bp = Convert.ToInt32(s);

        var dc = new DeathCircle(nop, bp);

        while (dc.PeopleLeftAlive > 1)
        {
            dc.Move();
            dc.Execute();
        }

        Console.WriteLine(dc.Survivor);

        Console.ReadLine();
    }
}



I'd give me 5 stars ^_^

Enjoy
 
Share this answer
 
v2
Comments
Akbar Fardi 7-Apr-15 13:27pm    
thanks my friend so much
Andy Lanng 8-Apr-15 4:14am    
A little early ^_^
Akbar Fardi 9-Apr-15 9:08am    
but this program work wrong .because if the input was 10 and 1 the output will be 5 but at your program show 1.
Andy Lanng 9-Apr-15 9:11am    
If 10 an 1 then my program does
start @ 1
+1 kill +1 kill +1 kill +1 etc so will end on 1 as everyone else is killed.

Do you mean start on 1 and go up by 2 or 3 each time?

then use the second constructor overload that takes peopleCount = 10, startingPosition = 1 & moveByNumber = 2

Does that help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900