2017.6.29 C# 無痛學習 第 6 章 筆記
紀錄時間: 1830~
函數呼叫練習,三次方好難= =
=======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp9
{
class Program
{
public static int SqTotal(uint i, params int[] Num) //params 是在函數傳入Array的寫法
{
int Total = 0; //不加Total在這沒辦法return
for (int j = 0; j < Num.Length; j++) //幹...不過是乘個三次方相加怎麼難成這樣
//第一個for先依序取出三個數字,在將數字乘三次
{
int Sq = Num[j]; //因為Sq值會持續被累乘,這裡先設定Sq的初始值,就是Num1~3原本的值
for (int k = 0; k < i-1; k++) //開乘 i 次
{
int Tmp = Num[j]; //因為Sq值會持續被累乘,這裡用Tmp當接下來乘的值,就是就是Num1~3原本的值,課本的寫法: Tmp *= num[j]
Sq = Sq * Tmp; //第一圈: 4 = 2*2,第二圈: 8 = 4*2 ...
}
Total = Total + Sq; //第一圈: 8 = 0 + 8,第二圈: 35 = 8 + 27(3的立方) ...
Console.WriteLine("第{0}次的Total = {1}", j, Total);
}
return Total; //傳回加完的立方
}
static void Main(string[] args)
{
Console.WriteLine("請依序輸入三個數字");
Console.Write("第一個數字==>");
int Num1 = int.Parse(Console.ReadLine());
Console.Write("第二個數字==>");
int Num2 = int.Parse(Console.ReadLine());
Console.Write("第三個數字==>");
int Num3 = int.Parse(Console.ReadLine());
int AfterSqTotal = SqTotal(3, Num1, Num2, Num3); //呼叫函數這邊要幫他設個變數
Console.WriteLine("{0}的立方 + {1}的立方 + {2}的立方 = {3}", Num1, Num2, Num3, AfterSqTotal); //輸出結果
Console.Read(); //記得加他媽的這行,不然甚麼鬼都看不到
}
}
}
========================================================================================
費式數列
========================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp11
{
class Program
{
public static int fib(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
static void Main(string[] args)
{
Console.Write("請輸入費式數列值 = ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i <= n; i++)
{
int fibNum = fib(i);
Console.WriteLine("第{0}階費式數列值 = {1}", i, fibNum);
}
Console.Read();
}
}
}
=========================================================================================
函數呼叫練習,三次方好難= =
=======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp9
{
class Program
{
public static int SqTotal(uint i, params int[] Num) //params 是在函數傳入Array的寫法
{
int Total = 0; //不加Total在這沒辦法return
for (int j = 0; j < Num.Length; j++) //幹...不過是乘個三次方相加怎麼難成這樣
//第一個for先依序取出三個數字,在將數字乘三次
{
int Sq = Num[j]; //因為Sq值會持續被累乘,這裡先設定Sq的初始值,就是Num1~3原本的值
for (int k = 0; k < i-1; k++) //開乘 i 次
{
int Tmp = Num[j]; //因為Sq值會持續被累乘,這裡用Tmp當接下來乘的值,就是就是Num1~3原本的值,課本的寫法: Tmp *= num[j]
Sq = Sq * Tmp; //第一圈: 4 = 2*2,第二圈: 8 = 4*2 ...
}
Total = Total + Sq; //第一圈: 8 = 0 + 8,第二圈: 35 = 8 + 27(3的立方) ...
Console.WriteLine("第{0}次的Total = {1}", j, Total);
}
return Total; //傳回加完的立方
}
static void Main(string[] args)
{
Console.WriteLine("請依序輸入三個數字");
Console.Write("第一個數字==>");
int Num1 = int.Parse(Console.ReadLine());
Console.Write("第二個數字==>");
int Num2 = int.Parse(Console.ReadLine());
Console.Write("第三個數字==>");
int Num3 = int.Parse(Console.ReadLine());
int AfterSqTotal = SqTotal(3, Num1, Num2, Num3); //呼叫函數這邊要幫他設個變數
Console.WriteLine("{0}的立方 + {1}的立方 + {2}的立方 = {3}", Num1, Num2, Num3, AfterSqTotal); //輸出結果
Console.Read(); //記得加他媽的這行,不然甚麼鬼都看不到
}
}
}
========================================================================================
費式數列
========================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp11
{
class Program
{
public static int fib(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
static void Main(string[] args)
{
Console.Write("請輸入費式數列值 = ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i <= n; i++)
{
int fibNum = fib(i);
Console.WriteLine("第{0}階費式數列值 = {1}", i, fibNum);
}
Console.Read();
}
}
}
=========================================================================================
留言
張貼留言