【项目6:输出星号图】
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(j=1;j<=6;j++)
cout<<" ";
cout<<"*"<<endl;
for(i=1;i<=5;i++)
{
for(j=1;j<=6-i;j++)
cout<<" ";
cout<<"*";
for(j=1;j<=-1+2*i;j++)
cout<<" ";
cout<<"*";
cout<<endl;
}
for(j=1;j<=13;j++)
cout<<"*";
cout<<endl;
return 0;
}
【项目7:穷举法解决组合问题】
百钱百鸡问题
<pre name="code" class="cpp">#include<iostream>
using namespace std;
int main()
{
int x,y,z,count=0;
cout<<" 方案有:"<<endl;
for(x=0;x<=20;x++)
for(y=0;y<=33;y++)
for(z=0;z<=100;z++)
if(5*x+3*y+z/3.0==100&&x+y+z==100)
{
count++;
cout<<count<<"鸡翁:"<<x<<"只,鸡母"<<y<<"只,鸡雏"<<z<<"只"<<endl;
}
return 0;
}
【项目4:输出完数】
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
cout<<"1000以内的所有完数有:";
for(i=2;i<=1000;i++)
{
k=0;
for(j=1;j<=i/2;j++)
if(i%j==0) k=k+j;
if(k==i) cout<<k<<",";
}
cout<<endl;
return 0;
}