项目2-分数的累加:
#include <iostream>
using namespace std;
int main ()
{
double sum=0,x=0;int i=1,k=1;
while(i<=19)
{
x=i*1.0/(i+2)*k;
k=-k;
sum+=x;
i=i+2;
}
cout<<"sum="<<sum<<endl;
return 0;
}
百钱百鸡问题:
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
for(x=0;x<=20;x++)
for(y=0;y<=33;y++)
for(z=0;z<=100;z++)
if(x+y+z==100&&5*x+3*y+z/3==100)
cout<<":鸡翁"<<x<<"只,鸡母"<<y<<"只,鸡雏"<<z<<"只。\n";
return 0;
}
项目6:输出星号图:
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=0;j<6-i;j++)
cout<<" ";
cout<<"*";
for(j=1;j<=2*i-3;j++)
cout<<" ";
if(i==1)cout<<endl;
if(i>1)cout<<"*"<<endl;
}
for(j=1;j<=11;j++)
cout<<"*";
cout<<endl;
return 0;
}