当前位置:网站首页>【函数实现c语言基础问题】
【函数实现c语言基础问题】
2022-04-21 21:54:00 【Alderson_】
函数实现c语言基础问题
不知不觉2022都快过半了,拒绝拖延(达咩达咩),简单梳理了下代码_(:з」∠)_
目录
------>函数实现 + 升华 (判断输入是否为素数)
------->函数实现 + 升华 (判断输入是否为闰年)
-------->函数实现
我们在学习C语言中的函数时不禁会感慨:建立并使用一个纯粹的函数是多么的愉悦.(函数应该是短小精悍的,功能越纯粹越好). 合理的利用函数可以减少 main()函数 过于冗杂.,更方便他人阅读你的代码.
| 素数 | 闰年 | 二分查找 |
|---|---|---|
| prime number | the leap year | binary search |
100~200素数前期代码:
#include<math.h>
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int flag = 1;
for (j = 2; j <= sqrt(i); j++)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
这个代码我们利用了素数的性质(只有能被1 和 其本身整除时才是素数),两层循环产生一个新i后利用j来试除,并利用flag的巧妙性质来告诉我们找到了素数。我们试除的是不满足的条件使用的 if 不能使用else是因为else包含了模数不等于情况,顾用j无法判断的数就是素数。
素数函数实现:
#include<math.h>
int is_prime(int n)
{
int j = 0;
for (j = 2; j <= sqrt(n); j++)
{
if (n % j == 0)
{
return 0;
}
}
return 1;
}
#include<stdio.h>
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", count);
return 0;
}
函数实现的原理大抵是差不多的,多了从main函数向is_prime的传参过程。在 is_prime 函数中return 0 的作用是筛查出非素数,并且跳出循环(既然利用循环可以甄别出非素数,那么素数经过循环无法被识别,循环会跳出,并返回值 1 到主函数)。我们不用担心 i 的值在这过程中是否改变,因为在内存中开辟了一块专门的地址放 i 的数值,没有对左值传输右值,i 是无法改变的,所以循环是连续的。
素数升华:
#include<math.h>
int is_prime(int a)
{
int j = 0;
for (j = 2; j <= sqrt(a); j++)
{
if (a % j == 0)
{
return 0;
}
}
return 1;
}
#include<stdio.h>
int main()
{
int input = 0;
do
{
scanf("%d", &input);
if (is_prime(input) == 1)
{
printf("%d是素数\n", input);
continue;
}
else
{
printf("%d不是素数\n", input);
continue;
}
} while (input);
return 0;
}
可见判断一个输入的数是否为素数,是求范围内素数的一个元素。只用一层循环就可以求出。我们需要注意**sqrt(a)**的条件为 <=,单纯的用小于号会导致判断121、4,等为偶数。
1000~2000闰年前期代码:
#include<stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1000; i <= 2000; i++)
{
if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d", count);
return 0;
}
判断闰年还是很简单的,记住条件就行。
闰年函数实现:
int is_leap_year(int i)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
return 1;
}
else
return 0;
}
#include<stdio.h>
int main()
{
int count = 0;
int y = 0;
for (y = 1000; y <= 2000; y++)
{
if (is_leap_year(y) == 1)
{
count++;
printf("%d ", y);
}
}
printf("\ncount = %d", count);
return 0;
}
闰年升华:
int is_leap_year(int i)
{
if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0))
return 1;
else
{
return 0;
}
}
#include<stdio.h>
int main()
{
int input = 0;
do
{
scanf("%d", &input);
if (is_leap_year(input) == 1)
{
printf("%d是闰年\n", input);
continue;
}
else
{
printf("%d不是闰年\n", input);
continue;
}
} while (input);
return 0;
}
这个也是求范围内问题,变成求其中一个元素。
二分查找有序数组前期代码:
#include<stdio.h>
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
//位数 0 1 2 3 4 5 6 7 8 9
int n = 0;
scanf("%d", &n);
int sz = sizeof(arr) / sizeof(arr[1]);
//建立左右下标
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < n)
{
left = mid + 1;
}
else if (arr[mid] > n)
{
right = mid - 1;
}
else
{
printf("找到了,下标为 %d", mid);
break;
}
}
return 0;
}
二分查找的函数实现:
int binary_search(int a, int arr[], int sz)
{
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = (right + left) / 2;
if (arr[mid] > a)
right = mid - 1;
else if (arr[mid] < a)
left = mid + 1;
else
return mid;
}
if (left > right)
{
return -1;
}
}
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[1]);
int j = binary_search(n, arr, sz);
if (j == -1)
{
printf("找不到" );
}
else
printf("找到了,下标为:%d",j);
return 0;
}
这里的二分查找就非常的巧妙,因为使用了函数来判断,而查找不等于简单的判断(如满足条件就是素数(可用bool),就是闰年),所以我们需要用函数返回下标,而下标是0~n的整数,只能返回找到的,那找不到的可以返回负数或者小数,明显负数更加简单。
运行+代码截图:
素数:
闰年:
二分查找:(就贴了一个图)
终于敲完了,吃碗鸭血粉丝补补(o( ̄︶ ̄)o).
版权声明
本文为[Alderson_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Alderson_/article/details/124318559
边栏推荐
- 2022 high altitude installation, maintenance and demolition of examination question bank and simulated examination
- [charming Java] - data types and variables
- 短视频直播模式让偏远地区农产品“走出去”
- 微服务,中台,RPA和低代码火热背后的一些冷思考
- MySQL最全整理(面试题+笔记+导图),面试大厂不再被MySql难倒
- POI Point of interesting.
- How does wechat applet realize the function of jumping from commodity list to commodity details page
- Hard core strength, recognized by many parties | cloud expansion technology, as the core manufacturer of RPA, was selected into the 2022 China RPA procurement guide
- 135, 137, 138, 139 and 445 port explanation and closing method
- PHP数组函数extract 使用详解
猜你喜欢

在線CSV轉YAML工具

How does PHP add an array element to an array

helm安装jenkins(官方)

WPF data-driven method for modifying binding

Practice of JVM custom class loader in code extensibility

Pay off 900000 housing loans to get the resignation certificate? Tencent response: inconsistent with the actual situation

危机四伏,卡士酸奶的高端人设还立得住吗?

LeetCode琅琊榜第三层-盛最多水的容器

Helm install Jenkins (official)

Tgip-cn 038 registration | in depth analysis of Apache pulsar source code reading correct posture (I)
随机推荐
JVM自定义类加载器在代码扩展性的实践
微信小程序怎么实现商品列表跳转商品详情页功能
【JVM】10道不得不会的JVM面试题
LeetCode琅琊榜第三层-盛最多水的容器
Mswep data NC format to TIF format
How does wechat applet realize the function of jumping from commodity list to commodity details page
【题解】[SDOI2012] 吊灯
Restcloud ETL开箱即用-永久免费
How does PHP add an array element to an array
Makefile file configure executable file and cflags parameter
In depth analysis of static, const, volatile, extern and register keywords
分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜
Hospital-Oriented RFID Service
危机四伏,卡士酸奶的高端人设还立得住吗?
TCP / IP protocol
Common SQL optimization
我国工业互联网产业发展成效显著,但技术挑战仍是一项长期工程
Linux MySQL common commands
JS to realize automatic scrolling of announcements
How to realize the automatic message sending function of wechat with vbs