C语言的基础经典查找算法

待到秋来九月八,我花开后百花杀

遍历查找

思路:从数组头循环遍历至数组尾,对比查找,知道对比成功,然后输出。

代码实现:

#include<stdio.h>
#pragma warning(disable:4996)
int main()
{
    
    
 int a[] = {
    
     0,1,3,4,6,7,9,19,32,37 };
 int num = sizeof(a) / sizeof(a[0]);
 int n = 0;
 scanf("%d", &n);
 int i = 0;
 for (; i < num; i++) {
    
    
  if (a[i] == n) {
    
    
   printf("ture\n");
   break;
  }
 }
 if (i >= num) {
    
    
  printf("false\n");
 }
 system("pause");
 return 0;
}

二分查找

思路:二分査找就是折半查找,其基本思想是:首先选取表中间位置的记录为关键字,将其关键字与给定关键字 n 进行比较,若相等,则査找成功;若 n 值比该关键字值大,则要找的元素一定在右子表中,则继续对右子表进行折半查找:若 n 值比该关键字值小,则要找的元素一定在左子表中,继续对左子表进行折半査找。如此递推,直到査找成功或査找失败(或査找范围为 0)。

画图解析:
在这里插入图片描述
代码实现:

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int Search(int a[], int n, int y)
{
    
    
 int L = 0;
 int R = n - 1;
 while (L < R) {
    
    
  int mid = (L + R) / 2;
  if (y > a[mid]) {
    
    
   L = mid + 1;
  }
  else if (y < a[mid]) {
    
    
   R = mid - 1;
  }
  else {
    
    
   return 1;
  }
 }
 return 0;
}
int main()
{
    
    
 int a[] = {
    
     0,1,3,4,6,7,9,19,32,37 };
 int num = sizeof(a) / sizeof(a[0]);
 int b = 0;
 int result = 0;
 printf("Please Enter your number:\n");
 while(1){
    
    
  scanf("%d", &b);
  result = Search(a, num, b);
  if (result){
    
    
   printf("Yes!you get!");
   break;
  }
  else{
    
    
   printf("No!you don't get!Please try again:\n");
  }
 }
 system("pause");
 return 0;
}

猜数字游戏

有了上面查找算法的基础,我们来做一个简单的小时候经常的小游戏吧!

为了提升小游戏的真实实用性,加入了登录程序界面。

#include<stdio.h>
#include<windows.h>
#include<time.h>
#pragma warning(disable:4996)

const char *name = "xuenuo";
const char *key = "104934";

void Showtime();
void Login();
void Game();
void Play();

void Login()//登录
{
    
    
 int chance = 3;
 int time = 1;
 while (chance > 0) {
    
    
  char N[50];
  char K[50];
  printf("Please Enter your name:\n");
  scanf("%s", &N);
  printf("Please Enter your key:\n");
  scanf("%s", &K);
  if (0 == strcmp(name, N) && 0 == strcmp(key, K)) {
    
    
   Showtime();
   break;
  }
  else {
    
    
   printf("Your Enter Message Is Error!\n");
   chance--;
   printf("You have %d more chances\n", chance);
  }
  if (chance <= 0) {
    
    
   printf("You have no chance,Lock %d s,Wait....\n", time * 10);
   Sleep(10000 * time);
   chance = 3;
   time++;
   if (time >= 3) {
    
    
    printf("You never get a chance!\n");
    break;
   }
  }
 }
}

void Showtime()//登录成功展示
{
    
    
 char word_str[] = "Login success!";
 char sym_str[] = "##############";
 int left = 0;
 int right = strlen(word_str) - 1;
 printf("%s\n", sym_str);
 while (left <= right) {
    
    
  sym_str[left] = word_str[left];
  sym_str[right] = word_str[right];
  printf("%s\r", sym_str);
  left++, right--;
  Sleep(500);
 }
 printf("\n");
}

void Game()
{
    
    
 while (1) {
    
    
  int select = 0;
  printf("###################################\n");
  printf("######### 1. Play   2. Exit #######\n");
  printf("###################################\n");
  printf("Please Select: ");
  scanf("%d", &select);
  switch (select)
  {
    
    
  case 1:
   Play();
   break;
  case 2:
   printf("See you again!\n");
   return;
  default:
   printf("Enter Error! Try Again\n");
   break;
  }
 }
}

void Play()
{
    
    
 srand((unsigned long)time(NULL));
 int x = rand() % 200 + 1;//[1..200]
 int data;
 while (1) {
    
    
  printf("Guess: ");
  scanf("%d", &data);
  if (data > x) {
    
    
   printf("guess Big!\n");
  }
  else if (data < x) {
    
    
   printf("guess Small!\n");
  }
  else {
    
    
   printf("guess Right!\n");
   break;
  }
 }
}

int main()
{
    
    
    Login();
 Game();
 system("pause");
 return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40893595/article/details/102889365