1.百元买百鸡问题:
已知公鸡5元一只,母鸡3元一只,小鸡1元一只,用100元买100只鸡,问公鸡、母鸡、小鸡各多少只?
完整代码:
#include<iostream>
using namespace std;
void Chicken()
{
int x,y,z;
int count=0;
for(x=0;x<=20;x++)
{
for(y=0;y<=33;y++)
{
z=100-x-y;
if((z%3==0)&&(5*x+3*y+z/3==100))
{
count++;
cout<<"公鸡:"<<x<<" 母鸡:"<<y<<" 小鸡:"<<z<<endl;
}
}
}
if(count==0)
cout<<"问题无解"<<endl;
}
int main()
{
Chicken( );
return 0;
}
运行结果:
2.查找问题中的蛮力法:
串匹配问题:
1>BF算法:
完整代码:
#include<iostream>
using namespace std;
int BF(char S[],char T[])
{
int i=0,j=0,index=0;
while(S[i]!='\0'&&T[j]!='\0'){
if(S[i]==T[j]){
i++;
j++;
}else{
index++;
i=index;
j=0;
}
}
if(T[j]=='\0'){
return index+1;
}else{
return 0;
}
}
int main()
{
char S[]="abcabcabcaccb";
char T[]="abcacc";
int index=BF(S,T);
cout<<"子串"<<T<<"在主串"<<S<<"中的位置是:"<<index<<endl;
return 0;
}
运行结果:
2>KMP算法
模式T的next值的求解思路:
1.需要从j=1开始依次求next[j]的值(next[0]=-1)。——第一层for循环。
2.相等子串的最大长度为j-1,所以从先将j-1赋值给len,并且循环一次len减1直至len等于1。(若len=0则next[j]=0在此层for循环执行完毕后执行。)——第二层for循环。
3.循环比较0到len-1与j-len到j-1是否相等,不相等则跳出循环。若执行完此for循环未中途跳出循环,则next[j]=len。——第三层for循环。
完整代码:
#include<iostream>
#include <string.h>
using namespace std;
void get_next(char T[],int next[]){
int i,j,len;
next[0]=-1;
for(j=1;T[j]!='\0';j++)
{
for(len=j-1;len>=1;len--)
{
for(i=0;i<len;i++)
if(T[i]!=T[j-len+i]) break;
if(i==len)
{
next[j]=len;
break;
}
}
if(len<1) next[j]=0;
}
}
int KMP(char S[20],char T[20]){
int i=0,j=0;
int next[80];
get_next(T,next);
while(S[i]!='\0'&&T[j]!='\0'){
if(S[i]==T[j]){
i++;j++;
}else{
j=next[j];
if(j==-1){
i++;
j++;
}
}
}
if(T[j]=='\0'){
return (i-strlen(T)+1);
}
else{
return 0;
}
}
int main()
{
char S[]="abcabcabcaccb";
char T[]="abcacc";
int index=KMP(S,T);;
cout<<"子串"<<T<<"在主串"<<S<<"中的位置是:"<<index<<endl;
return 0;
}
运行结果:
3.排序问题中的蛮力法:
1>选择排序
思路:
1.从i=0开始对n个记录进行n-1趟选择排序——第一层for循环
2.从j=i+1开始在无序区中查找最小记录,并将最小记录的下标值赋给index——第二层for循环
3.将r[index]与r[i]进行交换
完整代码:
#include<iostream>
using namespace std;
void SelectSort(int r[ ],int n)
{
int i,j,temp,index;
for(i=0;i<n-1;i++)//对n个记录进行n-1趟选择排序
{
index=i;
for(j=i+1;j<n;j++)//在无序区中查找最小记录
{
if(r[j]<r[index])
{
index=j;
}
}
if(index!=i)
{
temp=r[i];r[i]=r[index];r[index]=temp;//将要进入有序区的r[i]与无序区的最小记录交换
}
}
}
int main()
{
int r[]={1,6,7,5,3,8,9,2};
SelectSort(r,8);
for(int i=0;i<8;i++)
cout<<r[i]<<" ";
return 0;
}
运行结果:
2>起泡排序
完整代码:
#include<iostream>
using namespace std;
void BubbleSort(int r[ ],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)//共扫描n-1次
{
for(j=0;j<n-1-i;j++)
{
if(r[j]>r[j+1])
{
temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
}
}
}
}
int main()
{
int r[]={1,6,7,5,3,8,9,2};
BubbleSort(r,8);
for(int i=0;i<8;i++)
cout<<r[i]<<" ";
return 0;
}
运行结果:
上述代码在待排序序列基本有序的情况下效率很低,因此做如下优化。在一趟起泡排序中,没有记录相交换(exchange=0),则表明序列已经有序,算法将终止。
改进代码:
#include<iostream>
using namespace std;
void BubbleSort(int r[ ],int n)
{
int bound,exchange=n-1;
while(exchange!=0)
{
bound=exchange;exchange=0;
for(int j=0;j<bound;j++)
{
if(r[j]>r[j+1])
{
int temp=r[j];r[j]=r[j+1];r[j+1]=temp;
exchange=j;
}
}
}
}
int main()
{
int r[]={1,6,7,5,3,8,9,2};
BubbleSort(r,8);
for(int i=0;i<8;i++)
cout<<r[i]<<" ";
return 0;
}
运行结果:
4.组合问题中的蛮力法
1>0/1背包问题
2>任务分配问题
5.图问题中的蛮力法
1>哈密顿回路问题
2>TSP问题
6.几何问题中的蛮力法
1>最近对问题
完整代码:
#include<iostream>
using namespace std;
int ClosestPoints(int x[],int y[],int n)//第i个点的坐标存为x[i]y[i],第j个表的坐标存为x[j]y[j]
{
int minDist=1000,dis,index1,index2;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
dis=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
if(dis<minDist)
{
minDist=dis;
index1=i;
index2=j;
}
}
}
cout<<"最近的点对是第"<<index1<<"和"<<index2<<"对点"<<endl;
cout<<"距离是:"<<minDist<<endl;
return minDist;
}
int main()
{
int x[]={1,3,5,1};
int y[]={1,2,4,2};
ClosestPoints(x,y,4);
return 0;
}
运行结果:
2>凸包问题
完整代码:
#include<iostream>
using namespace std;
void ConvexHull(int x[ ],int y[ ],int n)
{
int i,j,k,sign1,sign2;//sign1和sign2表示两个半平面
int A,B,C;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
sign1=0;sign2=0;
A=y[i]-y[j];
B=x[j]-x[i];//注:不是x[i]-x[j],可用两点式求直线方程推导
C=x[i]*y[j]-x[j]*y[i];
for(k=0;k<n;k++)
{
if(k!=i&&k!=j)
{
if(A*x[k]+B*y[k]+C>0)
sign1=1;
else sign2=1;
if(sign1==sign2)
break;//两个半平面均有点
}
}
if(k==n)
cout<<"("<<i<<","<<j<<")"<<endl;
}
}
}
int main()
{
int x[]={1,3,5,1,2,2,2};
int y[]={1,1,4,3,2,3,6};
ConvexHull(x,y,7);
return 0;
}
运行结果: