pong网的算法题,超时了,发到这边帮忙看看怎样
/// <summary>
/// 给定一个数t,以及n个整数,在这n个整数中找到相加之和为t的所有组合,例如t = 4,n = 6,这6个数为[4, 3, 2, 2, 1, 1],这样输出就有4个不同的组合,
/// 它们的相加之和为4:4, 3+1, 2+2, and 2+1+1。请设计一个高效算法实现这个需求。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int t = 4;
List<int> source = new List<int>();
source.Add(4);
source.Add(3);
source.Add(2);
source.Add(2);
source.Add(1);
source.Add(1);
List<List<int>> result = new List<List<int>>();
source.Sort();
List<List<int>> rs = GetResult(t, source);
List<List<int>> rss = FilterResult(rs);
}
public static Dictionary<string, int> DicKey = new Dictionary<string, int>();
public static List<List<int>> GetResult(int target, List<int> source)
{
List<List<int>> result = new List<List<int>>();
int length = source.Count;
for (int i = length - 1; i >= 0; i--)
{
///从排序的数组中得到
if (source[i] < target)
{
List<List<int>> inResult = GetResult(target - source[i], source.Take(i).ToList());
foreach (List<int> ll in inResult)
{
ll.Add(source[i]);
}
result.AddRange(inResult);
}
else if (source[i] == target)
{
List<int> inr = new List<int>();
inr.Add(source[i]);
result.Add(inr);
}
else
{
List<List<int>> inResult = GetResult(target - source[0], source.Take(i).ToList());
result.AddRange(inResult);
}
}
return result;
}
public static List<List<int>> FilterResult(List<List<int>> source)
{
List<List<int>> rs = new List<List<int>>();
for (int i = source.Count - 1; i >= 0; i--)
{
if (!DicKey.ContainsKey(string.Join(",", source[i])))
{
rs.Add(source[i]);
DicKey.Add(string.Join(",", source[i]), 1);
}
}
return rs;
}
/// 给定一个数t,以及n个整数,在这n个整数中找到相加之和为t的所有组合,例如t = 4,n = 6,这6个数为[4, 3, 2, 2, 1, 1],这样输出就有4个不同的组合,
/// 它们的相加之和为4:4, 3+1, 2+2, and 2+1+1。请设计一个高效算法实现这个需求。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int t = 4;
List<int> source = new List<int>();
source.Add(4);
source.Add(3);
source.Add(2);
source.Add(2);
source.Add(1);
source.Add(1);
List<List<int>> result = new List<List<int>>();
source.Sort();
List<List<int>> rs = GetResult(t, source);
List<List<int>> rss = FilterResult(rs);
}
public static Dictionary<string, int> DicKey = new Dictionary<string, int>();
public static List<List<int>> GetResult(int target, List<int> source)
{
List<List<int>> result = new List<List<int>>();
int length = source.Count;
for (int i = length - 1; i >= 0; i--)
{
///从排序的数组中得到
if (source[i] < target)
{
List<List<int>> inResult = GetResult(target - source[i], source.Take(i).ToList());
foreach (List<int> ll in inResult)
{
ll.Add(source[i]);
}
result.AddRange(inResult);
}
else if (source[i] == target)
{
List<int> inr = new List<int>();
inr.Add(source[i]);
result.Add(inr);
}
else
{
List<List<int>> inResult = GetResult(target - source[0], source.Take(i).ToList());
result.AddRange(inResult);
}
}
return result;
}
public static List<List<int>> FilterResult(List<List<int>> source)
{
List<List<int>> rs = new List<List<int>>();
for (int i = source.Count - 1; i >= 0; i--)
{
if (!DicKey.ContainsKey(string.Join(",", source[i])))
{
rs.Add(source[i]);
DicKey.Add(string.Join(",", source[i]), 1);
}
}
return rs;
}
...全文
相关推荐
pong:经典的 Atari Pong 游戏
#Pong 经典的 Atari Pong 游戏! 玩。 科迪·麦克纳马拉和达娜·雅库宾斯基 ####指示: 使用向上和向下箭头移动您的桨 享受!
p5-pong:JS Pong游戏
p5傍 JavaScript Pong游戏。
js_pong:用JavaScript进行Pong
用JavaScript进行Pong 这是我做过的第一个真正的视频游戏,尽管它肯定是在开发过程中发展起来的。 蛋白石版本可以在这里播放,实际上与此版本相同: : 。
pong游戏Pong!.zip
Pong! 是一个完全用 JavaScript 实现的 pong 游戏,基于 Socket.io 和 Canvas 技术实现。 特性: 服务器 socket.io 集成 支持两个在线玩家 响应式和自适应 UI 支持移动设备 支持手势操作 tokenized lobby entry page
c++ 游戏 pong
c++做的简单双人小游戏,适合初学者了解简单游戏制作


