⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 find.cpp

📁 这是一个在一个规则矩阵中快速查找某个数值的小程序。
💻 CPP
字号:
// find.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>

main()
{int m=6,n=5;
 int array[6][5]={{1,3,5,7,9},{2,4,6,8,10},{4,7,8,9,13},{5,9,10,15,16},{6,12,15,17,18},{7,15,17,20,24}};
 int i,count;
 int num;
 printf("Please input a number:");
 scanf("%d",&num);                                  /*输入一个用于比较的数值*/
 printf("number line colume\n");                      /*输出结果的栏目*/
 int high,low,mid;
													/*定义二分查找的最大、最小和中间位置*/
 if((num>array[m-1][n-1])||(num<array[0][0]))
   printf("NULL\n");
 count=0;
 for(i=0;i<m;i++)                                   /*按行进行二分查找,完成一行后进行下一行的对比查找*/
   {high=n-1;
    low=0;
	mid=(high+low)/2;
    while(low<high)
     {if(num==array[i][low])                        /*如果是行的第一个元素,输出*/
 {printf("%4d,%4d,%4d\n",num,i,low);
 count=count+1;break;}
      else if(num==array[i][high])                  /*如果是行的最后一个元素,输出*/
	  {printf("%4d,%4d,%4d\n",num,i,high);
	  count=count+1;break;}
      else if((num>array[i][low])&&(num<array[i][high]))    
                                                    /*如果用于比较的元素num在这一行的范围内,进行比较*/
	  { if(num==array[i][mid])
	  {printf("%4d,%4d,%4d\n",num,i,mid);
	  count=count+1;break;}
          else if(num>array[i][mid])
            {low=mid+1;
             mid=(low+high)/2;}
          else
            {high=mid-1;
             mid=(low+high)/2;}
	  }
   //   if(low+1==high)
	else break;                                  /*如果用于比较的元素num不在这一行的范围内,进行下一行*/
     }
   }
 if(count==0)printf("NULL\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -