/* 用一个函数求素数*/
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
int is_prime(int num)
{
int j = 0;
for (j = 2; j <= sqrt(num); j++)
{
if (num%j == 0)
return 0;
}
return 1;
}
int main()
{
int num = 0;
int ret = is_prime(0);
{
if (ret == 0)
printf("是\n");
else
printf("不是\n");
}
getchar();
return 0;
}
//二分法
#include<stdio.h>
#include<stdlib.h>
int binary_search(int arr[], int key, int left,int right)
{
while (left <= right)
{
int mid = left + ((right - left) >> 1);
if (key > arr[mid])
left = mid + 1;
else if
(key < arr[mid])
right = mid - 1;
else
return mid;
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int key = 8;
int ret = binary_search( arr, key,3,8);
int left = 1;
int right = 1;
if (ret == -1)
{
printf("找不到\n");
}
else
{
printf("找到了\n",ret );
}
getchar();
return 0;
}