//1:递归求第n个斐波那契数
/*#include<stdio.h >
#include<windows.h>
int fib(int n)
{
if (n < 3)
{
return 1;
}
else return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 3;
int ret=fib(n);
printf("%d", ret);
system("pause");
return 0;
}*/
//1:迭代求第n个斐波那契数
/*#include<stdio.h>
#include<windows.h>
int fib(int n)
{
int first = 1;
int second = 1;
int third = 0;
while (n <= 2)
{
return 1;
}
while (n > 2)
{
n--;
third = first + second;
first = second;
second = third;
}
return third;
}
int main()
{
int n = 5;
printf("%d\n", fib(n));
system("pause");
return 0;
}*/
//2:n^k递归法
/*#include<stdio.h>
#include<windows.h>
int fun(int n ,int k)
{
if (k == 0)
{
return 1;
}
else {
return n*fun(n, k - 1);
}
}
int main()
{
int n=2;
int k = 5;
int ret = fun(n,k);
printf("%d\n", ret);
system("pause");
return 0;
}*/
//3: 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和,例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
/*#include<stdio.h>
#include<windows.h>
int DigitSum(int n)
{
int add = 0;
if (n > 9)
{
add=DigitSum(n / 10);
}
return n % 10+add;
}
int main()
{
int n = 1728;
int add =DigitSum(n);
printf("%d\n", add);
system("pause");
return 0;
}*/
//4:编写一个函数reverse_string(char * string)(递归实现)
/*#include<stdio.h>
#include<windows.h>
char* reverse_string(char * string)//一维数组传参时,降为一级指针
{
int n = 0;
char *tring;
char temp;
tring = string;//将原始的字符串保存到string指针中
while (*string != 0)//求得字符串长度n,让string指针指向最后一个数组元素的地址
{
n++;
string++;
}
if (n > 1)
{
temp = tring[0];//将第一个元素给中间变量
tring[0]=tring[n-1];//将最后一个元素给第一个元素
tring[n - 1] = '\0';//最后一个位置放\0
reverse_string(tring + 1);//使用递归法,重复上面的操作
tring[n-1] = temp;
}
return tring;
}
int main()
{
char string[] = "abcdefg";
printf("翻转前:%s", string);
printf("\n");
printf("翻转后:%s",reverse_string(string));
system("pause");
return 0;
}*/
//5:递归实现strlen
/*#include<stdio.h>
#include<windows.h>
int strlen(const char*str)
{
if (*str == '\0')
{
return 0;
}
else return 1 + strlen(str + 1);
}
int main()
{
char*p = "abcdefg";
int len = strlen(p);
printf("%d\n", len);
system("pause");
return 0;
}*/
//5:非递归实现strlen
/*#include<stdio.h>
#include<windows.h>
int strlen(const char*str)
{
int count = 0;
while (*str ++)
{
count++;
}
return count;
}
int main()
{
char*p = "abcdefg";
int len = strlen(p);
printf("%d\n", len);
system("pause");
return 0;
}*/
//6:递归和非递归分别实现求n的阶乘
/*#include<stdio.h>
#include<windows.h>
#define _CRT_SECURE_NO_WARNINGS 1
int fun1(int n)
{
if (n <= 1)
{
return 1;
}
else return n*fun1(n - 1);
}
int fun2(int n)
{
int ret = 1;
while (n > 1)
{
ret *= n;
n--;
}
return ret;
}
int main()
{
int n = 4;
printf("%d\n", fun1(n));
printf("%d\n", fun2(n));
system("pause");
return 0;
}*/
//7:递归方式实现打印一个整数的每一位
/*#include<stdio.h>
#include<windows.h>
void fun(int num1)
{
if (num1 >= 10)
{
fun(num1 / 10);
}
printf( "%d ",num1 % 10);
}
int main()
{
int num = 123456;
printf("%d\n", num);
fun(num);
system("pause");
return 0;
}*/
//5:实现一个函数,可以左旋字符串中的k个字符。
//ABCD左旋一个字符得到BCDA
//ABCD左旋两个字符得到CDAB
/*#include<stdio.h>
#include<windows.h>
void fun(char *p,int k,int size)
{
k %= size;
int i = 0;
int j = 0;
char temp ;
for (i=0; i < k; i++)
{
temp = p[0];
for (j=0; j < size - 1; j++)
{
p[j] = p[j + 1];
}
p[size-1] = temp;
}
}
int main()
{
char a[] = "ABCD";
int size = strlen(a);
printf("旋转签的字符串:%s", a);
int k;
printf("请输入旋转k:");
scanf_s("%d\n",&k);
fun(a, k, size);
printf("%s", a);
system("pause");
return 0;
}*/
//6:判断一个字符串是否为另外一个字符串旋转之后的字符串。
//例如:给定s1 = AABCD和s2 = BCDAA,返回1,给定s1 = abcd和s2 = ACBD,返回0.
#include<stdio.h>
#include<windows.h>
#include<string.h>
int fun_s(char*a,char*b,int size,int sz)
{
int i = 0;
int j = 0;
int temp = 0;
int count = 0;
for (; i < size; i++)
{
for (j = i; j < sz; j++)
{
if (a[i] != b[j])
{
count ++;
}
}
}
}
void fun(char *p, int k, int size)
{
k %= size;
int i = 0;
int j = 0;
char temp;
for (i=0; i < k; i++)
{
temp = p[0];
for (j=0; j < size - 1; j++)
{
p[j] = p[j + 1];
}
p[size-1] = temp;
}
}
int main()
{
char a[] = "ABCD";
char b[] = "CDAB";
int size = strlen(a);
int sz = strlen(b);
printf("旋转签的字符串:%s", a);
int k;
printf("请输入旋转k:");
scanf_s("%d\n", &k);
fun(a, k, size);
printf("%s", a);
printf("\n");
int count=fun_s(a, b, size, sz);
if (count == 1)
printf(" 不相同%d", 0);
else printf(" 相同%d", 1);
system("pause");
return 0;
}
栈帧l练习
最新推荐文章于 2022-08-26 18:13:34 发布