C++作业4

项目2-分数的累加

#include<iostream>  
using namespace std;  
int main()  
{  
    double i=1,j=3,sum=0,k=1;  
    while(i<=10)  
    {  
        sum=sum+k*((2*i-1)/(2*i+1));  
        i++; 
		k=-k;  
    }  
    cout<<"1/3-3/5+5/7-7/9...+19/21="<<sum<<endl;  
    return 0;  
}  
项目 6 :输出星号图
#include<iostream>  
using namespace std;  
int main()  
{  
    int i,j;  
	for(i=1;i<=5;i++)     
    {  
        for(j=1; j<=6-i;j++)            
            cout<<" ";  
        cout<<"*";    
        for(j=1; j<=2*i-3; j++)   
            cout<<" "; 
		if(i==1)
			cout<<endl;
		else
			cout<<"*"<<endl;   
    }   
    for(i=1; i<=11; i++)     
        cout<<"*";    
    cout<<endl;    
    return 0;  
}  
项目 7 :穷举法解决组合问题—百钱百鸡问题
#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<=300;z++)  
                if(5*x+3*y+z/3==100&&x+y+z==100&&z%3==0)  
                    cout<<"鸡翁"<<x<<"只"<<","<<"鸡母"<<y<<"只"<<","<<"鸡雏"<<z<<"只"<<"。"<<endl;  
                return 0;  
}  


你可能感兴趣的:(C++作业4)