您的位置:首页 > 其它

第四周作业1

2014-03-31 22:26 309 查看
第三章


1.完成课本每一个编程题。要求先画出流程算法图或N-S图,然后编程实现,有可能的话使用两种以上方法;
2.编程求“百钱百鸡”问题。(鸡翁一值钱五,鸡母 一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?)
3.编程输入一个整数,计算它是几位数字,分别输出每一位数字,并输出各个数位上数字之和。
4.在一个平面上,有四个圆柱形塔,塔底圆心坐标分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),塔半径为1,塔高为10米,塔外无建筑物。编程,输入任一个点平面坐标,求该点建筑物的高度。
5.编程计算s=1!+2!+3!+......n!(其中n为整数,n!表示计算n阶乘),要求使用两种以上的方法。

一:课本习题

5.

 
/*************************** 

****计算并输出自然对数的值 

  *************************/  

  

#include <iostream>  

using namespace std;  

  

int main()  

{  

    double b=1,c=1,f=1;  

    int a,d=1;  

    while(!(b<1e-6))  

    {  

        for(a=1;a<=d;a++)  

            c*=a;  

  

        b=1/c;  

        f+=b;  

        c=1;  

        d++;  

    }  

cout<<"e="<<f<<endl;  

    return 0;  

}

 6.
 

//功能:计算圆周率的值  

#include "stdafx.h"  

#include<iostream>  

#include<cmath>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    int a,b=0;  

    double c=0,pi;  

        for(a=1;1.0/a>=1e-8;a+=2)  

        {  

            c+=pow(-1,b)*(1.0/a);  

            b++;  

        }  

        pi=4*c;  

        cout<<"圆周率PI的值约为:"<<pi<<endl; 

    return 0;  

}  

          结果:圆周率约为:3.14159

7.

#include"stdafx.h"  

#include<iostream>  

#include<cmath>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    float a,b;  

    int c,d=0;  

    cout<<"please input a number"<<endl;  

    cin>>a;  

    for(b=10;b<a;b*=10)  

    {  

    if(a/b>1)  

        c=1;  

    d+=c;  

    }  

    switch(d)  

    {  

    case 0:cout<<a<<"is 0 to10"<<endl;break;  

    case 1:cout<<a<<"is 10 to100"<<endl;break;  

    case 2:cout<<a<<"is 100 to1000"<<endl;break;  

    default:  

        cout<<a<<"is larger than1000"<<endl;  

    }  

    return 0;  

}  
8.
 

#include "stdafx.h"  

#include<iostream>  

#include<iomanip>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    cout<<setfill(' ')  

        <<setw(7)<<"*"<<endl 

        <<setw(9)<<"* **"<<endl  

        <<setw(11)<<"* * * **"<<endl  

        <<setw(13)<<"* * * * * **"<<endl  

        <<setw(11)<<"* * * **"<<endl  

        <<setw(9)<<"* **"<<endl  

        <<setw(7)<<"*"<<endl; 

    return 0;  

}  

9.

#include "stdafx.h"  

#include<iostream>  

#include<cmath>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    int a=0,b;  

    for(b=1;b<=1000;)  

    {  

        a++;  

        b+=pow(a,2);  

          

    }  

    cout<<"满足条件的最大值为"<<a-1<<endl;  

    return 0;  

}  

       结果:13

10.
 
 

//功能:习题10 

#include "stdafx.h"  

#include<iostream>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    float a=10000,b=0.01;  

    int c;  

    for(c=2;c<=30;c++)  

    {  

        b+=2*b;  

        a+=10000;  

    }  

    cout<<"陌生人给了富翁"<<a/10000<<"万元"<<endl; 

    cout<<"富翁给了陌生人"<<b/10000<<"万元"<<endl; 

    return 0;  

}  

11.           九九乘法表
 

#include "stdafx.h"  

#include<iostream>  

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  

{  

    int a,b,c;  

    for(a=1;a<=9;a++)  

    {  

        for(b=1;b<=a;b++)  

        {  

        c=b*a;  

        cout<<b<<"x"<<a<<"="<<c<<"\t"; 

        }  

        cout<<endl;  

    }  

    return 0;  

}  

二.

# include <iostream>    

using namespace std ;    

    

int main ()    

{     

  

    int i , j , k ;    

    cout << endl ;  

    cout << "方案为:" << endl ;  

    for (i = 0 ; i < 100 ; i++)     

        {for (j = 0 ; j < 100 ; j++)     

            {for (k = 0 ; k < 100 ; k+=3)     

                {       

                    if(5*i+3*j+k/3==100 && i+j+k==100)  

                    cout << "鸡翁" << i << "只"<< "鸡母" << j << "只" << "鸡雏" << k << "只"<< endl ;    

                }       

            }    

        }               

    return 0 ;    

  

}  

三.

# include <iostream>  

using namespace std ;  

  

int main()  

{  

    int  a , b , c = 0 ,  d ,temp , s = 0 ;  

    cout << "请输入一个整数" ;      

    cin >> a ;  

    b = a ;  

    d = a ;  

    do   

    {  

        a = a / 10 ;  

        c = c++ ;  

    } while ( a > 0 ) ;  

    cout << "该数为" << c << "位数" << endl << endl ;  

    cout << "该数" << d << "每个位上的数分别为" ;  

    do  

    {  

        temp = b % 10 ;  

        b = b / 10 ;  

        s = s + temp ;  

        cout <<  temp << "\t" ;  

    } while ( b > 0) ;  

    cout << endl ;  

    cout << "该数每一位相加之和为:" << s << endl ;  

  

      

  

    return 0 ;  

}  

四.

# include <iostream>  

# include <math.h>  

using namespace std ;  

  

int main()  

{  

    float x , y , d ;  

    cout << "请输入任一个点平面横坐标x:";  

    cin >> x ;  

    cout << "请输入任一个点平面横坐标y:";  

    cin >> y ;      

    x = fabs(x) ;  

    y = fabs(y) ;  

    d = sqrt((x - 2) * (x - 2) + (y - 2) * (y - 2)) ;<span style="white-space:pre"> </span>//求(|x| , |y|) 到点(2,2)的距离  

    if (d <= 1)  

        cout << "该点坐标为:(" << x << "," << y << ")" <<"该建筑物的高为10米" << endl ;  

    else   

        cout << "该建筑物的高为0" << endl ;  

      

    return 0 ;  

}  

五.

# include <iostream>  

using namespace std ;  

  

int main ()  

{  

    int i , n , s = 1 , sum = 0  ;  

    cout << "请输入任意一个自然数n=" ;  

    cin >> n ;  

     i = 1 ;   

    do  

    {     

        s = s * i ;  

        sum = sum + s ;  

        i++ ;  

  

    } while (i <= n);  

    cout << "sum = 1!+2!+……+" << n << "!" << "=" << sum << endl ;  

    return 0 ;  

  

}  

9443
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: