zoukankan      html  css  js  c++  java
  • 顺序查找

    #include<stdio.h>
    int fun(int a[],int n,int x)
    {
       int i;
       for(i=0;i<n;i++)
    {
          printf("该点是%d
    ",a[i]);
    if(a[i]==x) 
          {         puts("找到
    ");     return(i);     }//查找到,返回位置
    }
    if(i==n)     return(-1);//没有找到,返回-1
    }
    
    main()
    {
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    int p,m;
    scanf("%d",&p);
    printf("你要查找%d.
    ",p);
    m=fun(a,10,p);
    if(m==-1) printf("没有找到啊
    ");
    }
    
    
    
    *************************************************************************************
    
    
    
    
    
    
    #include<stdio.h>
     main()
    {
     int a[10]={1,2,3,4,5,6,7,8,9,10};
     int i,x,y;
     printf("输入你要查找的数:
    ");
     scanf("%d",&x);
     y=0;                        //标记是否已找到,y=1表是找到了,y=0表示没找到
     for(i=0;i<10;i++)           //循环,把x和数组中的元素一个个比较
     {
      if(x==a[i])             //如果x=a[i]说明已经找到
      {
       y=1;                //把y变成1,说明已经找到了              
       printf("你要查找的数%d在第个%d位置
    ",x,i+1);    //输出找到的相关信息
       break;                        //跳出循环
      }
     }
     if(y==0)printf("无法找到你要查找的数
    ");          //y=0表示找不到
    }
    

      

  • 相关阅读:
    iframe跨域
    changePage() 页面跳转
    APACHE启动失败是SYSTEM对apache目录没权限导致
    git使用中出现的错误
    python面试总结
    python面试30-40题
    python面试1-30题
    购物车的基本流程
    vue的基础知识
    三大框架的对比
  • 原文地址:https://www.cnblogs.com/wc1903036673/p/3488282.html
Copyright © 2011-2022 走看看