递归与非递归:实现求第n个斐波那契数。实现n^k。输入一个非负整数,返回组成它的数字之和。将参数字符串中的字符反向排列。实现strlen。求n的阶乘。打印一个整数的每一位。

news/2025/1/15 15:59:53/

1.递归和非递归分别实现求第n个斐波那契数。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>int fib(int n)
{if (n <= 2)return 1;elsereturn fib(n - 1) + fib(n - 2);
}int main()
{int n = 0;int ret = 0;scanf("%d", &n);ret = fib(n);printf("%d\n", ret);system("pause");return 0;
}

2.编写一个函数实现n^k,使用递归实现

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>int fun(int n, int k)
{if (k == 0)return 1;else if (k == 1)return n;return n * fun(n, k - 1);
}int main()
{int n = 0;int k = 0;scanf("%d %d", &n, &k);int c = fun(n, k);printf("%d\n", c);system("pause");return 0;
}


3. 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和,
例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>int DigitSum(int n)
{if (n < 0)return -1;else if(n >= 0 && n <= 9)return n;return (n % 10) + DigitSum(n / 10);
}int main()
{int num = 0;scanf("%d", &num);int ret = DigitSum(num);printf("%d\n", ret);system("pause");return 0;
}


4. 编写一个函数 reverse_string(char * string)(递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。

#include<stdio.h>
#include<stdlib.h>char reverse_string(char *string)
{if (*string == '\0')printf("%c", *string);else{reverse_string(++string);printf("%c", *(--string));}
}int main()
{char *a = "abcde";reverse_string(a);printf("\n");system("pause");return 0;
}


5.递归和非递归分别实现strlen

递归
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>int my_strl(char *p)
{assert(p != NULL);if (*p == '\0')return 0;elsereturn 1 + my_strl(p + 1);
}int main()
{char *a = "abcdefg";int ret = my_strl(a);printf("%d\n", ret);system("pause");return 0;
}
非递归
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>int my_strl(char *p)
{assert(p != NULL);int num = 0;while (*p){num++;p++;}return num;
}int main()
{char *a = "abcdefg";int ret = my_strl(a);printf("%d\n", ret);system("pause");return 0;
}


6.递归和非递归分别实现求n的阶乘

递归
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>int fun(int n)
{if (n == 1)return 1;elsereturn n * fun(n - 1);
}int main()
{int num = 0;scanf("%d", &num);int ret = 0;ret = fun(num);printf("%d\n", ret);system("pause");return 0;
}非递归
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>int fun(int n)
{int sum = 1;for (int i = 1; i <= n; ++i){sum = sum * i;}return sum;
}
int main()
{int num = 0;scanf("%d", &num);int ret = 0;ret = fun(num);printf("%d\n", ret);system("pause");return 0;
}


7.递归方式实现打印一个整数的每一位

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>void Prit(int num)
{if (num < 10)printf("%d\n", num);else{Prit(num / 10);printf("%d\n", num % 10);}
}int main()
{int num = 0;scanf("%d", &num);Prit(num);system("pause");return 0;
}

 


http://www.ppmy.cn/news/304079.html

相关文章

高精度 hdu 2940 Hex Factorial

直接暴力打表&#xff0c;也可以在代码外打表复制粘贴 #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int BIT 16; struct BIGint{int bit[200];void MEMset(){memset(bit,0,sizeof(bit));bit[1] 1;}void multiply(int …

POJ 2940 Wine Trading in Gergovia [贪心]

Description As you may know from the comic “Asterix and the Chieftain’s Shield”, Gergovia consists of one street, and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simple enough: everyone buys wine from other inha…

HDU 2940 Hex Factorial

题目求N&#xff01;中有多少个0 用16进制高精度模拟 想起来一起以前做过的一道题&#xff0c;求末尾有多少个0&#xff0c;不能取0前边的3 4个非零位存下来进行计算&#xff0c;误差太大 #include <iostream> #include <fstream> #include <cstring> usin…

poj 2940 求和

#include <stdio.h>#include <stdlib.h>int main(){ int i,n,a,sum; sum0; scanf("%d%d",&a,&n); for(i0;i<n;i) { suma; aa%1010*a; } printf("%d\n",sum); return 0;} 转载于:https://www…

稳定的LDO芯片推荐

推荐几款稳定性较好的LDO芯片&#xff1a; LM2937&#xff1a;这是一款常用的低噪声LDO&#xff0c;具有良好的稳定性和高效率。 LM2675&#xff1a;这是一款高效率的LDO&#xff0c;特别适用于需要高输出精度的应用。 LM2937-N&#xff1a;这是LM2937的低噪声版本&#xff0c;…

poj2940

简单题 View Code #include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define maxn 100005int n, f[maxn];void input(){for (int i 0; i < n; i) scanf("%d", &f[i]);}void w…

百练 2940 求和

题目链接&#xff1a;http://bailian.openjudge.cn/practice/2940 # include <stdio.h>int main() {int Sn,a,a_back,n;int i;scanf("%d%d",&a,&n);a_backa; Sna;for(i1;i<n;i){aa*10a_back;SnSna;}printf("%d\n",Sn); return 0; } &am…

HDOJ2940高精度阶乘优化版

题目链接&#xff1a;hdoj2940-Hex Factorial 在此转到关于本书博客ACM大学生程序设计竞赛在线题库最新精选题解&#xff08;赵端阳&#xff09;部分解析 原书和网上大多数程序都是从0到200最高位不断加乘&#xff0c;然后判断前导零位置。实际上阶乘是递增记录的&#xff0c;可…