1.写一个函数找出两个整数中的最大值
#include<stdio.h>
int get_max(int x, int y)
{
int max = 0;
if (x > y)
max = x;
else
max = y;
return max;
}
int main()
{
int a = 10;
int b = 20;
int max = get_max(a, b);
printf("max = %d\n", max);
max = get_max(5, 6);
printf("max = %d\n", max);
return 0;
}
2.写一个函数可以交换两个整形变量的内容
#include<stdio.h>
void Swap1(int x, int y)//形参
{
int z = x;
x = y;
y = z;
}
void Swap2(int *pa, int *pb)
{
int z = 0;
z = *pa;
*pa = *pb;
*pb = z;
}
int main()
{
int a = 10;
int b = 20;
printf("a = %d b = %d\n", a, b);
Swap1(a, b);//实参
//当实参传给形参的时候,形参是实参的一份临时拷贝
//当形参的值修改的时候,不会影响实参
printf("a = %d b = %d\n", a, b);
Swap2(&a, &b);
printf("a = %d b = %d\n", a, b);
}
3.写一个函数打印100~200之间的素数
#include<stdio.h>
int is_prime(int i)
{
int j = 0;
for (j = 2; j <i; j++)
{
if (i%j == 0)
return 0;
}
return 1;
}
int main()
{
int i = 0;
//int count = 0;
for (i = 100; i <= 200; i++)
{
if (is_prime(i) == 1)
{
//count++;
printf("%d ", i);
}
}
//printf("\ncount = %d\n", count);
return 0;
}
4.写一个函数打印1000~2000之间的闰年
#include<stdio.h>
int is_leap_year(int n)
{
if ((n % 4 == 0) && (n % 100 != 0) || (n % 400 == 0))
return 1;
else
return 0;
}
int main()
{
int n = 0;
for (n = 1000; n <= 2000; n++)
{
if (is_leap_year(n) == 1)
printf("%d ", n);
}
return 0;
}
或者:
#include<stdio.h>
int is_leap_year(int n)
{
return((n % 4 == 0) && (n % 100 != 0) || (n % 400 == 0));
}
int main()
{
int n = 0;
for (n = 1000; n <= 2000; n++)
{
if (is_leap_year(n) == 1)
printf("%d ", n);
}
return 0;
}
4.写一个函数在一个有序数组中查找具体的某个数字n
#include<stdio.h>
int binary_search(char arr[], int k, int sz)
{
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
int main()
{
char arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int k = 7;
int sz = sizeof(arr) / sizeof(arr[0]);
int ret = binary_search(arr, k, sz);
if (ret == -1)
{
printf("没有\n");
}
else
{
printf("找到了,下标是:%d\n", ret);
}
return 0;
}
5.写一个函数,每调用一次函数,num的值就会加1
#include<stdio.h>
int Add(int *p)
{
(*p)++;
}
int main()
{
int num = 0;
Add(&num);
printf("%d\n", num);
Add(&num);
printf("%d\n", num);
return 0;
}
递归
6.按照顺序打印一个整形值得每一位,例如:输入:1234,输出:1 2 3 4
#include<stdio.h>
void print(unsigned int num)
{
if (num > 9)
{
print(num / 10);
}
printf("%d ", num%10);
}
int main()
{
unsigned int num = 1234;
print(num);
return 0;
}
7.编写不允许创建临时变量,求字符串的长度
#include<stdio.h>
#include<string.h>
int my_strlen(char*str)
{
if (*str == '\0')
return 0;
else
return 1 + my_strlen(str + 1);
}
int main()
{
int len = my_strlen("abcdef");
printf("len = %d\n", len);
return 0;
}
8.用函数求n的阶乘
#include<stdio.h>
int fac(int n)
{
if (n <= 1)
return 1;
else
return n*fac(n - 1);
}
int main()
{
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = fac(n);
printf("%d\n", ret);
return 0;
}
9.求第n个斐波那契数
方法1:(如果参数较大,可能会出现栈溢出,建议数值较大时采用方法2)
#include<stdio.h>
int fib(n)
{
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = fib(n);
printf("%d\n", ret);
return 0;
}
方法2:
#include<stdio.h>
int fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = fib(n);
printf("%d\n", ret);
return 0;
}
版权声明:本文为qq_45806781原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。