小编Ken*_*Kin的帖子

如何在不存储卡片的情况下实施经销商类别?

  • 即使只有52张牌,permutationIndex我在" 解释"部分中描述的位置也是一个很大的数字; 它是一个数字52!,需要29个字节才能存储.

    因此,我不知道计算permutationIndex大范围的简单方法,并以最小成本存储索引,或者也可以计算.我在想这个问题的解决方案是三种算法:

    1. 其计算正确的算法permutationIndex实现Dealing方法

    2. 其计算正确的算法permutationIndex实现Collect方法

    3. 以最小成本存储(或计算)的算法permutationIndex

  • 说明

    我本来试图实现一个整数手柄发生器从一系列int.MinValeint.MaxValue使用排列.

    因为范围真的很大,所以我从实现开始一个Dealer52卡的类,它实际上不存储像hashset或数组的卡片,甚至不想随机(初始除外).

    对于给定范围的序数,我认为其中一个完整排列的每个序列都有一个索引,并将其命名permutationIndex.我使用索引来记住它是哪个排列而不是真正存储序列.序列是卡片组的可能顺序之一.

    这里有一个动画图形模拟示例,以显示我的想法. 在此输入图像描述

    每次我发卡时,我都会更改permutationIndexdealt(发卡的数量),我知道哪些卡是那些卡,哪些卡还在手中.当我收回发回的卡片时,我会知道卡片号码,并将其放在顶部,它也会成为下次处理的卡片.在动画中,colleted卡号.


有关更多信息,请按以下步骤

  • 代码说明

    Dealer仅有三个3的概念样本类如下.代码是用编写的,我也在考虑任何与解决方案.

    以下是示例代码的一些描述

    • 通过这种方法Dealing(),我们获得了许多处理的卡片.它总是返回最右边的数字(与数组相关),然后通过更改将其左边的数字(比如下一个可用的数字)滚动到最右边的位置permutationIndex.

    • 该方法 …

c# language-agnostic algorithm math permutation

6
推荐指数
1
解决办法
1593
查看次数

物理磁盘大小不正确(IoCtlDiskGetDriveGeometry)

我使用下面的代码来获取物理磁盘大小,但返回的大小不正确.我用其他工具检查了尺寸.

以下代码报告

总磁盘空间:8.249.955.840字节

它应该是

总磁盘空间:8.254.390.272字节

如何检索实际/正确的物理磁盘大小?在USB驱动器和普通硬盘上测试.代码很长,这里将它分开来显示.

结构:

[StructLayout(LayoutKind.Sequential)]
internal struct DiskGeometry {
    public long Cylinders;
    public int MediaType;
    public int TracksPerCylinder;
    public int SectorsPerTrack;
    public int BytesPerSector;
}
Run Code Online (Sandbox Code Playgroud)

原生方法:

internal static class NativeMethods {
    [DllImport("Kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
    public static extern SafeFileHandle CreateFile(
        string fileName,
        uint fileAccess,
        uint fileShare,
        IntPtr securityAttributes,
        uint creationDisposition,
        uint flags,
        IntPtr template
        );

    [DllImport("Kernel32.dll", SetLastError=false, CharSet=CharSet.Auto)]
    public static extern int DeviceIoControl(
        SafeFileHandle device,
        uint controlCode,
        IntPtr inBuffer,
        uint inBufferSize,
        IntPtr outBuffer, …
Run Code Online (Sandbox Code Playgroud)

c# size ioctl disk deviceiocontrol

6
推荐指数
1
解决办法
3789
查看次数

StackOverflowException甚至在增加堆栈大小之后

我有一个C#WinForm应用程序,我运行x86模式.它适用于x86模式.当我使用任何CPU模式运行此应用程序时,会出现此问题.我得到以下提到的错误:

XXXXXX.dll中出现未处理的"System.StackOverflowException"类型异常

我知道这可能是由无限循环引起的,但在这种情况下,x86模式中应该发生相同的错误.我知道这不是因为无限次迭代.它与堆栈溢出有关.

做了一些研究后,我用Editbin增加了堆栈大小

Editbin.exe/Stack:14000000"$(TargetDir)MyProject.exe"

Editbin.exe/Stack:14000000"$(TargetDir)MyProject.exe"

任何想法可能是什么原因以及我应该进入的方向?

c# visual-studio-2012

6
推荐指数
2
解决办法
1608
查看次数

对于没有第二个条件的循环,即布尔检查?

我必须编写一个函数来计算传入的unsigned int的日志库16的底限.对于允许使用哪些运算符和哪些常量有限制,我们只能使用专门的for循环.

为清楚起见,我们不能使用任何条件语句(如果,否则,切换...).功能原型是:

int floor_log16(unsigned int x); 
Run Code Online (Sandbox Code Playgroud)

允许的运营商: ++ -- = & | ~ ^ << ! >>

允许的常数: 1 2 3 4 8 16

我写了一个程序版本如下:

int floor_log16(unsigned int x) {
    int index=1;
    int count=(1!=1);

    count--;

    for(; index<=x; index<<=4) {
        count++;
    }

    return count;
}
Run Code Online (Sandbox Code Playgroud)

这看似按预期工作.但是,我意识到,根据后来的功能和所需的功能,我们必须编写说明,我注意到,在"允许运营商"有时><上市.

我推断这意味着,因为对于floor_log16上面列出的函数,我们没有明确告知使用><,我只能假设上面发布的解决方案不被接受.

这让我很困惑,因为我不明白你怎么可能有一个没有布尔检查的for循环?

在条件满足时,不是循环迭代的整个想法吗?

c loops for-loop

6
推荐指数
1
解决办法
6885
查看次数

C#集合由属性索引?

我经常遇到的一个问题是需要以这样的方式存储对象集合,以便我可以通过特定的字段/属性来检索它们,该字段/属性是该对象的唯一"索引".例如,我有一个Person对象,其name字段是唯一标识符,我希望能够从一些Person对象集合中检索Person其中name="Sax Russell".在Java中,我通常通过使用Map我真正想要的地方来实现这一点Set,并且总是使用对象的"索引"字段作为地图中的键,即peopleMap.add(myPerson.getName(), myPerson).我想在C#中用Dictionarys 做同样的事情,像这样:

class Person {
    public string Name {get; set;}
    public int Age {get; set;}
    //...
}

Dictionary<string, Person> PersonProducerMethod() {
    Dictionary<string, Person> people = new Dictionary<string, Person>();
    //somehow produce Person instances...
    people.add(myPerson.Name, myPerson);
    //...
    return people;
}

void PersonConsumerMethod(Dictionary<string, Person> people, List<string> names) {
    foreach(var name in names) {
        person = people[name];
        //process person somehow...
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,这看起来很笨拙,并且在Dictionary …

c# linq collections

6
推荐指数
2
解决办法
3384
查看次数

如何从visual studio 2013中禁用mscorlib.dll?

我试图在visual studio 2013中使用自定义标准库,似乎无法弄明白.使用/ nostdlib在命令行上编译没有问题,尽管我希望能够在IDE中利用intellisense.除了我的自定义corelib之外,我已经删除了所有引用,并且由于有两个mscorlib变体,我遇到了相互矛盾的代码错误.

VS文档说:

在Visual Studio开发环境中设置此编译器选项

  1. 打开项目的" 属性"页面.

  2. 单击" 构建属性"页面.

  3. 单击" 高级"按钮.

  4. 修改不要引用mscorlib.dll属性.

虽然情况似乎并非如此,因为此选项不存在.有谁知道如何在vs2013中禁用mscorlib.dll?

c# msbuild visual-studio

6
推荐指数
1
解决办法
3677
查看次数

LINQ的新功能:这是使用LINQ的正确位置吗?

首先,我是LINQ的新手,所以我真的不知道它的来龙去脉.我试着在一些代码中使用它,根据我的诊断,它似乎与以相同方式使用for循环一样快.但是,我不确定这会如何扩展,因为我正在使用的列表可能会非常显着地增加.

我使用LINQ的碰撞检测功能的一部分(这仍然是在作品),我用它来剔除名单仅是相关的检查的.

这是LINQ版本:

partial class Actor {
    public virtual bool checkActorsForCollision(Vector2 toCheck) {
        Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);

        if(!causingCollision) // skip if this actor doesn't collide
            return false;

        foreach(
            Actor actor in
            from a in GamePlay.actors
            where a.causingCollision==true&&a.isAlive
            select a
            )
            if( // ignore offscreen collisions, we don't care about them
                (actor.location.X>GamePlay.onScreenMinimum.X)
                &&
                (actor.location.Y>GamePlay.onScreenMinimum.Y)
                &&
                (actor.location.X<GamePlay.onScreenMaximum.X)
                &&
                (actor.location.Y<GamePlay.onScreenMaximum.Y)
                )
                if(actor!=this) { // ignore collisions with self
                    Vector2 actorfloor=new Vector2((int)actor.location.X, (int)actor.location.Y);

                    if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
                        return true;
                }

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我以前的方法:

partial class …
Run Code Online (Sandbox Code Playgroud)

c# linq xna

5
推荐指数
1
解决办法
479
查看次数

如何检查字符串是否包含单个子串的出现?

我有这个:

string strings = "a b c d d e";
Run Code Online (Sandbox Code Playgroud)

我需要类似的东西string.Contains(),但我不仅需要知道字符串是否存在(如果在字母上方),而且还需要知道它是否仅存在一个单一时间.

我怎样才能做到这一点?

c# string

5
推荐指数
2
解决办法
5273
查看次数

构建组合矩阵

我确信这已被问了一百万次,但是当我搜索所有的例子都不太合适时,所以我想我还是应该问它.

我有两个数组,每个数组总共包含6个项目.例如:

string[] Colors=
    new string[] { "red", "orange", "yellow", "green", "blue", "purple" };

string[] Foods=
    new string[] { "fruit", "grain", "dairy", "meat", "sweet", "vegetable" };
Run Code Online (Sandbox Code Playgroud)

在这两个阵列之间,有36种可能的组合(例如"红色水果","红色颗粒").

现在我需要进一步将这些组合成六组唯一值.

例如:

meal[0]=
    new Pair[] { 
        new Pair { One="red", Two="fruit" }, 
        new Pair { One="orange", Two="grain" }, 
        new Pair { One="yellow", Two="dairy" }, 
        new Pair { One="green", Two="meat" }, 
        new Pair { One="blue", Two="sweet" }, 
        new Pair { One="purple", Two="vegetable" } 
    };
Run Code Online (Sandbox Code Playgroud)

在哪里吃饭

Pair[][] meal;
Run Code Online (Sandbox Code Playgroud)

在我的"餐"列表中不能重复任何元素.因此,只有一个"红色"项目和一个"肉"项目等.

我可以根据前两个数组轻松创建对,但我在如何最好地将它们组合成唯一组合方面做了一个空白.

c# algorithm combinations permutation matrix

5
推荐指数
1
解决办法
1865
查看次数

检查字节是否为0x00

编写此方法的最具可读性(和惯用语)是什么?

private bool BytesAreValid(byte[] bytes) {
    var t = (bytes[0] | bytes[1] | bytes[2]);
    return t != 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要一个函数来测试一个不是以它开头的文件的前三个字节00 00 00.

没有做太多的字节操作.上面的代码对我来说似乎不正确,因为t推断了类型Int32.

c# byte bit-manipulation bytearray

5
推荐指数
1
解决办法
6418
查看次数