#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
get_max(int x,int y)
{
int z = 0;
if (x > y)
{
z = x;
}
else
{
z = y;
}
return z;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
int max = get_max(a, b);
printf("%d", max);
return 0;
}
无法完成交换
void Swap(int x, int y)
{
int z = 0;
z = x;
x = y;
y = z;
}
int main()
{
int a = 10;
int b = 20;
//写函数,交换2个整形变量的值
printf("交换前:a=%d b=%d\n", a, b);
Swap(a, b);
printf("交换后:a=%d b=%d\n", a, b);
}
int main()
{
int a = 0;
int* pa = &a;
*pa = 20;
printf("%d", a);
return 0;
}
void Swap2(int* pa, int* pb)
{
int z = 0;
z = *pa;
*pa = *pb;
*pb = z;
}
int main()
{
int a= 10;
int b = 20;
//写函数,交换2个整形变量的值
Swap1(a, b);//传值调用
printf("交换前:a=%d b=%d\n", a, b);
Swap2(&a, &b);//传址调用
printf("交换后:a=%d b=%d\n", a, b);
}
函数判断100-200 素数
int is_prime(int n )
{
int j = 0;
for (j = 2; j < n; j++)
{
if (n % j == 0)
{
return 0;
}
}
return 1;
}
int is_prime(int n)
{
int j = 0;
for (j = 2; j <=sqrt(n); j++)
{
if (n % 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("\n%d\n", count);
return 0;
}
判断1000-2000的闰年
判断是闰年返回1
不是闰年,返回0
int is_leap_year(int n)
{
if ((n % 4 == 0) && (n % 100 != 0) || (n % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int y = 0;
int count = 0;
for (y = 1000; y <= 2000; y++)
{
if (is_leap_year(y) == 1)
{
count++;
printf("%d", y);
}
}
printf("\ncount=%d\n", count);
return 0;
}
传过来的地址名可以一样,也可以不一样
int binary_search(int a[], int k, int s)
{
int left = 0;
int right = s - 1;
while (left<=right)
{
int mid = (left + right) / 2;
if (a[mid] > k)
{
right = mid - 1;
}
else if (a[mid] < k)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;//找不到
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int key = 7;
int sz = sizeof(arr) / sizeof(arr[10]);
//找到了就返回找的位置的下标
//找不到就返回-1
int ret = binary_search(arr, key, sz);
if (-1 == ret)
{
printf("找不到\n");
}
else
{
printf("找到了,下标是:%d\n", ret);
}
return 0;
}
num ++
void Add(int* p)
{
(*p)++;
}
int main()
{
int num = 0;
Add(&num);
printf("%d\n", num);
Add(&num);
printf("%d\n", num);
Add(&num);
printf("%d\n", num);
}
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
return 0;
}
int main()
{
int a = 10;
int b = 20;
//函数声明一下,告知
int Add(int, int);
int c = Add(a, b);
printf("%d\n", c);
return 0;
}
int Add(int x, int y)
{
return x + y;
}
//函数递归
int main()
{
printf("hehe\n");
main();
return 0;
}
接受一个整型值(无符号),
按照顺序打印它的每一位.例如:输入1234,打印1 2 3 4
void print(unsigned int n)
{
if (n > 9)
{
print(n / 10);
}
printf("%d", n % 10);
}
int main()
{
unsigned int num = 0;
scanf("%u", &num);
//递归,函数自己调用自己
print(num);//print函数可以打印参数部分数字的每一位
return 0;
}
写递归代码的时候
1.不能死递归,都要有跳出条件,每次递归逼近跳出条件
2.递归层次不能太深
int main()
{
char arr[] = "bit";
printf("%d\n", strlen(arr));
return 0;
}
int my_strlen(char* str)
{
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "bit";
//['b'] ['i'] ['t'] ['\0']
printf("%d\n",my_strlen(arr));
return 0;
}
int my_strlen(char* str)
{
if (*str != '\0')
return 1 + my_strlen(str+1);
else
return 0;
}
int main()
{
char arr[] = "bit";
//['b'] ['i'] ['t'] ['\0']
printf("%d\n", my_strlen(arr));
return 0;
}
n阶乘
int main()
{
int n = 0;
scanf("%d", &n);
int i = 0;
int ret = 1;
for (i = 1; i <= n; i++)
{
ret = ret * i;
}
printf("%d\n", ret);
return 0;
}
递归方法写n阶乘
int Fac(int n)
{
if (n <= 1)
{
return 1;
}
else
{
return n * Fac(n - 1);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fac(n);
printf("%d\n", ret);
return 0;
}
求n个斐波拉契数
1,1,2,3,5,8,13,21,34,55
int count = 0;
//递归可以求解,但是效率太低
int Fib(int n)
{
//统计第三个斐波拉契的计算次数
if (n == 3)
{
count++;
}
if (n <= 2)
{
return 1;
}
else
{
return Fib(n - 1) + Fib(n - 2);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
printf("count=%d\n", count);
return 0;
}
算法优化
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;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
return 0;
}