#include <iostream.h>
//#include <stdlib.h>
typedef bool logical;
void binarySearch(int*numArray,int item,int n,bool &found);//折半查找
void bobSort(int*numArray,int n);//冒泡排序
void printBinary(int n);//打印一个int整数的二进制表示
void main()
{
// int item,n=0;
// int *array=NULL;
// logical found=false;
//
// cout<<"请输入数组元素个数:";
// cin>>n;
// array=(int*)malloc(n*sizeof(int));
// cout<<"请输入数组元素:";
// for(int i=0;i<n;i++)
// cin>>array[i];
// cout<<"请输入要查找的数组元素:";
// cin>>item;
// bobSort(array,n);
// cout<<endl;
// binarySearch(array,item,n,found);
// if (found)
// cout<<"有"<<endl;
// else
// cout<<"没有"<<endl;
printBinary(17);
}
void binarySearch(int*numArray,int item,int n,bool &found)
{
int first=0,last=n-1;
int mid;
while(!found&&first<=last)
{
mid = (first + last)/2;
if(item <numArray[mid])
last=mid-1;
else if(item>numArray[mid])
first=mid+1;
else
{ found=true;
break;
}
}
}
void bobSort(int*numArray,int n)
{
int i,j,temp;
for (i=0;i<n;i++)
for (j=i+1;j<n;j++)
{
temp = numArray[i];
if (temp>numArray[j])
{
numArray[i]=numArray[j];
numArray[j]=temp;
}
}
cout<<"冒泡排序后的数组为:";
for(i=0;i<n;i++)
cout << numArray[i]<< ' ';
}
void printBinary(int n)//打印二进制数
{
unsigned int mask=0x80000000;//移位运算符>>在处理带符号数时移位后最高位是补符号位,无符号则补0
for (int i=0;i<32;i++)
{
if (mask&n)
cout<<'1';
else
cout<<'0';
mask = mask>>1;
}
cout <<endl;
}
复习一下算法,提神醒脑
最新推荐文章于 2024-08-19 14:07:26 发布