当前位置:网站首页>[basic problems of function implementation C language]
[basic problems of function implementation C language]
2022-04-21 21:57:00 【Alderson_】
Function implementation c Basic language problems
Before you know it 2022 It's almost half done , Refuse to delay ( Da baa Da baa ), Simply sort out the code _(:з」∠)_
Catalog
Pre code : Output 100~200 prime number
------> Function implementation + sublimation ( Judge whether the input is prime )
Pre code : Output 1000~2000 Leap year within the year
-------> Function implementation + sublimation ( Judge whether the input is a leap year )
Pre code : Using binary search Ordered array The corresponding subscript of the inner element
--------> Function implementation
We are learning C I can't help feeling when I use functions in language : How pleasant it is to build and use a pure function .( Functions should be short and concise , The purer the function, the better ). Rational use of functions can reduce main() function Too miscellaneous ., Make it easier for others to read your code .
| prime number | Leap year | Two points search |
|---|---|---|
| prime number | the leap year | binary search |
100~200 Prime code :
#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;
}
In this code, we use the properties of prime numbers ( Only can be 1 and It is prime when it is divided by itself ), The two-layer cycle produces a new i Post utilization j Let's try to get rid of , And make use of flag The ingenious nature of tells us that we have found prime numbers . What we try to eliminate is the use of... That does not meet the conditions if Out of commission else Because else Including the case that the modulus is not equal to , Gu Yong j Numbers that cannot be judged are prime numbers .
Prime function implementation :
#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;
}
The principle of function implementation is almost the same , More from main Functional direction is_prime The process of transferring parameters . stay is_prime Function return 0 The role of is to screen out non prime numbers , And jump out of the loop ( Since non prime numbers can be identified by using cycles , Then prime numbers cannot be recognized through cycles , The loop will jump out , And the return value 1 To the main function ). We don't have to worry about i Whether the value of changes in the process , Because a special address is opened in memory to put i The numerical , No right value is transferred to the left value ,i It can't be changed , So the cycle is continuous .
Prime sublimation :
#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 Prime number \n", input);
continue;
}
else
{
printf("%d Not primes \n", input);
continue;
}
} while (input);
return 0;
}
It can be seen that whether an input number is a prime number , Is to find an element of the prime number in the range . It takes only one layer of circulation to find . We need to pay attention **sqrt(a)** On the condition that <=, Simply using the less than sign will lead to judgment 121、4, Wait for an even number .
1000~2000 Pre leap year code :
#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;
}
It's easy to judge leap years , Just remember the conditions .
Leap year function implementation :
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;
}
Leap year sublimation :
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 It's a leap year \n", input);
continue;
}
else
{
printf("%d It's not a leap year \n", input);
continue;
}
} while (input);
return 0;
}
This is also a problem within the scope , To find one of the elements .
Binary search ordered array pre code :
#include<stdio.h>
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
// digit 0 1 2 3 4 5 6 7 8 9
int n = 0;
scanf("%d", &n);
int sz = sizeof(arr) / sizeof(arr[1]);
// Establish left and right subscripts
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(" eureka , Subscript to be %d", mid);
break;
}
}
return 0;
}
Binary search function implementation :
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(" Can't find " );
}
else
printf(" eureka , Subscript to be :%d",j);
return 0;
}
The binary search here is very clever , Because the function is used to judge , Search is not equal to simple judgment ( If the condition is satisfied, it is prime ( You can use bool), It's a leap year ), So we need to use the function to return the subscript , And the subscript is 0~n The integer of , Only those found can be returned , If you can't find it, you can return negative numbers or decimals , Obviously negative numbers are simpler .
function + Code screenshot :
prime number :
Leap year :
Two points search :( Just posted a picture )
It's over at last , Eat a bowl of duck blood vermicelli to replenish (o( ̄︶ ̄)o).
版权声明
本文为[Alderson_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212154002648.html
边栏推荐
- Outil CSV - YAML en ligne
- Alibaba cloud OSS, user authentication and patient
- Architecture document of outsourcing student management system
- 【用例级别定义】
- 2022 crane driver (limited to bridge crane) work license question bank and online simulation examination
- Redis + caffeine two-level cache allows smooth access speed
- Ffmpeg serial 1 - environment construction
- Analysts believe that Samsung Galaxy Z fold 4 and Z flip 4 may be cheaper than their previous products
- 3D printer cr-10s cr10s Pro ender-3 Ender 3pro Ender 5 how to modify the pulse value or transmission value E of the extrusion motor when replacing the BMG extruder
- 测试用例和设计方法
猜你喜欢

How does PHP add an array element to an array

Thread safety for the first time. This article is enough

TCP / IP protocol

Push to origin / Master was rejected: error reporting solution

外包学生管理系统的架构文档

Outil CSV - YAML en ligne

在线YAML转Properties工具

RFC 之间的关系

What should you do to benefit from digital transformation?

MySQL fuzzy search and proofreading rules
随机推荐
Oracle级联删除表(不受外键约束)
ISMAR 2022 Journal Paper (TVCG)投稿交流
国产API管理神器Eolink,我爱了
我国工业互联网产业发展成效显著,但技术挑战仍是一项长期工程
mysql 模糊搜索与校对规则
面试必刷算法TOP101之背包九讲篇 TOP13
软件测试的定义
Online yaml to properties tool
在线YAML转Properties工具
Jupyter notebook has no run button
what? Your company has not set the JVM initial and maximum heap memory size to the same value?
[summary of the most complete bat must ask high concurrency in history]
The third floor of leetcode Langya list - the container with the most water
【ES6】Generator
【FeignClient】feignClient跨服务下载文件
Seven schemes of implementing distributed lock based on redis
Short video live broadcast mode enables agricultural products in remote areas to "go global"
Ffmpeg serial 3-video decoding
How to realize the automatic message sending function of wechat with vbs
如何将 ODBC 数据库与 PHP 连接?