1.函数
#include<stdio.h>
void Add(int *p)
{
(*p)++;
}
int main()
{
int time=0;
Add(&time);
printf("%d\n",time);
return 0;
}
2.二分查找
#include<stdio.h>
void Add(int *p)
{
(*p)++;
}
int main()
{
int time=0;
Add(&time);
printf("%d\n",time);
return 0;
}
[hym@VM-4-7-centos C_0610]$ cat binary_search.c
#include<stdio.h>
int binary_search(int arr[],int k,int sz)
{
int left = 0;
int right = sz-1;
while(left<=right)
{
int mid = (left+right)/2;
if(arr[mid]==k)
return mid;
else if(arr[mid]>k)
right = mid-1;
else
left = mid+1;
}
return -1;
}
int main()
{
int arr [] = {1,2,3,4,5,6,7,8,9,10};
int k = 7;
int length = sizeof(arr)/sizeof(arr[0]);
int ret = binary_search(arr,k,length);
if(ret == -1)
{
printf("找不到\n");
}
else
{
printf("序号为%d\n",ret);
}
return 0;
}
3.函数调用
#include<stdio.h>
void fun(void)
{
printf("hehe\n");
}
int main()
{
fun();
return 0;
}
4. 分文件编写
#include<stdio.h>
#include"test2.h"
int main()
{
int ret = add(30,20);
printf("%d\n",ret);
return 0;
}
[hym@VM-4-7-centos C_0610]$ cat add.c
#include<stdio.h>
void Add(int *p)
{
(*p)++;
}
#include"test2.h"
int add(int x,int y)
{
return x+y;
}
5.函数
#include<stdio.h>
int get_max(int num1,int num2)
{
return num1>num2?num1:num2;
}
int main()
{
int a = 10;
int b = 20;
int max = get_max(a,b);
printf("%d\n",max);
return 0;
}
6.memset用法
#include<stdio.h>
#include<memory.h>
int main()
{
char arr[] = "hello world";
memset(arr,'x',5);
printf("%s\n",arr);
return 0;
}
7.0到200的素数个数
#include<stdio.h>
int is_prime(int n)
{
int i = 0;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main()
{
int count=0;
int i = 0;
for(i = 100;i <= 200;i++)
{
if(is_prime(i)==1)
{
count++;
printf("%d\n",i);
}
}
printf("count = %d\n",count);
return 0;
}
8.strcpy用法
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = {};
strcpy(arr2,arr1);
printf("%s\n",arr2);
return 0;
}
9. 函数调用
#include<stdio.h>
void swap(int* x,int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
int main()
{
int a =10;
int b = 20;
printf("a=%d,b=%d",a,b);
swap(&a,&b);
printf("a=%d,b=%d",a,b);
return 0;
}
10.计算闰年
#include<stdio.h>
int is_leap_year(int y)
{
if(y%4==0 && y%100!=0)
return 1;
else if(y%400 ==0)
return 1;
else
return 0;
}
int main()
{
int year = 0;
for(year = 1000;year<=2000;year++)
{
if(is_leap_year(year)==1)
{
printf("%d ",year);
}
}
}