Skip to content

Instantly share code, notes, and snippets.

@libraplanet
Last active September 1, 2016 15:06
Show Gist options
  • Save libraplanet/fbb5d49c62749a3eb47baf18d357f350 to your computer and use it in GitHub Desktop.
Save libraplanet/fbb5d49c62749a3eb47baf18d357f350 to your computer and use it in GitHub Desktop.
count, if total is 24 at all line in permutation.
count, if total is 24 at all line in permutation.
using System;
using System.Text;
using System.Collections.Generic;
class Programs {
delegate TResult Func<T, TResult>(T t);
static void Main(){
int cnt = 0;
cnt = calc(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, new int[0], delegate(int[] list){
// [0]
// [1] [2]
// [3] [4] [5]
// [6] [7] [8] [9]
int[] total = new int[] {
list[0] + list[1] + list[3] + list[6],
list[0] + list[2] + list[5] + list[9],
list[6] + list[7] + list[8] + list[9],
};
bool f = true;
for(int i = 0; i < total.Length; i++ ){
f &= total[i] == 24;
}
if(f) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < total.Length; i++ ){
if(sb.Length > 0) {
sb.Append(",");
}
sb.Append(total[i]);
}
Console.WriteLine("");
Console.WriteLine(string.Format("[f({0}), total({1})]", new object[]{f, sb.ToString(), }));
Console.WriteLine(string.Format(" {0,3} ", new object[]{list[0], }));
Console.WriteLine(string.Format(" {0,3} {1,3} ", new object[]{list[1], list[2], }));
Console.WriteLine(string.Format(" {0,3} {1,3} {2,3} ", new object[]{list[3], list[4], list[5], }));
Console.WriteLine(string.Format("{0,3} {1,3} {2,3} {3,3}", new object[]{list[6], list[7], list[8], list[9], }));
Console.WriteLine();
return 1;
} else {
Console.Write(".");
return 0;
}
});
Console.WriteLine("");
Console.WriteLine("Hellow C#");
Console.WriteLine("cnt = " + cnt);
}
static int calc(int[] src, int[] tgt, Func<int[], int> func) {
if(src.Length == 0) {
return func(tgt);
} else {
int ret = 0;
for(int i = 0; i < src.Length; i++) {
List<int> srcList = new List<int>(src);
List<int> tgtList = new List<int>(tgt);
srcList.RemoveAt(i);
tgtList.Add(src[i]);
ret += calc(srcList.ToArray(), tgtList.ToArray(), func);
}
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment