using namespace std;
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#include <ctime>
#include <Windows.h>
// -----------------------------------------the begin of part 1 ------------------------------------------
class fibonacci
{
public:
clock_t start_1 ,end_1 ;
clock_t start_2 ,end_2 ;
int times_1 ,times_2;
unsigned long int maxunsignedint;
unsigned long int numberof_maxfib ;
unsigned long int s ; //最大整数
public:
unsigned long int Fib_1(); //求斐波那契数第n项的值
unsigned long int Fib_2(unsigned long int n);
double Fib_3(int n);
unsigned long int Fib_4(int n);
unsigned long int Maxint();
};
unsigned long int fibonacci :: Maxint()
{
maxunsignedint = 1;
while(maxunsignedint*2+1 != maxunsignedint)
{
maxunsignedint = maxunsignedint*2+1 ;
}
return maxunsignedint;
}
unsigned long int fibonacci :: Fib_1()
{
s = 0;
unsigned long int a = 0, b = 1, temp = 0 , i = 0;
Maxint() ;
for(;s < maxunsignedint;){ //从第3项开始逐项求值
if(temp > s) //如果temp大于s则发生了溢出
{
numberof_maxfib = i ;
// cout << "numberof_maxfib = " << numberof_maxfib << endl;
return i;
}
temp = s;
s = a+b; //求出第i项的值,迭代关系式
a = b;
b = s;
i++; //重置a和b的值,为求正一项作准备
}
}
unsigned long int fibonacci :: Fib_4 ( int n) //迭代算法
{
unsigned long int a = 0, b = 1, i = 1;
s = 0;
start_1 = clock();
for(i ;i < n; i++)
{ //从第3项开始逐项求值
s = a+b; //求出第i项的值,迭代关系式
a = b;
b = s;
times_1++;
}
end_1 = clock();
return s;
}
unsigned long int fibonacci :: Fib_2( unsigned long int n ) //递归算法
{ //递归
if(n <= 1)
return n;
times_2++;
return Fib_2(n-1)+Fib_2(n-2);
}
//求表示的最大斐波那契数
double fibonacci :: Fib_3( int n )
{
double gh5 = sqrt((double)5);
return(pow((1+gh5),n)-pow((1-gh5),n))/(pow((double)2,n)*gh5);
} //改进后的算法
//-------------------------------------------end of part 1--------------------------------------------------
// ------------------------------------------the begin of part 2 --------------------------------------------------
template <typename T>
class Strassen
{
public:
int** Matrix_BF(T** MatrixA, T** MatrixB, T** MatrixResult, int size); //矩阵相乘 BruteForce
void ADD(T** MatrixA, T** MatrixB, T** MatrixResult, int size);
void SUB(T** MatrixA, T** MatrixB, T** MatrixResult, int size);
T** NormalMul(T** MatrixA, T** MatrixB, T** MatrixResult, int size);
void StrassenMul(T** MatrixA, T** MatrixB, T** MatrixResult, int size);
int FillMatrix(T** Matrix, int size); //给A、B矩阵赋初值
int printMatrix(T** Matrix,int n);
// int GetMatrixSum(T** Matrix, int size); //用来计算矩阵各个元素的和,如果两种算法得出的矩阵的和相等则认为算法正确。
};
template < typename T >
int** Strassen<T> :: Matrix_BF(T** MatrixA, T** MatrixB, T** MatrixResult, int size)
{
srand((unsigned)time(NULL)); //在堆区开辟空间存放数组(若是在栈区存放数组,随着函数结束,数组名指向的地址存放的内容也会被系统释放,而堆上的空间是由程序员自动给予分配和释放的)
for(int i = 0; i < size; i++){
for( int j = 0; j < size; j++ )
{
MatrixResult[i][j] = 0;
for( int k = 0; k < size; k++ )
{
MatrixResult[i][j] += MatrixA[i][k] * MatrixB[k][j];
}
}
}
return MatrixResult;
}
template < typename T > //加
void Strassen<T> :: ADD(T** MatrixA,T** MatrixB,T** MatrixResult,int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
MatrixResult[i][j] = MatrixA[i][j] + MatrixB[i][j];
}
}
}
template < typename T > //减
void Strassen<T> :: SUB(T** MatrixA, T** MatrixB, T** MatrixResult, int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
MatrixResult[i][j] = MatrixA[i][j] - MatrixB[i][j];
}
}
}
template < typename T > //normal
int Strassen<T> :: FillMatrix(T** Matrix, int size)//给A、B矩阵赋初值
{
srand((unsigned int)time(NULL));
int inputway = 0 ;
cout << " 请选择赋值方式: " << endl
<< " 1.自动创建" << endl
<< " 2.手动输入" << endl;
while ( inputway <= 0 || inputway > 2 )
cin >> inputway;
cout << " 数组:"<<endl ;
for(int i = 0; i<size; i++)
{
if(inputway == 1)
{
for(int j = 0; j<size; j++)
{
Matrix[i][j] = rand()%5;
}
}
else if( inputway == 2 )
{
for(int j = 0;j < size; j++)
{
cout << " 请输入 arry["<< i << "][" << j << "]的值"<< endl; //此处需要修正
cout << " ";
cin >> Matrix[i][j];
}
}
}
printMatrix(Matrix,size); //输出数组 ;
}
template < typename T >
int Strassen<T> :: printMatrix(T** Matrix, int size)
{
for(int i = 0; i<size; i++)
{
cout << " ";
for(int j = 0; j<size; j++)
{
cout << Matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
template <typename T>
void Strassen<T> :: StrassenMul(T** MatrixA,T** MatrixB,T** MatrixResult,int size) // Strassen
{
if(size == 1)
{
MatrixResult[0][0] = MatrixA[0][0] * MatrixB[0][0];
// cout << MatrixResult[0][0];
}
else
{
int half_size = size/2;
T** A11;T** A12;T** A21;T** A22;
T** B11;T** B12;T** B21;T** B22;
T** C11;T** C12;T** C21;T** C22;
T** M1;T** M2;T** M3;T** M4;T** M5;T** M6;T** M7;
T** MatrixTemp1;T** MatrixTemp2;
A11 = new int*[half_size];
A12 = new int*[half_size];
A21 = new int*[half_size];
A22 = new int*[half_size];
B11 = new int*[half_size];
B12 = new int*[half_size];
B21 = new int*[half_size];
B22 = new int*[half_size];
C11 = new int*[half_size];
C12 = new int*[half_size];
C21 = new int*[half_size];
C22 = new int*[half_size];
M1 = new int*[half_size];
M2 = new int*[half_size];
M3 = new int*[half_size];
M4 = new int*[half_size];
M5 = new int*[half_size];
M6 = new int*[half_size];
M7 = new int*[half_size];
MatrixTemp1 = new int*[half_size];
MatrixTemp2 = new int*[half_size];
for(int i=0;i<half_size;i++)
{
A11[i] = new int[half_size]; A12[i] = new int[half_size]; A21[i] = new int[half_size]; A22[i] = new int[half_size];
B11[i] = new int[half_size]; B12[i] = new int[half_size]; B21[i] = new int[half_size]; B22[i] = new int[half_size];
C11[i] = new int[half_size]; C12[i] = new int[half_size]; C21[i] = new int[half_size]; C22[i] = new int[half_size];
M1[i] = new int[half_size]; M2[i] = new int[half_size]; M3[i] = new int[half_size]; M4[i] = new int[half_size];
M5[i] = new int[half_size]; M6[i] = new int[half_size]; M7[i] = new int[half_size];
MatrixTemp1[i] = new int[half_size];
MatrixTemp2[i] = new int[half_size];
}
//赋值
for(int i=0;i<half_size;i++)
{
for(int j=0;j<half_size;j++)
{
A11[i][j] = MatrixA[i][j];
A12[i][j] = MatrixA[i][j+half_size];
A21[i][j] = MatrixA[i+half_size][j];
A22[i][j] = MatrixA[i+half_size][j+half_size];
B11[i][j] = MatrixB[i][j];
B12[i][j] = MatrixB[i][j+half_size];
B21[i][j] = MatrixB[i+half_size][j];
B22[i][j] = MatrixB[i+half_size][j+half_size];
}
}
//calculate M1
ADD(A11,A22,MatrixTemp1,half_size);
ADD(B11,B22,MatrixTemp2,half_size);
StrassenMul(MatrixTemp1,MatrixTemp2,M1,half_size);
//calculate M2
ADD(A21,A22,MatrixTemp1,half_size);
StrassenMul(MatrixTemp1,B11,M2,half_size);
//calculate M3
SUB(B12,B22,MatrixTemp1,half_size);
StrassenMul(A11,MatrixTemp1,M3,half_size);
//calculate M4
SUB(B21,B11,MatrixTemp1,half_size);
StrassenMul(A22,MatrixTemp1,M4,half_size);
//calculate M5
ADD(A11,A12,MatrixTemp1,half_size);
StrassenMul(MatrixTemp1,B22,M5,half_size);
//calculate M6
SUB(A21,A11,MatrixTemp1,half_size);
ADD(B11,B12,MatrixTemp2,half_size);
StrassenMul(MatrixTemp1,MatrixTemp2,M6,half_size);
//calculate M7
SUB(A12,A22,MatrixTemp1,half_size);
ADD(B21,B22,MatrixTemp2,half_size);
StrassenMul(MatrixTemp1,MatrixTemp2,M7,half_size);
//C11
ADD(M1,M4,C11,half_size);
SUB(C11,M5,C11,half_size);
ADD(C11,M7,C11,half_size);
//C12
ADD(M3,M5,C12,half_size);
//C21
ADD(M2,M4,C21,half_size);
//C22
SUB(M1,M2,C22,half_size);
ADD(C22,M3,C22,half_size);
ADD(C22,M6,C22,half_size);
//赋值
for(int i=0;i<half_size;i++)
{
for(int j=0;j<half_size;j++)
{
MatrixResult[i][j] = C11[i][j];
MatrixResult[i][j+half_size] = C12[i][j];
MatrixResult[i+half_size][j] = C21[i][j];
MatrixResult[i+half_size][j+half_size] = C22[i][j];
}
}
//释放申请的内存
for(int i=0;i<half_size;i++)
{
delete[] A11[i]; delete[] A12[i]; delete[] A21[i]; delete[] A22[i];
delete[] B11[i]; delete[] B12[i]; delete[] B21[i]; delete[] B22[i];
delete[] C11[i]; delete[] C12[i]; delete[] C21[i]; delete[] C22[i];
delete[] M1[i]; delete[] M2[i]; delete[] M3[i]; delete[] M4[i]; delete[] M5[i]; delete[] M6[i]; delete[] M7[i];
delete[] MatrixTemp1[i]; delete[] MatrixTemp2[i];
}
delete[] A11; delete[] A12; delete[] A21; delete[] A22;
delete[] B11; delete[] B12; delete[] B21; delete[] B22;
delete[] C11; delete[] C12; delete[] C21; delete[] C22;
delete[] M1; delete[] M2; delete[] M3; delete[] M4; delete[] M5; delete[] M6; delete[] M7;
delete[] MatrixTemp1; delete[] MatrixTemp2;
}
}
class Coins
{
public:
void produceCoins(int coins[], int n);
int sum_coins(int coins[], int from, int to);
int check_coins(int coins[], int from, int to);
};
void Coins::produceCoins(int coins[], int n)//n 是硬币个数
{
int i;
srand((unsigned int)time(NULL));
int a = rand() % 20 + 1;//在1~11之间随机产生真币的重量
//先把所有硬币的重量都赋值为真币的质量
for(i = 0; i < n; i++)
{
coins[i] = a;
}
int b;//假币的重量
do
{
b = rand() % 20 + 1;//在1~21之间随机产生假币的重量
}
while(b == a);//假币的重量不能和真币的重量相同
int c = rand() % n;//随机产生假币的坐标
coins[c] = b;//把假币添加进去
}
//求coins数组下标从from到to的所有硬币的重量的和
int Coins:: sum_coins(int coins[], int from, int to)
{
int i, sum = 0;
for(i = from; i <= to; i++)
{
sum+=coins[i];
}
return sum;
}
int Coins :: check_coins(int coins[], int from, int to)
{
int n = to - from + 1;//要比较的硬币的个数
int res = 0;//假币的坐标,初始化为0
if(n == 1)
{
/*当只有1枚硬币时,coins[from] == coins[from + 2]或
coins[from] == coins[from - 2]说明该硬币是真币,否则说明
该硬币是假币*/
int a = coins[from] == coins[from + 2];
int b = coins[from] == coins[from - 2];
if (a || b)
{
res = 0;
}
else
{
res = from;
}
}
else
{
/*要比较的硬币个数为双数时,一分两半,分别求出两部分的硬币质量
之和;若是两部分的硬币质量之和相等,说明两部分都是真币;若是不
相等,说明假币在这两部分中,再细分成几个部分,递归调用该方法继
续比较,直至找到假币为止*/
if(n % 2 == 0)//要比较的硬币个数为双数
{
int a = sum_coins(coins, from, from + n/2 -1);
int b = sum_coins(coins, to - n/2 + 1, to);
if(a == b)
{
res = 0;
}
else
{
int res1=check_coins(coins, from, from + n/2 -1);
if(res1 == 0)//两部分质量之和相等
{
//继续比较其他部分
int res2=check_coins(coins, to - n/2 + 1, to);
if(res2 != 0)
res = res2;
else
res = 0;
}
else//两部分质量之和不相等
{
res = res1;
}
}
}
/*要比较的硬币个数为单数时,空出第一个,后面的部分为双数,从
中间分成两部分,然后按双数的比较方法继续比较,如果两部分质量
之和相等,再判断被空出的第一枚硬币是否是假币;如果两部分质量之
和不相等,再细分成几个部分,递归调用该方法继续比较,直至找到
假币为止*/
else//要比较的硬币的个数为单数
{
int res3 = check_coins(coins, from + 1, to);
if(res3 == 0)
{
//判断被空出的第一枚硬币是否是假币
if(coins[from] != coins[from + 1])
{
res = from;
}
}
else
{
res = res3;
}
}
}
return res;//返回假币的下标
}
class sort
{
public:
void adjust(int arr[], int len, int index);
void heapSort(int arr[], int size);
} ;
void sort::adjust(int arr[], int len, int index)
{
int left = 2*index + 1;
int right = 2*index + 2;
int maxIdx = index;
if(left<len && arr[left] > arr[maxIdx]) maxIdx = left;
if(right<len && arr[right] > arr[maxIdx]) maxIdx = right; // maxIdx是3个数中最大数的下标
if(maxIdx != index) // 如果maxIdx的值有更新
{
swap(arr[maxIdx], arr[index]);
adjust(arr, len, maxIdx); // 递归调整其他不满足堆性质的部分
}
}
void sort::heapSort(int arr[], int size)
{
for(int i=size/2 - 1; i >= 0; i--) // 对每一个非叶结点进行堆调整(从最后一个非叶结点开始)
{
adjust(arr, size, i);
}
for(int i = size - 1; i >= 1; i--)
{
swap(arr[0], arr[i]); // 将当前最大的放置到数组末尾
adjust(arr, i, 0); // 将未完成排序的部分继续进行堆排序
}
}
//------------------------------------------------------------end of part 2-------------------------------------------------------------
int main(int argc, char* argv[])
{
int s_num = 1 ;
int pc = 1;
fibonacci Fbonacci;
Strassen<int> Strassen;
Coins F_coins;
sort Sort;
long startTime_normal,endTime_normal;
long startTime_strasse,endTime_strassen;
//srand(time(0));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
// system("color FA");
cout << "------------------------------------------------------------------------------------------------------------------------" << endl;
cout << "算法与分析实验" << endl;
while( s_num ){
// system("color 0A");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
cout << "------------------------------------------------------------------------------------------------------------------------" << endl;
cout << " 1.算法分析基础"<<endl;
cout << " 2.分治法在数值问题的应用--矩阵相乘问题" << endl;
cout << " 3.减治法在组合问题中的应用--8枚硬币问题" << endl;
cout << " 4.变治法在排序问题中的应用--堆排序问题" << endl;
cout << " 5.动态规划法在图问题中的应用--全源最短路径问题" << endl;
cout << " 99.退出本实验"<<endl;
cout << " 请输入您所要执行的操作(1,2,3,4,5,99):"<<endl;
cout << "------------------------------------------------------------------------------------------------------------------------" << endl;
cin >> s_num;
// --------------------------------------------------------begin pf part1-----------------------------------------------------------------
if( 1 == s_num )
{
int pc = 1; //如果不设置这一条,下次将无法进入 ,pc已经被修改为0 ,循环无法继续
while(pc)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED);
cout <<" 1.迭代算法找出最大的n,给定具体的执行时间"<<endl
<<" 2.递归算法找出最大的n,给定具体的执行时间" <<endl
<<" 3.比较两种算法"<<endl
<<" 4.改进后的迭代算法"<<endl
<<" 0 回到主菜单"<< endl << endl;
cin >> pc;
if(pc == 1)
{
Fbonacci.start_1 = clock() ;
cout << " 整数能最大能表示第" << Fbonacci.Fib_1() << "个斐波拉契数" << endl;//maxunsignedint()
cout << " 计算机能表示的最大数" << Fbonacci.maxunsignedint << endl;
cout << " 最大的整数范围内能表示的最大的斐波拉契数:" << Fbonacci.s << endl << endl ;
Fbonacci.end_1 = clock();
cout << " 开始时间:" << Fbonacci.start_1 << endl;
cout << " 结束时间 " << Fbonacci.end_1 << endl
<< " 时间消耗:" << (Fbonacci.end_1 - Fbonacci.start_1) <<"ms" << endl ;
}
else if(2 == pc)
{
unsigned long int n = 0;
unsigned long int result = 0;
clock_t start = 0,end = 0;
Fbonacci.Fib_1();
cout << endl << endl << " 计算中........." <<endl;
start = clock();
result = Fbonacci.Fib_2(Fbonacci.numberof_maxfib+1);
end = clock();
cout << " 计算机最大能表示"<<Fbonacci.numberof_maxfib<<"个斐波那契数是"<< result <<endl;
cout << " 开始时间 " << start << endl
<< " 结束时间 " << end << endl
<< " 时间消耗:" << end -start << "ms" << endl<< endl;
}
else if(3 == pc)
{
int n = 0;
cout << " 请输入合法的参数范围n,n>0" << endl;
while(n <= 0)
cin >> n; //读入n;
Fbonacci.times_1 = 0;
Fbonacci.times_2 = 0;
Fbonacci.Fib_4(n);
Fbonacci.start_2 = clock();
int result = Fbonacci.Fib_2(n);
Fbonacci.end_2 = clock();
cout << " 第"<<n<<"个斐波那契数是"<< result <<endl;
cout << " 迭代开始时间:" << Fbonacci.start_1 <<" 递归开始时间:"<<Fbonacci.start_2 << endl
<< " 迭代结束时间:" << Fbonacci.end_1 <<" 递归结束时间:"<<Fbonacci.end_2 << endl
<< " 相差" << (Fbonacci.end_2-Fbonacci.start_2 )-(Fbonacci.end_1-Fbonacci.start_1)<<"ms"<< endl
<< " 迭代计算次数" << Fbonacci.times_1 << " 递归计算次数:" << Fbonacci.times_2 << endl
<< " 相差" << Fbonacci.times_2 - Fbonacci.times_1 << "次" << endl << endl;
cout << endl ;
}
else if(4 == pc)
{
int n = 0;
cout << " 请输入合法的参数范围,n>0"<<endl;
while(n <= 0)
cin >> n;
cout << " 第"<<n<<"个斐波那契数是"<<(int) (Fbonacci.Fib_3(n)+1)<<endl;
cout << endl;
}
else
cout << " 输入合法的参数" << endl << endl << endl;
}
}
//--------------------------------------------------------the begin of patr2---------------------------------------------------------------
else if(2 == s_num)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
int pc = 1;
while( pc )
{
cout << " .分治法在数值问题的应用--矩阵相乘问题" << endl
<< " 1.BF算法求解矩阵相乘问题" << endl
<< " 2.DAC算法求解矩阵相乘问题" << endl
<< " 3.时间复杂性分析" << endl
<< " 0.退出"<<endl;
cin >> pc; //循环读入
int n = 0;
cout << " 请输入数组的大小n,n>0" << endl;
while(n <= 0)
cin >> n;
int** Matrix1_p = new int*[n];
int** Matrix2_q = new int*[n];
int** Matrix3_result = new int*[n]; //申请三个二维数组
for(int i = 0; i<n; i++)
{
Matrix1_p[i] = new int[n];
Matrix2_q[i] = new int[n];
Matrix3_result[i] = new int[n];
}
if(1 == pc)
{
Strassen.FillMatrix(Matrix1_p, n); //初始化数组
Strassen.FillMatrix(Matrix2_q, n);
int **Result = Strassen.Matrix_BF(Matrix1_p, Matrix2_q, Matrix3_result, n);
cout << " 计算结果:"<< endl;
Strassen.printMatrix(Result, n) ;
}
else if (2 == pc)
{
Strassen.FillMatrix(Matrix1_p, n); //初始化数组
Strassen.FillMatrix(Matrix2_q, n);
cout << " Strassen算法开始时间:" << (startTime_strasse = clock()) << endl;
Strassen.StrassenMul(Matrix1_p, Matrix2_q, Matrix3_result, n);
cout << " Strassen算法结束时间:" << (endTime_strassen = clock()) << endl;
cout << " 计算结果:" ;
Strassen.printMatrix(Matrix3_result,n);
cout << " 总时间:" << endTime_strassen-startTime_strasse << endl;
// cout << " sum = " << Strassen.GetMatrixSum(Matrix3_result,n) << ';' << endl << endl << endl;
}
else if(3 == pc)
{
Strassen.FillMatrix(Matrix1_p, n); //初始化数组
Strassen.FillMatrix(Matrix2_q, n);
cout << " BF算法开始时间:" << (startTime_normal = clock()) << endl;
Strassen.Matrix_BF(Matrix1_p, Matrix2_q, Matrix3_result, n);
cout << " BF算法结束时间:" << (endTime_normal = clock()) << endl;
cout << " 总时间:" << endTime_normal-startTime_normal << endl;
// cout << " sum = " << Strassen.GetMatrixSum(Matrix3_result,n) << ';' << endl;
cout << " Strassen算法开始时间:" << (startTime_strasse = clock()) << endl;
Strassen.StrassenMul(Matrix1_p, Matrix2_q, Matrix3_result, n);
cout << " Strassen算法结束时间:" << (endTime_strassen = clock()) << endl;
cout << " 总时间:" << endTime_strassen-startTime_strasse << endl;
// cout << " sum = " << Strassen.GetMatrixSum(Matrix3_result,n) << ';' << endl << endl << endl;
}
else
cout << " 请输入正确的选项 " << endl << endl <<endl;
}
}
// part3
else if(3 == s_num )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
cout << " 3.减治法在组合问题中的应用--8枚硬币问题" << endl;
int amount_of_coin = 0 ;
cout << " 请输入硬币数量:" << endl;
while(amount_of_coin <= 2 )
{
cin >> amount_of_coin;
if(amount_of_coin <= 2)
cout << "无法比较。请输入合法的硬币数量(大于2)" << endl;
}
int* coins= new int[amount_of_coin];
F_coins.produceCoins(coins, amount_of_coin);//产生n枚硬币
//输出数组下标
for( int i = 0; i < amount_of_coin ; i++)
{
cout << " 第" << i << "枚硬币的质量: " << coins[i] << endl;
}
int end = F_coins.check_coins(coins, 0, amount_of_coin - 1);//找出假币
printf("\nend = %d\n", end);
int a = coins[end] > coins[end + 1];
int b = coins[end] > coins[end - 1];
if (a || b)
{
printf("第%d枚是假币,假币较重", end + 1);
}
else
{
printf("第%d枚是假币,假币较轻", end + 1);
}
cout << endl ;
}
// part4
else if(4 == s_num)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
cout << " 4.变治法在排序问题中的应用--堆排序问题" << endl;
int number = 3 ;
int size = 0;
while(number)
{
number = 3;
size = 0;
cout << " 请选择赋值方式: " << endl
<< " 1.手动输入" << endl
<< " 2.自动创建" << endl
<< " 0.退出" << endl << endl;
while ( number <0 || number > 2)
cin >> number;
if( 1 == number ){ //手动输入区
cout << " 请输入要排序的数组得大小(10个数据左右):" << endl;
while(size <= 0)
cin >> size; //读入数组的大小
int* newarry= new int[size];
cout << " 请输入" << size << "个数字,用空格隔开:" << endl;
for(int i = 0; i < size; i++)
cin >> newarry[i];
Sort.heapSort(newarry, size);
cout << " 排序结果:"<<endl;
cout<< " " ;
for(int i = 0; i < size; i++)
cout << newarry[i] << " ";
cout << endl;
}
else if (2 == number ){ //自动生成区
clock_t start_time = clock();//计时函数
cout << " 请输入要排序的数组得大小(1500~3000个数据左右):" << endl;
while(size <= 0)
cin >> size; //读入数组的大小
int* newarry= new int[size];
srand(unsigned(time(NULL)));//time()用系统时间初始化种。为rand()生成不同的随机种子。
cout<<" 随机函数生成的随机数是: "<<endl;
for(int i = 0; i < size; i++)
{
newarry[i] = rand()%1500;
cout << newarry[i] << "\t";
}
cout << endl << " 随机数经过堆排序后的序列: " << endl;
Sort.heapSort(newarry,size);
for(int i = 0; i < size; i++)
cout << newarry[i] << "\t";
cout<< endl << " 时间消耗: " << double(clock()-start_time) << "ms" << endl;
}
}
}
// part5
else if(5 == s_num)
{
cout << " 5.动态规划法在图问题中的应用--全源最短路径问题" << endl;
}
// part6
else if(99 == s_num)
{
cout << " 循环已退出"<<endl;
s_num = 0 ;
}
else
cout << " 请输入正确选项"<<endl;
// part7
}
return 0;
}