Skip to content

Instantly share code, notes, and snippets.

@edward-hsu-1994
Last active August 29, 2015 14:24
Show Gist options
  • Save edward-hsu-1994/0e5b7f4b3a0b72461b7a to your computer and use it in GitHub Desktop.
Save edward-hsu-1994/0e5b7f4b3a0b72461b7a to your computer and use it in GitHub Desktop.
ITSA 39
//ITSA 39 - Problem_1重點函數
static int[] Problem_1(int Start) {
List<int> result = new List<int>();
do {
result.Add(Start);
if(Start % 2 == 0) Start /= 2;
else Start = Start * 3 + 1;
} while(Start != 1);
result.Add(1);
return result.ToArray();
}
//=====
//ITSA 39 - Problem_2重點函數
static int[,] Problem_2(int[,] A,int[,] B) {
for(int i = 0; i < A.GetLength(0); i++) {
for(int j = 0; j < A.GetLength(1); j++) {
A[i ,j] *= B[i ,j];
}
}
return A;
}
//=====
//ITSA 39 - Problem_3重點函數
static int Problem_3(int Level,int[] M) {//M預設為1,2,LEVEL表示階梯數量,M表示可以跨出步數
if(Level < 0)return 0;
if(Level == 0)return 1;
int result = 0;
foreach(int value in M) {
result+=Problem_3(Level - value ,M);
}
return result;
}
//=====
//ITSA 39 - Problem_4重點函數
static string Problem_4(string Input) {
if(Input.Length == 0)return string.Empty;
switch(Input[0]) {
case 'a':
return Input[0] + Problem_4(new string(Input.Substring(1).Reverse().ToArray()));
case 'b':
return Input[0] + Problem_4(Input.Substring(1) + Input.Substring(1));
case 'c':
return Input[0] + Problem_4(Input.Substring(1).Remove(0 ,2));
default:
return Input[0] + Problem_4(Input.Substring(1));
}
}
//=====
//ITSA 39 - Problem_5重點函數
static List<List<int>> Problem_5(int Max,List<int> MemberCount,List<int> Old = null) {//湊隊伍人數至MAX且多餘人數最少
MemberCount = (from t in MemberCount where t != Max select t).ToList<int>();
if(Old == null)Old = new List<int>();
if(Old.Sum() >= Max)return new List<List<int>>(new List<int>[]{Old});
List<List<int>> result = new List<List<int>>();
foreach(int MCount in MemberCount) {
List<int> Item = new List<int>(Old);//Copy
List<int> CopyMember = new List<int>(MemberCount);
Item.Add(MCount);CopyMember.Remove(MCount);
result.AddRange(Problem_5(Max ,CopyMember ,Item));
}
result.Sort((A,B) => {
Stack<int> As = new Stack<int>(A);
Stack<int> Bs = new Stack<int>(B);
int ASum = 0,index = 0, BSum = 0;
while(As.Count > 0)ASum += As.Pop() * (int)Math.Pow(10 ,index++);
index = 0;
while(Bs.Count > 0)BSum += Bs.Pop() * (int)Math.Pow(10 ,index++);
return ASum - BSum;
});
return (from t in result orderby t.Sum() select t).ToList<List<int>>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment