Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何检查列表中x的n个数(mod运算符?)_C# - Fatal编程技术网

C#如何检查列表中x的n个数(mod运算符?)

C#如何检查列表中x的n个数(mod运算符?),c#,C#,我希望能够控制列表中条目的顺序。前五个条目应为“A”,后两个条目应为“B”,接下来五个条目应为“A”,接下来两个条目应为“B”,依此类推。我只是不能很好地理解这样做的逻辑 我似乎遇到了麻烦,“A”和“B”都可以被它们的重量整除。在这段代码中,当你有10 x A和2 x B时,你可以看到这一点。我的逻辑说下一个应该是“A”,而实际上,我希望下两个是“B”,给我一个10 x A,4 x B的总数,在这一点上,它应该返回到报告A作为下一个序列 我的示例代码如下所示,只需向数据变量添加更多的“a”或“b

我希望能够控制列表中条目的顺序。前五个条目应为“A”,后两个条目应为“B”,接下来五个条目应为“A”,接下来两个条目应为“B”,依此类推。我只是不能很好地理解这样做的逻辑

我似乎遇到了麻烦,“A”和“B”都可以被它们的重量整除。在这段代码中,当你有10 x A和2 x B时,你可以看到这一点。我的逻辑说下一个应该是“A”,而实际上,我希望下两个是“B”,给我一个10 x A,4 x B的总数,在这一点上,它应该返回到报告A作为下一个序列

我的示例代码如下所示,只需向数据变量添加更多的“a”或“b”即可对其进行测试

var aWeight = 5;
var bWeight = 2; 

var data = new string[] {"a", "a", "a", "a", "a"}; 

var aCount = data.Count(x => x == "a"); 
var bCount = data.Count(x => x == "b"); 

//aCount.Dump(); 
//bCount.Dump(); 

if(aCount % aWeight == 0) 
{
    if(aCount % aWeight == 0 && bCount % bWeight == 0 && bCount > 0)
    {
        Console.WriteLine("Next is A! - pos 1"); 
        return;
    }

    Console.WriteLine("Next is B! - pos 2"); 
}
else if(bCount % bWeight == 0) 
{
    Console.WriteLine("Next is A! - pos 3" ); 
} 

根据我对您想要什么的理解,您可以检查5:2与
a计数
b计数
的比率。如果它们相同,那么您需要一个
a
下一个

将if语句更改为包含已存在的aWeight、bWeight和aCount、bCount间接比率之间的比率检查。老实说,我不知道你是否理解这个问题本身,但根据我的判断,我给你的好处是怀疑

另外,您第二次检查
if(aCount%aWeight==0)
是因为它是多余的

if (aCount % aWeight == 0)
{
    var weightRatio = ((double)aWeight/bWeight);
    var countRatio = ((double)aCount / bCount);
    if (bCount % bWeight == 0 && bCount > 0 && (weightRatio == countRatio))
    {
        Console.WriteLine("Next is A! - pos 1");
        return;
    }

    Console.WriteLine("Next is B! - pos 2");
}
else if (bCount % bWeight == 0)
{
    Console.WriteLine("Next is A! - pos 3");
} 

您可以使用以下选项:

List<Tuple<int, string> > repetitions =new List<Tuple<int, string>>()
{
    new Tuple<int, string>(5, "a"),
    new Tuple<int, string>(2, "b")
};

int numberOfElements = 15;
int currentCount = 0;
int currentIndex = 0;
for (int i = 0; i <= numberOfElements; i++)
{
    if (currentCount >= repetitions.ElementAt(currentIndex).Item1)
    {
        currentCount = 0;
        // Switch to next element
        if (++currentIndex > repetitions.Count - 1)
        {
            // Reset
            currentIndex = 0;
        }
    }

    Console.Out.WriteLine(repetitions.ElementAt(currentIndex).Item2);
    currentCount++;
}
列表重复=新列表()
{
新元组(5,“a”),
新元组(2,“b”)
};
int numberOfElements=15;
int currentCount=0;
int currentIndex=0;
for(int i=0;i=repetitions.ElementAt(currentIndex.Item1)
{
currentCount=0;
//切换到下一个元素
如果(++currentIndex>repetitions.Count-1)
{
//重置
currentIndex=0;
}
}
Console.Out.WriteLine(repetitions.ElementAt(currentIndex.Item2));
currentCount++;
}
这将输出:

AAAAA BBAAABBAA


在这段代码中,当你有10 x A和2 x B=>怎么做?我以前见过这个问题:)这是来自面试测试吗?@Thomas Ayoub,如果你在LINQPad或VS中运行代码并附加到数据中,即var data=新字符串[]{“A”、“A”、“A”、“A”、“A”、“A”、“A”、“B”、“B”}@福波:没有,但我想这会是一个好的!我试图在控制器的各种视图中显示的内容中添加一些逻辑。但我已经把我的问题分解到最低限度,试图让人们更容易理解,而不必费力地完成所有这些,这就是我所面临的问题的本质。不确定我们是否能够从你的解释中理解这个问题,因为你自己可能不理解这个问题?这正是我所寻找的。非常感谢。