日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言xio习笔记1递归函数实例

發布時間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言xio习笔记1递归函数实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C語言xio習筆記1-遞歸函數實例

  • 1.翻牌問題
  • 2.猴子選大王
  • 3. Passing the Ball
  • 4.快速求冪

<_>聲明:本人小白一枚。eee第一次寫blog如有錯誤,歡迎指出。

解釋遞歸概念的文章不難找到。
這里從我們學校c語言練習題里面搬簡單的遞歸實例

1.翻牌問題

手上有一疊牌, 把第一張牌(即位于頂面的牌)開始從上往下依次編號為1~n. 重復以下操作: 把第一張牌扔掉, 然后把新的第一張牌放到整疊牌的最后. 問最后扔掉的是哪一張?

Standard Input
有多組測試數據。輸入的第一行是整數T(1<=T<=100),表示隨后有T組測試數據。每組測試數據占一行,該行是一個正整數n, n <= 200.

Standard Output
對應每組測試數據,輸出最后扔掉的那張牌編號, 占一行.

遞推公式 f(2n)=2f(n),f(2n-1)=2f(n)-2f(1)=1

參考代碼

#include<stdio.h> #include<stdlib.h> int solve(int n); int main() {//int T;//scanf("%d", &T);//while(T--)//{int k;scanf("%d", &k);printf("%d\n", solve(k));//}(需要直接按題目樣式輸出把//去掉即可)return 0; } int solve(int n) {int f;if(n==1)f = 1;elseif(n%2==0)f = 2 * solve(n / 2);elsef = 2 * solve((n + 1) / 2) - 2;return f; }

2.猴子選大王

在m只猴子聚在一起選大王, 商定規則如下: 大家圍成一圈, 按順時針從1編號, 第一次從編號為1的開始報數, 以后循環進行, 當報到n時退出圈子, 下一只則重新從1開始報數, 圈子中剩下的最后一只猴子則為大王.

Standard Input
有多組測試數據. 輸入的第一行是整數T(1<=T<=100), 表示隨后測試數據的組數. 每組測試數據占一行, 由正整數m和n組成, 兩數之間有一個空格. 2 <= m,n <= 200.
Standard Output
對應每組測試數據, 輸出選出的大王的猴子編號.

遞推公式:f(1,n)=1,f(m,n)=(f(m-1,n)+n)%m.

參考代碼

#include <stdio.h> #include<stdlib.h> int solve(int m,int n); int main() {//int T;//scanf("%d", &T);//getchar();//while(T--)/{int m, n;scanf("%d %d", &m,&n);printf("%d\n",solve(m,n));//}return 0; } int solve(int m,int n) {int f;if(m==1)f = 1;elsef = (solve(m - 1, n) + n) % m;if(f==0)f = m;return f; }

3. Passing the Ball

There are N people passing the ball. It starts from the first person, and each time the person who gets the ball should pass the ball to another one. So what is the number of situations in which the ball is passed back to the first person after passing M times.
大概就是N個人傳球,問傳過M次之后傳回第一個人的有多少種情況,其中 N∈[2,9],M∈[1,15].
Standard Input
Including two integers N and M, and N∈[2,9],M∈[1,15].
讀入N,M
Standard Output
The answer,and it does not exceed 2^31.
輸出結果
遞推公式:〖f(m,n)=(m-1)〗^(n-1)-f(m,n-1)

#include<stdio.h> #include<stdlib.h> int solve(int m, int n); int my_pow(int x,int t); int main() {int m, n;scanf("%d %d", &m, &n);printf("%d", solve(m, n));return 0; } int solve(int m,int n) {int ans;if(n==1)ans = 0;elseans =my_pow(m-1,n-1)-solve(m,n-1);return ans; } int my_pow(int x,int t)//(math.h)用里面的pow函數來求冪誤差挺大的,小整數的整數次冪比較好算,就自己寫了個 {int ans=1;for (int i=1;i<=t;i++)ans = x * ans;return ans; }

4.快速求冪

對于正整數a和n, 求a^ n一般考慮成n個a相乘. 但這樣運算不夠快速. 當n為偶數時, a^ n可表示為(a^ 2)^ (n/2); 當a為奇數時, a^ n可表示為(a^ 2)^ (n/2)再乘a. 你的任務是在給出a和n的情況下, 利用這種算法求出a^n. 要求: 1. 定義結構體表示一個大整數; 2. 大整數乘法寫成自定義函數; 3. 快速冪算法寫成自定義遞歸函數.

Standard Input
有多組測試數據.輸入的第一行是整數T(1<=T<=20), 表示隨后測試數據的組數. 每組測試數據占一行, 由正整數a和n組成, 兩數之間有一個空格. 0 < a < 100, 0 < n < 200.
Standard Output
對應每組測試數據, 輸出一行a^n的準確結果.

#include<stdio.h> #include<stdlib.h> #include<string.h> #define Max 400 struct large_num {int num[Max];int length; }; void solve(); struct large_num my_power(struct large_num a, int n); struct large_num my_mutiply(struct large_num a, struct large_num b); int main() {// int T;//scanf("%d", &T);//while(getchar()!='\n');//while(T--)solve();return 0; } void solve() {int a, n;scanf("%d %d", &a, &n);struct large_num num_a,ans;memset(&num_a, 0, sizeof(num_a));num_a.num[Max-1] = a%10;num_a.num[Max-2] = a/10;num_a.length = (a/10!=0)?2:1;ans = my_power(num_a, n);for (int i=ans.length;i>=1;i--)printf("%d", ans.num[Max - i]);printf("\n"); } struct large_num my_power(struct large_num a, int n)// {struct large_num ans1;memset(&ans1, 0, sizeof(ans1)); if(n==1)ans1 = a;else {if(n==2)ans1 = my_mutiply(a, a);else{if(n%2==0)ans1 = my_power(my_power(a, 2), n / 2);elseans1 = my_mutiply(my_power(my_power(a, 2), (n-1)/ 2),a);}}return ans1; } struct large_num my_mutiply(struct large_num a, struct large_num b) {struct large_num ans2;memset(&ans2, 0, sizeof(ans2));int l = a.length +b.length;for (int i=1;i<=a.length;i++){for (int j = 1; j <= b.length;j++)ans2.num[Max - (i + j) + 1] += (a.num[Max-i] )* (b.num[Max-j]);}for (int k = 1;k<=l-1;k++){ans2.num[Max - k - 1] += (ans2.num[Max - k]) / 10;ans2.num[Max - k ] =(ans2.num[Max-k])%10;}ans2.length = (ans2.num[Max - l]!=0) ? l:(l - 1);return ans2; }

來源:https://acm.uestc.edu.cn/contest

總結

以上是生活随笔為你收集整理的C语言xio习笔记1递归函数实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。