標題:
2025/4/11 課堂重點(昀杰)
[打印本頁]
作者:
郭竑志
時間:
2025-4-11 18:48
標題:
2025/4/11 課堂重點(昀杰)
本帖最後由 郭竑志 於 2025-4-11 19:48 編輯
[課程重點]
遞迴函式 (一) - 計算總和
遞迴函式 (三) - 費氏數列
1-2+3-4+....+n
1+4+9+16+....+n*n
2^n
格式化輸出
[今日作業]
遞迴函式 (二) - 階層運算
作者:
陳昀杰
時間:
2025-4-11 19:13
#include<iostream>
#include<cstdlib>
using namespace std;
int fai(int n)
{
if(n<=2) //邊界條件
return 1;
else
return fai(n-2)+fai(n-1);
}
int main()
{
int n;
cout<<"請問要推算費氏數列到第幾項次? ";
cin>>n;
for(int i=1; i<=n; i++)
cout<<fai(i)<<" ";
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳昀杰
時間:
2025-4-11 19:45
#include<bits/stdc++.h>
using namespace std;
int f3(int n)//f5
{
if(n==1)
return 2;
else
return f3(n-1)*2;
}
int main()
{
int n;
cin>>n;
cout<<f3(n);
return 0;
}
複製代碼
作者:
陳昀杰
時間:
2025-4-11 19:45
#include<bits/stdc++.h>
using namespace std;
int f2(int n)
{
if(n==1)
return 1;
else
return f2(n-1)+n*n;
}
int main()
{
int n;
cin>>n;
cout<<f2(n);
return 0;
}
複製代碼
作者:
陳昀杰
時間:
2025-4-11 19:46
#include<bits/stdc++.h>
using namespace std;//tot5=tot4+5=tot3-4+5
int f1(int n)
{
if(n==1) //邊界條件
return 1;
else if(n%2==1)
return f1(n-1)+n;
else
return f1(n-1)-n;
}
int main()
{
int n;
cin>>n;
cout<<f1(n)<<" ";
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2