码迷,mamicode.com
首页 > 编程语言 > 详细

C言语次序查找算法及代码

时间:2016-08-27 23:51:14      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:include   元素   胜利   

次序査找是一种复杂的査找算法,其完成办法是从序列的肇端元素开端,逐一将序列中的元素与所要查找的元素停止比拟,假如序列中有元素与所要查找的元素相等,那么査找胜利,假如査找到序列的最初一个元素都不存在一个元素与所要査找的元素值相等,那么标明査找掉败。接下来经过一段代码来理解次序査找的详细运用。

			#include <stdio.h> #include <stdlib.h> #include <memory.h> int ordersearch(int a[], int n, int des){ int i; for(i=0; i<n; i++) if(des==a[i]) return 1; return 0; } int main(){ int i, val; int a[8] = {32,12,56,78,76,45,43,98}; int ret; for(i=0; i<8; i++) printf("%d\t", a[i]); printf("\n请输出所要查找的元素:"); while(1){ scanf("%d", &val); fflush(stdin); ret = ordersearch(a, 8, val); if(1 == ret) printf ("查找胜利!"); else printf ("查找掉败!"); printf("\n请输出所要查找的元素:"); } return 0; }

运转后果:

32    12    56    78    76    45    43    98
请输入所要查找的元素:78
查找胜利!
请输入所要查找的元素:5
查找掉败!

剖析下面的运转后果,起首输出所要查找的元素为78,该数在所要查找的序列中是存在的,所以打印输入“查找胜利! ”。接下来输出的数值5在所要查找的序列中并不存在,因而打印输入“查找掉败!”。


本文出自 “11999725” 博客,请务必保留此出处http://12009725.blog.51cto.com/11999725/1843308

C言语次序查找算法及代码

标签:include   元素   胜利   

原文地址:http://12009725.blog.51cto.com/11999725/1843308

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!