如何解决此程序问题 [英] How Can I Fix This Programs Problem

查看:88
本文介绍了如何解决此程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个显示jouzphose问题的程序。

在这个问题中,我们有一些人围着一个圆圈杀死了。

例如我们有10个人(nop),我们从2号人(bp)开始,这将发生:

5,8,1,4,9,3,10,7,2

和6号人将幸存下来。



我的计划是这样的:



10 = 1010(10的二进制形式),我从2开始



如果我移动1010两次我会有0110



和0110 = 6



我的问题出在班次

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

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();
    


        }
    }
}

推荐答案

问题是您使用的是16位的int。这意味着它不是1010而是0000000000001010。你的移位使得x101000为32 + 8 = 40.



你需要创建自己的对象[nop]很久,翻身。我建议使用bool数组,并在索引被终止时将其设置为true。您可以使用linq将圆缩小一,仅返回数组项== false的人。每次计数器滚动时,您都可以使用mod(%)来计算翻转。

Array.where(p =>!p).toarray()[counter%length] = true。



我在工作在一些示例代码上向您展示但是时间不多了。我想提前通知你答案。我明天会发布(9:30 gmt)



编辑:



好​​的 - 我觉得这个有效。

您可以使用另一个变量。总人数,起始位置和要移动的人数。目前该示例使用相同的数字作为起始位置和要移动的数字,但您可以相当容易地调整它。



这不是最有效的但是我我想打破它,所以很明显发生了什么。索引设置是所有翻转发生的地方所以我已经放了5行可以在一个上完成。



数组显然是零索引所以我调整了输入和最终输出为1,所以你可以使用'真实案例'数字。



如果这不正确那么它非常接近。



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)



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.

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();
    }
}







我会给我5星^ _ ^



享受




I'd give me 5 stars ^_^

Enjoy


这篇关于如何解决此程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆