1.折半查找法寻找一组有序数中的某一个值
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int binary_search(int arr[], int k, int left, int right)
{
int mid;
while (left <= right)
{
mid= (right - left) / 2 + left;
if (arr[mid] < k)
{
left = mid + 1;
}
else
if (arr[mid] > k)
{
right = mid- 1;
}
else
return mid;
return -1;
}
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int k = 6;
int left = 0;
int right = sz - 1;
int ret = binary_search( arr[10],k,left,right);
if (ret == -1)
{
printf(“找不到了\n”);
}
else
{
printf(“找到了,下标是:%d\n”, ret);
}
return 0;
}
2.职员总薪水
#include<iostream
using namespace std ;
class Employee
{public:
Employee(string n,float s);
~Employee(); .
static void display();
private:
string name;
float salary;
static float sumsalary;
};
float Employee : :sumsalary= 0;
Employee: : Employee(string n,float s)
{ name=n ;salary=s;
sumsalary=sumsalary+salary;
Employee : :~Employee()
{ sumsalary=sumsalary-salary;}
void Employee ::display( )
{cout<<“sumsalary”<<sumsalary<<endl;}
int main()
{ Employee *e1=new Employee(“张三”,1 000);
e1 ->display();
Employee *e2=new Employee (“李四” ,3000) ;
Employee: :display();
Employee *e3=new Employee(“Jim” ,5000) ;
( *e3) . display();
delete e3;
Employee: :display();
delete e2;
Employee: :display();
delete e1;
Employee: :display()
return 0;
}