###### tags: `C#`
# 資料結構
三維陣列
```
public static void Main()
{
int[,,] num = {
{{33, 45, 67},{23, 71, 56},{55, 38, 66}},
{{21, 9, 15},{38, 68, 18},{90, 101, 89}}
};
int min = num[0,0,0];
for(int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(1); j++)
{
for (int z = 0; z < num.GetLength(2); z++)
{
//Response.Write(num[i, j, z].ToString());
if(min >= num[i, j, z])
{
min = num[i, j, z];
}
}
}
}
Console.Write(min.ToString());
}
```
貪心演算法:假设商店老板需要找零n元钱,钱币的面额有:100元、50元、20元、5元、1元,如何找零使得所需钱币的数量最少?
```
public static void Main()
{
// your code goes here
//第一个问题 找零
//假设商店老板需要找零n元钱,钱币的面额有:100元、50元、20元、5元、1元,如何找零使得所需钱币的数量最少?
int[] len = ChangeMoney(346);
for (int i = 0; i < len.Length; i++){
Console.Write(len[i]);
}
}
public static int[] ChangeMoney(int n){
int[] money = new int[] {100, 50, 20, 5, 1};
int[] change = new int[] {0, 0, 0, 0,0};
for (int i = 0; i < money.Length; i++){
if (n == 0)
break;
change[i] = n / money[i];
n = n % money[i];
}
return change;
}
```
動態規劃
```
public static void Main()
{
// your code goes here
Console.Write(fib(4));
}
public static int fib(int n){
int[] output = new int[1000];
int result = output[n];
if (result == 0){
if (n == 0)
return 0;
if (n == 1)
return 1;
else
return (fib(n-1) + fib(n-2));
}
output[n] = result;
return result;
}
```
階乘
```
public static void Main()
{
// your code goes here
factorial(5);
}
public static void factorial(int num){
int sum = 1;
for(int i = 1; i < num + 1; i++){
for(int j = i; j > 0; j--){
sum = sum * j;
}
Console.Write(i + "!=" + sum + "\n");
sum = 1;
}
}
```