C# 有些循环使用条件。具体问题
我的C代码中有一个关于数学或组合数学的小问题。我不知道怎么写这个 我有一个类节和TestClass,但没有返回预期结果的方法C# 有些循环使用条件。具体问题,c#,loops,conditional-statements,permutation,C#,Loops,Conditional Statements,Permutation,我的C代码中有一个关于数学或组合数学的小问题。我不知道怎么写这个 我有一个类节和TestClass,但没有返回预期结果的方法 public class Section { public int Id { get; set; } public int Pages { get; set; } public string Name { get; set; } } [TestFixture] public class PermutatorTest { private I
public class Section
{
public int Id { get; set; }
public int Pages { get; set; }
public string Name { get; set; }
}
[TestFixture]
public class PermutatorTest
{
private IList<Section> _sections;
private int _targetPage;
[SetUp]
public void SetUp()
{
_targetPage = 30;
_sections = new List<Section>
{
new Section {Id = 1, Pages = 15, Name = "A"},
new Section {Id = 2, Pages = 15, Name = "B"},
new Section {Id = 3, Pages = 10, Name = "C" },
new Section {Id = 4, Pages = 10, Name = "D"},
new Section {Id = 5, Pages = 10, Name = "E"},
new Section {Id = 6, Pages = 5, Name = "F"}
};
}
[Test]
public void GetPermutationsTest()
{
// Code to return list of all combinations
}
}
e、 g.DDE=10+10+10=30正常
CFFFF=10+5+5+5+5=30正常
等等
我不知道最好的方法来创建循环,并把记录列表
非常感谢您对我的每一次帮助。仅供参考和搜索。 我知道,这个代码不是很干净 但我把它放在这里作为nUnit测试 它返回我想要的。。。我想
using System;
using System.Collections.Generic;
using NUnit.Framework;
[TestFixture]
public class PermutatorTest
{
private IList<Section> _sections;
private int _targetPage;
private IList<Config> _result;
[SetUp]
public void SetUp()
{
_targetPage = 30;
_sections = new List<Section>
{
new Section {Id = 1, Pages = 15, Name = "A"},
new Section {Id = 2, Pages = 15, Name = "B"},
new Section {Id = 3, Pages = 10, Name = "C" },
new Section {Id = 4, Pages = 10, Name = "D"},
new Section {Id = 5, Pages = 10, Name = "E"},
new Section {Id = 6, Pages = 5, Name = "F"}
};
_result = new List<Config>();
}
[Test]
public void GetPermutationsTest()
{
for (var b =0 ; b<=_sections.Count-1; b++)
{
var config = new Config
{
Name = _sections[b].Name,
Ids = _sections[b].Id.ToString(),
Pages = _sections[b].Pages
};
GoDeeperAndAddToResult(config, b);
}
Console.WriteLine(_result.Count);
foreach (var item in _result)
{
Console.WriteLine($"{item.Name} - {item.Ids} - {item.Pages}");
}
}
private void GoDeeperAndAddToResult(Config config, int startIndex)
{
for (var b = startIndex; b <= _sections.Count-1; b++)
{
var section = _sections[b];
var combName = config.Name;
var combIds = config.Ids;
var combPages = config.Pages;
var maxSec = _targetPage / section.Pages;
for (var a = 1; a <= maxSec; a++)
{
combName = combName + section.Name;
combIds = combIds + section.Id.ToString();
combPages = combPages + section.Pages;
var subConfig = new Config
{
Name = combName,
Ids = combIds,
Pages = combPages
};
if (subConfig.Pages == _targetPage)
{
_result.Add(subConfig);
break;
}
else if (subConfig.Pages < _targetPage)
{
GoDeeperAndAddToResult(subConfig, b + 1);
}
else
{
break;
}
}
}
}
public class Config
{
public string Name { get; set; }
public string Ids { get; set; }
public int Pages { get; set; }
}
public class Section
{
public int Id { get; set; }
public int Pages { get; set; }
public string Name { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用NUnit.Framework;
[测试夹具]
公共类置换侵权测试
{
私人IList_部门;
私人int_targetPage;
私有IList_结果;
[设置]
公共作废设置()
{
_targetPage=30;
_节=新列表
{
新节{Id=1,Pages=15,Name=“A”},
新节{Id=2,Pages=15,Name=“B”},
新节{Id=3,Pages=10,Name=“C”},
新节{Id=4,Pages=10,Name=“D”},
新的部分{Id=5,Pages=10,Name=“E”},
新节{Id=6,Pages=5,Name=“F”}
};
_结果=新列表();
}
[测试]
public void GetPermutationsTest()
{
对于(var b=0;b这是我最初的想法,我将为您发布,它只是返回一个字符串列表
public List<String> result;
public void GetResultList(int startOffs, String CurNames, int curTotal)
{
for (int newOffs = startOffs; newOffs < _sections.Count; newOffs++)
{
int newTotal = curTotal + _sections[newOffs].Pages;
String newNames = CurNames+ _sections[newOffs].Name;
if (newTotal < _targetPage)
GetResultList(newOffs, newNames, newTotal);
else if (newTotal == _targetPage)
result.Add(newNames);
}
}
公开列表结果;
public void GetResultList(int startoff、字符串CurNames、int curTotal)
{
对于(int newOffs=startOffs;newOffs<\u sections.Count;newOffs++)
{
int newTotal=curTotal+\u节[newOffs]。页数;
字符串newNames=CurNames+_节[newOffs].Name;
如果(新总数<\u目标页)
GetResultList(newOffs、NewName、newTotal);
else if(newTotal==\u targetPage)
结果.添加(新名称);
}
}
通过初始化结果和启动参数调用:
result = new List<String>();
GetResultList(0,"",0);
result=newlist();
GetResultList(0,“,0);
这是一个修改为使用您的配置类的版本
public void GetResultList(int startOffs, Config CurConfig)
{
for (int newOffs = startOffs; newOffs < _sections.Count; newOffs++)
{
Config newConfig = new Config{ Name = CurConfig.Name + _sections[newOffs].Name,
Ids = CurConfig.Ids + _sections[newOffs].Id.ToString(),
Pages = CurConfig.Pages + _sections[newOffs].Pages};
if (newConfig.Pages < _targetPage)
GetResultList(newOffs, newConfig);
else if (newConfig.Pages == _targetPage)
_result.Add(newConfig);
}
}
public void GetResultList(int startOffs,Config CurConfig)
{
对于(int newOffs=startOffs;newOffs<\u sections.Count;newOffs++)
{
Config newConfig=newConfig{Name=CurConfig.Name+\u节[newOffs].Name,
Ids=CurConfig.Ids+\u节[newOffs].Id.ToString(),
Pages=CurConfig.Pages+\u节[newOffs].Pages};
如果(newConfig.Pages<\u targetPage)
GetResultList(newOffs、newConfig);
else if(newConfig.Pages==\u targetPage)
_result.Add(newConfig);
}
}
调用需要初始化结果&启动配置实例
_result = new List<Config>();
Config s = new Config { Ids = "", Pages=0, Name=""};
GetResultList(0,s);
\u结果=新列表();
配置s=new-Config{Ids=”“,Pages=0,Name=”“};
GetResultList(0,s);
递归应该比循环更容易,我想应该有31个结果-为什么DEE(10+10+10)不在您的列表中?事实上..DEE也:)我也尝试通过递归来制作一些东西。
_result = new List<Config>();
Config s = new Config { Ids = "", Pages=0, Name=""};
GetResultList(0,s);