Chapter 3 C++程序设计初步—习题(下篇)

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

3.9习题补充

1.设圆的半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球的表面积、圆球的体积、圆柱体积,用cin输入数据,输出计算结果,输出时要有文字说明,取小数点后两位

//
//  main.cpp
//  Chapter 3_1
//设圆的半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球的表面积、圆球的体积、圆柱体积,用cin输入数据,输出计算结果,输出时要有文字说明,取小数点后两位
//  Created on 14.12.23.
//

#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, const char * argv[])
{
    float r=0,h=0;
    cout<<"请输入半径:"<<endl;
    cin>>r;
    cout<<"请输入圆柱的高:"<<endl;
    cin>>h;
    float c1=0;//圆周长
    float s1=0;//圆面积
    float s2=0;//圆球的表面积
    float v1=0;//圆球体积
    float v2=0;//圆柱体积
    c1=2*(3.14)*r;
    s1=3.14*r*r;
    s2=4*3.14*r*r;
    v1=4/3*3.14*r*r*r;
    v2=h*s1;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"圆周长是:"<<c1<<endl;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"圆面积吗是:"<<s1<<endl;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"圆的表面积是:"<<s2<<endl;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"圆球体积是:"<<v1<<endl;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"圆柱体积是:"<<v1<<endl;
    return 0;
}

结果:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

2.输入一个华氏温度,要求输出摄氏温度,公式次c=5/9(F-32),输出要有文字说明,取两位小数 

//
//  main.cpp
//  Chapter 3_3
//输入一个华氏温度,要求输出摄氏温度,公式次c=5/9(F-32),输出要有文字说明,取两位小数
//  Created  on 14.12.23.
//

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    float F=0;//华氏摄氏度
    cout<<"华氏摄氏度是:"<<endl;
    cin>>F;
    float c=0;//摄氏度
    c=5*(F-32)/9;
    cout<<"摄氏度是:"<<setiosflags(ios::fixed)<<setprecision(2)<<c<<endl;
    return 0;
}

答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

3.用getchar函数读入两个字符给c1,c2,然后分别用putchar函数和cout语句输出这两个字符 

//
//  main.cpp
//  Chapter 3_3
//用getchar函数读入两个字符给c1,c2,然后分别用putchar函数和cout语句输出这两个字符
//  Created on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    
    char  c1,c2;
    c1=getchar();
    c2=getchar();
    putchar(c1);
    cout<<" "<<c2<<endl;
    return 0;
}

答案:输入的两个字符中不能有空格,否则c2输出是空格

有空格的情况

没有空格的情况

若把c1,c2变为int型,答案是多少呢 

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

4.有3个整数,a、b、c,由键盘输入,输出其最大值 

//
//  main.cpp
//  Chapter 3_4
//有3个整数,a、b、c,由键盘输入,输出其最大值
//  Created  on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int a=0,b=0,c=0,i=0;
    cout<<"请输入三个要比较的数:"<<endl;
    cin>>a>>b>>c;
    if(a>b)
    {
        if(a>c)
            cout<<"最大数是:"<<a<<endl;
        else  cout<<"最大数是:"<<c<<endl;
    }
    else if(a<b)
    {
        if(b>c)
        {
            cout<<"最大数是:"<<b<<endl;
        }
        else cout<<"最大数是:"<<c<<endl;
    }
    return 0;
}

答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

5.给一个百分制成绩,要求输出'A' 'B' 'C' 'D' 'E'  ,90分以上为'A',80~90分为'B',70~79分为'C',60~69分为'D',60分以为下'E'

//
//  main.cpp
//  Chapter 3_5
//给一个百分制成绩,要求输出'A' 'B' 'C' 'D' 'E'  ,90分以上为'A',80~90分为'B',70~79分为'C',60~69分为'D',60分以为下'E'
//  Created on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int grade=0;
    cout<<"请输入成绩:"<<endl;
    cin>>grade;
    if(grade<+100 && grade>90)
        cout<<'A'<<endl;
    else if (grade<=90 && grade>=80)
        cout<<'B'<<endl;
    else if(grade<=79 && grade>=70)
        cout<<'C'<<endl;
    else if (grade<=69 && grade>=60)
        cout<<'D'<<endl;
    else if(grade>=0 && grade<=60)
        cout<<'E'<<endl;
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

6.给一个不多余5位的正整数,要求:a:求出它是几位数,b:分别打印出每一位数字,c:按逆序打印出各位数字,例如原是321,应输出123

//
//  main.cpp
//  Chapter 3_6
//给一个不多余5位的正整数,要求:a:求出它是几位数,b:分别打印出每一位数字,c:按逆序打印出各位数字,例如原是321,应输出123
//  Created on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int a=0;
    int n=0,i=0;//位数,每一位数,
    int t=0,h=0,m=0,j=0,k=0;
    int d=0;
    cout<<"请输入此数:"<<endl;
    cin>>a;
    t=a/10000;//个位
    h=a/1000%10;//十位
    m=a/100%10;
    j=a/10%10;
    k=a%10;
    cout<<"从右到左每一位是:";
    while(a!=0 && a>0)
    {
        i=a%10;
        a=a/10;
        cout<<" ";
        cout<<i;
        n++;
    }
    cout<<endl;
    cout<<"位数是:"<<n<<endl;
    if(n==5)
    {
        d=t+h*10+m*100+j*1000+k*10000;
        cout<<"这个新的数是:"<<d<<endl;
    }
    else if(n==4)
    {
        d=h+m*10+j*100+k*1000;
        cout<<"这个新的数是:"<<d<<endl;
    }
    else if(n==3)
    {
        d=m+j*10+k*100;
        cout<<"这个新的数是:"<<d<<endl;
    }
    else if(n==2)
    {
        d=j+k*10;
        cout<<"这个新的数是:"<<d<<endl;
    }
    else if(n==1)
    {
        d=k;
        cout<<"这个新的数是:"<<d<<endl;
    }
    else cout<<"0"<<endl;
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

7.输入4个整数,要求由小到大的顺序排出

//
//  main.cpp
//  Chapter 3_7
//输入4个整数,要求由小到大的顺序排出
//  Created  on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int i=0,j;
    int a[4];//此处用了数组
    int n=0;
    for(i=0;i<4;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<3;i++)
    {
        for(j=1+i;j<4;j++)//j=i+1,当i=2时,就不用重复与前面那个进行比较了
        {
            if(a[i]>a[j])
            {
                n=a[i];
                a[i]=a[j];
                a[j]=n;
            }
        }
    }
    cout<<"正确顺序是:";
    for(i=0;i<4;i++)
    {
        
        cout<<a[i]<<"  ";
    }
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

8.输入两个正整数m和n,求其最大的公约数和最小公倍数

//
//  main.cpp
//  Chapter 3_8
//输入两个正整数m和n,求其最大的公约数和最小公倍数
//  Created  on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int m=0,n=0,i=0;
    int x=0,y=0;
    int max=0,min=0;
    cout<<"输入要求的两个数:";
    cin>>m>>n;
    x=m;
    y=n;
    while(n!=0)//辗转相除法
    {
        i=m%n;
        m=n;
        n=i;
        max=m;
    }
    min=x*y/max;
    cout<<"最大公约数是:"<<max<<endl;
    cout<<"最小公倍数是:"<<min<<endl;
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

9.输入一行字符,分别统计出英文字母,空格,数字,和其他字符的个数

//
//  main.cpp
//  Chapter 3_9
//输入一行字符,分别统计出英文字母,空格,数字,和其他字符的个数
//  Created  on 14.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    
    char  c;
    int x1=0,x2=0,x3=0,x4=0;
   while((c=getchar())!='\n')
   {
       if((c>=65 && c<=90) || (c>=97 && c<=122))
           x1++;
       else if(c==32)
           x2++;
       else if(c>=48 && c<=57)
           x3++;
       else
           x4++;
       
   }
    cout<<"英文字母有:"<<" "<<x1<<"个"<<endl;
    cout<<"空格有:"<<" "<<x2<<"个"<<endl;
    cout<<"数字有:"<<" "<<x3<<"个"<<endl;
    cout<<"其他字符有:"<<" "<<x4<<"个"<<endl;
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

10.求 Sn=a+aa+aaa+···+aa···a(n个a),其中,a是一个数字,例如:2+22+222+2222+22222(此时n=5),n由键盘输入

//
//  main.cpp
//  Chapter 3_10
//求 Sn=a+aa+aaa+···+aa···a(n个a),其中,a是一个数字,例如:2+22+222+2222+22222(此时n=5),n由键盘输入
//  Created on 18.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int n=0,Sum=0,m=0;
    cout<<"需要计算的位数是:";
    cin>>n;
    int a=2;
    int i=1;
    for(;i<=n;i++)
    {
        m=m+a;         //最初的加数从1个个位a开始。
        Sum=Sum+m; //累加的基本表达式。
        if(i<n)
        {
            cout<<m<<"+";
        }
        else cout<<m;
        m=10*m;         //下一个加数是原来的10倍(进入下次计算前个位再加1个a)。
       
    }
    cout<<"="<<Sum<<endl;
    return 0;
}

答案: 

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗 

11.求1到20的阶层

//
//  main.cpp
//  Chapter 3_11
//求1到20的阶层
//  Created  on 18.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int n=1;
    int Sum=0;
    for(n=1;n<21;n++)
    {
        n=1*n;
        if(n<20)
        {
            cout<<n<<"!"<<"+";
        }
        else cout<<n<<"!";
        Sum=n+Sum;
    }
    cout<<"="<<Sum<<endl;
    return 0;
}

答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗 

12.输出所有的“水仙花数”,所谓的“水仙花数”就是指一个3位数,其各位数立方和等于数本身,例如:153是一个水仙花数,因为153=1^3+5^3+3^3. 

//
//  main.cpp
//  Chapter 3_12
//输出所有的“水仙花数”,所谓的“水仙花数”就是指一个3位数,其各位数立方和等于数本身,例如:153是一个水仙花数,因为153=1^3+5^3+3^3.
//  Created on 18.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int n=100,i=0;
    int hundred=0;//百位
    int tens=0;//十位
    int ones=0;//个位
    for(n=100;n<1000;n++)
    {
        hundred=n/100;
        tens=n%100/10;
        ones=n%10;
        
        if(n == hundred*hundred*hundred+tens*tens*tens+ones*ones*ones)
        {
            cout<<n<<" ";
            i++;
            if(i==5)
            {
                cout<<'\n';
            }
        }
        else continue;
    }

    return 0;
}

 答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗 

13.一个数如果恰好等于他的因子之和,这个数就称为“完数”,例如,6的因子为1,2,3,而6=1+2+3,因此6是“完数”,编程找出1000内的所有数,并且按以下格式输出:6,its factors are 1,2,3

//
//  main.cpp
//  Chapter 3_13
//一个数如果恰好等于他的因子之和,这个数就称为“完数”,例如,6的因子为1,2,3,而6=1+2+3,因此6是“完数”,编程找出1000内的所有数,并且按以下格式输出:6,its factors are 1,2,3
//  Created on 19.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int n=2;
    for(;n<1001;n++)
    {
        int sum=1;
        for(int i=2;i<=n/2;i++)
            if(n%i==0)
            {
                sum =sum+i;
                if(sum==n)
                {
                    cout<<n;
                    cout<<",its factors are"<<" ";
                    for(i=2;i<n/2;i++)
                    {
                        if(n%i==0)
                        cout<<","<<i;
                       
                    }
                    cout<<'\n';
                }
               
            }
    }
   
    return 0;
}

答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗 

14.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13,·····求出这个数列的前20项之和 

//
//  main.cpp
//  Chapter 3_15
//有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13,·····求出这个数列的前20项之和
//  Created  on 19.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int n=1;
    int sum=0;
    for(;n<21;n++)
    {
        sum=sum+(n+1)/n;
        cout<<n+1<<"/"<<n<<" +"<<" ";
        if(n==20)
            cout<<n+1<<"/"<<n<<"=";
    }
    cout<<sum<<endl;
    return 0;
}

答案: 

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗  

15.猴子吃桃问题,猴子第1 天摘下若干桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将是剩下的吃掉一半,又多吃了一个,以后每天早上都吃了前一天的剩下的一半另加一个,到第十天早上想再吃,就只剩下一个桃子了,求第一天共摘了多少个桃子。

//
//  main.cpp
//  Chapter 3_15
//猴子吃桃问题,猴子第1天摘下若干桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将是剩下的吃掉一半,又多吃了一个,以后每天早上都吃了前一天的剩下的一半另加一个,到第十天早上想再吃,就只剩下一个桃子了,求第一天共摘了多少个桃子。
//  Created  on 19.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int i=1,m=0;
    for(int n=9;n>0;n--)
    {
        m=(i+1)*2;
        i=m;
    }
    cout<<"第一天有桃子:"<<m<<"个";
    return 0;
}

答案:

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗

16.用迭代法求x=a^ 1/2,求平方根的迭代公式为Xn+1=1/2(Xn+a/Xn),要求前后两次求出的X的差值的绝对值小于10^ (-5)

//
//  main.cpp
//  Chapter 3_16
//用迭代法求x=a^ 1/2,求平方根的迭代公式为Xn+1=1/2(Xn+a/Xn),要求前后两次求出的X的差值的绝对值小于10^ (-5)
//  Created  on 19.12.23.
//

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    float a;
    cout<<"请输入a的值:";
    cout<<endl;
    cin>>a;
    float x=sqrt(a);
    float x1=0;
    x1=(x+a/x)/2;
    if(1/(x-x1)<(10*10*10*10*10) || 1/(x-x1)>-(10*10*10*10*10))//把10^ (-5)与两次求出的X的差值做了一个位置调换
    {
        cout<<x;
    }
    else cout<<"error"<<endl;
    return 0;
}

答案

💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗 

17.输出以下图案

*

* * * *

* * * * * *

* * * * * * * *

* * * * * *

* * * *

*

//
//  main.cpp
//  Chapter 3_17
//输出以下图案
//*
//* * * *
//* * * * * *
//* * * * * * * *
//* * * * * *
//* * * *
//*
//  Created  on 19.12.23.
//

#include <iostream>
using namespace std;
int main()
{
    int i;
    for(i=1;i<5;i++)
    {
        int j=1;
        if (i==5)
            break;
        for(;j<=i*4;j++)
        {
            if(i==1)
            {
                cout<<"*"<<endl;
                break;
            }
            else if(j%2==0 && j!=i*4)
            {
                cout<<" ";
            }
            else if(j%2!=0)
                cout<<"*";
            else if(j==i*4)
            {
                cout<<endl;
                break;
            }
        }
    }
        for(int m=3;m>=0;m--)
        {
            int n=1;
            if(m==0)
                break;
            for(;n<=n*4;n++)
            {
                if(n%2==0 && n!=m*4)
                {
                    cout<<" ";
                }
                else if(m==1 & n==1)
                        {
                            cout<<"*"<<endl;
                            return 0;
                        }
                else if(n%2!=0)
                {
                    cout<<"*";
                }
                
                else if(n==m*4)
                {
                    cout<<endl;
                    break;
                }
                
            }
        }
        
        
        return 0;
    }

答案: 

共同学习,欢迎指正! 

  • 40
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值