C# 在C语言中,我得到了一个关于小猫问题的无效回报#

C# 在C语言中,我得到了一个关于小猫问题的无效回报#,c#,arrays,C#,Arrays,大家好。问题太长,我无法将其粘贴到此处,因此我将粘贴一个URL。我得到50%的无效回报。我加了一个检查,如果一只猫在那个牢房里收集食物/灵魂时遇到了僵局。我从早上6点开始处理这个问题,现在已经是上午11点了,我已经到了沮丧的地步。 简而言之,问题如下: 你的任务是计算Kitty收集的食物和灵魂,或者输出她已经僵持 在输入的第一行,您将收到编码者灵魂(“@”)、食物(“*”)和死锁(“x”)的位置作为字符串 在输入的第二行,您将收到Kitty的路径,它是一个字符串,整数由单个空格分隔。积极的意思是

大家好。问题太长,我无法将其粘贴到此处,因此我将粘贴一个URL。我得到50%的无效回报。我加了一个检查,如果一只猫在那个牢房里收集食物/灵魂时遇到了僵局。我从早上6点开始处理这个问题,现在已经是上午11点了,我已经到了沮丧的地步。

简而言之,问题如下: 你的任务是计算Kitty收集的食物和灵魂,或者输出她已经僵持

在输入的第一行,您将收到编码者灵魂(“@”)、食物(“*”)和死锁(“x”)的位置作为字符串

在输入的第二行,您将收到Kitty的路径,它是一个字符串,整数由单个空格分隔。积极的意思是向右移动,消极的意思是向左移动

起始位置将始终位于索引0处

最终结果要么是灵魂、食物和死锁计数,要么是一条字符串通知小猫死锁。该格式显示在zero测试和示例中

@-编码者灵魂的符号 *-食品符号 死锁的x符号

更多详情如下:

string input=Console.ReadLine();
int死锁=0;
字符串input2=Console.ReadLine();
字符串[]输出=输入2.Split(“”);
int位置=0;
int startposition=0;
int codersoulscollected=0;
int foodcollected=0;
int迭代次数=Math.Max(input.Length,output.Length);
bool[]isCollected=新bool[input.Length];
for(int i=0;i=input.Length)
{
startposition=startposition-input.Length;
}
字符a=输入[startposition];
if(a=='@'&(isCollected[startposition]==false))
{
codersoulscollected++;
isCollected[startposition]=真;
}
如果(a=='*'&&(isCollected[startposition]==false))
{
食品收集++;
isCollected[startposition]=真;
}
如果(a='x'&&(isCollected[startposition]==false))
{
死锁++;
如果(起始位置%2==0)
{
codersoulscollected--;
isCollected[startposition]=真;
}
其他的
{
食品收集--;
isCollected[startposition]=真;
}
}
else if(a='x'&&(isCollected[startposition]==true))
{
如果(起始位置%2==0)
{
codersoulscollected++;
}
其他的
{
食品收集++;
}
}
if(output.Length==i)
{
打破
}
position=int.Parse(输出[i]);
如果(食品收集量<0 | |编码器收集量<0)
{
WriteLine(“你僵持了,你这个贪婪的小猫!”);
WriteLine($“在死锁之前跳转:{i}”);
返回;
}
}
如果(食品收集>=0 | |编码者应收集>=0)
{
Console.WriteLine($“收集的编码器灵魂:{codersoulscollected}\r\n收集的日志:{foodcollected}\r\n加载锁:{deadlocks}”);
}

由于我手头有一些时间,我为您编写了一个简单的解决方案,它以面向对象的方式一步一步地引导您。希望你也能看到你的问题

这是你的猫。它可以在给定的路径上走给定的步数。它还收集食物和灵魂等

public class Cat
{
    /// <summary>
    /// Amount of collected coder souls.
    /// </summary>
    private int _coderSouls;

    /// <summary>
    /// Amount of collected food.
    /// </summary>
    private int _food;

    /// <summary>
    /// Amount of deadlocks collected.
    /// </summary>
    private int _deadlocks;

    /// <summary>
    /// Number of jumps before deadlocking.
    /// Starts from -1 because When we set the path
    /// The kitty starts from the 0th tile.
    /// </summary>
    private int _numberOfJumps = -1;

    /// <summary>
    /// If Cat can still move.
    /// </summary>
    private bool _deadLocked;

    /// <summary>
    /// Path to follow.
    /// </summary>
    private Path _path;

    /// <summary>
    /// Creates a Kitty
    /// </summary>
    /// <param name="path">Path for Kitty</param>
    public Cat(Path path)
    {
        SetPath(path);
    }

    /// <summary>
    /// Set the path for Kitty to follow.
    /// </summary>
    /// <param name="path">Path to follow.</param>
    private void SetPath(Path path)
    {
        _path = path;
        Walk(0);
    }

    /// <summary>
    /// Walks the Kitty with the given amount of steps.
    /// </summary>
    /// <param name="step">Amount of steps</param>
    /// <returns>If kitty can move any more.</returns>
    public bool Walk(int step)
    {
        // If Kitty is deadlocked it can not move any more
        if (_deadLocked)
        {
            return false;
        }
        // Walks the cat with the given step amount
        var got = _path.MoveToNext(step);
        // Increase the number of Jumps
        _numberOfJumps++;
        // Rule written in the question
        switch (got)
        {
            case ItemType.CoderSoul:
                _coderSouls++;
                break;

            case ItemType.Food:
                _food++;
                break;

            case ItemType.DeadLock:
                _deadlocks++;
                var isEven = _path.GetPosition() % 2 == 0;
                if (isEven)
                {
                    if (_coderSouls > 0)
                    {
                        _coderSouls--;
                        return true;
                    }
                    _deadLocked = true;
                    return false;
                }
                if (_food > 0)
                {
                    _food--;
                    return true;
                }
                _deadLocked = true;
                return false;
        }
        return true;
    }

    /// <summary>
    /// When Kitty finished moving, Gets Summary.
    /// </summary>
    /// <returns>Summary of movemebt</returns>
    public string Summarize()
    {
        return _deadLocked ? PrepareDeadLockMessage() : PrepareSummaryMessage();
    }

    /// <summary>
    /// Deadlock message.
    /// </summary>
    /// <returns>Deadlock message.</returns>
    private string PrepareDeadLockMessage()
    {
        return $"You are deadlocked, you greedy kitty!{Environment.NewLine}Jumps before deadlock: {_numberOfJumps}";
    }

    /// <summary>
    /// Normal finish.
    /// </summary>
    /// <returns>Normal finish.</returns>
    private string PrepareSummaryMessage()
    {
        return $"Coder souls collected: {_coderSouls}{Environment.NewLine}Food collected: {_food}{Environment.NewLine}Deadlocks: {_deadlocks}";
    }


}

它是否给出了你失败的细节?ie是什么输入导致它失败,为什么?同时查看
intiterations=Math.Max(input.Length,output.Length)-据我所知,这似乎与规范不符……因为这是对你技能的编码测试,我只给你一个提示。您正在使用c#一种面向对象的语言,但您没有使用任何一种。首先定义一个Cat类,然后为Path定义一个类。也许你的Cat类也应该有一些方法,比如向左走或向左走right@Chris不幸的是,它只说这是函数式编程的一部分。不是OOP的一部分。@grozdeto,那就更糟了。没有一个代码看起来像函数式编程,一切都是必需的。我帮助别人8个月后得到的快乐:)
public class Cat
{
    /// <summary>
    /// Amount of collected coder souls.
    /// </summary>
    private int _coderSouls;

    /// <summary>
    /// Amount of collected food.
    /// </summary>
    private int _food;

    /// <summary>
    /// Amount of deadlocks collected.
    /// </summary>
    private int _deadlocks;

    /// <summary>
    /// Number of jumps before deadlocking.
    /// Starts from -1 because When we set the path
    /// The kitty starts from the 0th tile.
    /// </summary>
    private int _numberOfJumps = -1;

    /// <summary>
    /// If Cat can still move.
    /// </summary>
    private bool _deadLocked;

    /// <summary>
    /// Path to follow.
    /// </summary>
    private Path _path;

    /// <summary>
    /// Creates a Kitty
    /// </summary>
    /// <param name="path">Path for Kitty</param>
    public Cat(Path path)
    {
        SetPath(path);
    }

    /// <summary>
    /// Set the path for Kitty to follow.
    /// </summary>
    /// <param name="path">Path to follow.</param>
    private void SetPath(Path path)
    {
        _path = path;
        Walk(0);
    }

    /// <summary>
    /// Walks the Kitty with the given amount of steps.
    /// </summary>
    /// <param name="step">Amount of steps</param>
    /// <returns>If kitty can move any more.</returns>
    public bool Walk(int step)
    {
        // If Kitty is deadlocked it can not move any more
        if (_deadLocked)
        {
            return false;
        }
        // Walks the cat with the given step amount
        var got = _path.MoveToNext(step);
        // Increase the number of Jumps
        _numberOfJumps++;
        // Rule written in the question
        switch (got)
        {
            case ItemType.CoderSoul:
                _coderSouls++;
                break;

            case ItemType.Food:
                _food++;
                break;

            case ItemType.DeadLock:
                _deadlocks++;
                var isEven = _path.GetPosition() % 2 == 0;
                if (isEven)
                {
                    if (_coderSouls > 0)
                    {
                        _coderSouls--;
                        return true;
                    }
                    _deadLocked = true;
                    return false;
                }
                if (_food > 0)
                {
                    _food--;
                    return true;
                }
                _deadLocked = true;
                return false;
        }
        return true;
    }

    /// <summary>
    /// When Kitty finished moving, Gets Summary.
    /// </summary>
    /// <returns>Summary of movemebt</returns>
    public string Summarize()
    {
        return _deadLocked ? PrepareDeadLockMessage() : PrepareSummaryMessage();
    }

    /// <summary>
    /// Deadlock message.
    /// </summary>
    /// <returns>Deadlock message.</returns>
    private string PrepareDeadLockMessage()
    {
        return $"You are deadlocked, you greedy kitty!{Environment.NewLine}Jumps before deadlock: {_numberOfJumps}";
    }

    /// <summary>
    /// Normal finish.
    /// </summary>
    /// <returns>Normal finish.</returns>
    private string PrepareSummaryMessage()
    {
        return $"Coder souls collected: {_coderSouls}{Environment.NewLine}Food collected: {_food}{Environment.NewLine}Deadlocks: {_deadlocks}";
    }


}
public class Path
{
    private readonly Item[] path;
    private int _currentIndex;

    public Path(string pathElements)
    {
        path = pathElements.Select(t => new Item(t)).ToArray();
        _currentIndex = 0;
    }

    public ItemType MoveToNext(int increase)
    {
        _currentIndex += increase;
        if (_currentIndex > path.Length)
        {
            _currentIndex -= path.Length;
        }
        if (_currentIndex < 0)
        {
            _currentIndex += path.Length;
        }
        return path[_currentIndex].Collect();
    }

    public int GetPosition()
    {
        return _currentIndex;
    }
}
public class Item
{
    /// <summary>
    /// Kitty already collected this cell or not?
    /// </summary>
    public bool IsCollected { get; private set; }

    /// <summary>
    /// ItemType in this cell
    /// </summary>
    public ItemType ItemType { get; }

    /// <summary>
    /// Represents a single item in each cell.
    /// </summary>
    /// <param name="c">Type of the item decided by char.</param>
    public Item(char c)
    {
        switch (c)
        {
            case '@':
                ItemType = ItemType.CoderSoul;
                break;

            case '*':
                ItemType = ItemType.Food;
                break;

            case 'x':
                ItemType = ItemType.DeadLock;
                break;
        }
    }

    /// <summary>
    /// Collect the item in this cell.
    /// </summary>
    /// <returns>The collected item.</returns>
    public ItemType Collect()
    {
        if (IsCollected)
        {
            return ItemType.None;
        }
        IsCollected = true;
        return ItemType;
    }
}
/// <summary>
/// The type of item located in each single cell.
/// </summary>
public enum ItemType
{
    None,
    CoderSoul,
    Food,
    DeadLock,
}
var cat = new Cat(new Path("x@*@*@*"));
var walkinOrder = "1 -1 -1 4";
var intOrder = walkinOrder.Split(' ').Select(int.Parse);
foreach (var step in intOrder) {
    if (cat.Walk(step) == false)
    {
    break;
    }
}
Console.WriteLine(cat.Summarize());