C++ 语法基础课 习题2 —— printf 语句与判断结构

这个博客文章包含了一系列的编程题目,涵盖了从简单的倍数判断到复杂的游戏时间计算,涉及区间判断、三角形性质、加薪算法、动物识别等多个方面。通过这些练习,读者可以提升自己的编程思维和问题解决能力,涵盖基础数学、逻辑判断和实际应用等多个知识点。
摘要由CSDN通过智能技术生成

例题

1. 倍数

Acwing 665.倍数

#include<iostream>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    
    if(a%b==0||b%a==0) cout<<"Sao Multiplos"<<endl;
    else cout<<"Nao sao Multiplos"<<endl;
    
    return 0;
}

2. 零食

Acwing 660.零食

#include<iostream>
#include<cstdio>
using namespace std;

int  main()
{
    int x,y;
    double price;
    scanf("%d%d",&x,&y);
    if(x==1) price =4;
    else if(x==2) price=4.5;
    else if(x==3) price =5;
    else if(x==4) price=2;
    else  price=1.5;
    
    printf("Total: R$ %.2lf",y*price);
}

3. 区间

Acwing 659.区间

#include<iostream>
using namespace std;

int main()
{
    double x;
    cin >> x;
    
    if(x>=0&&x<=25) cout<<"Intervalo "<<"[0,25]"<<endl;
    else if(x>25&&x<=50) cout<<"Intervalo "<<"(25,50]"<<endl;
    else if(x>50&&x<=75) cout<<"Intervalo "<<"(50,75]"<<endl;
    else if (x>75&&x<=100) cout<<"Intervalo "<<"(75,100]"<<endl;
    else cout<<"Fora de intervalo"<<endl;
    return 0;
}

4. 三角形

Acwing 664.三角形

#include<iostream>
using namespace std;

int main()
{
    double a,b,c;
    cin>>a>>b>>c;
    if( a+b > c && a+c > b && b+c > a) printf("Perimetro = %.1lf",a+b+c);
    else printf("Area = %.1lf",0.5*(a+b)*c);
    
    return 0;
}

5. 游戏区间

Acwing 667.游戏时间

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    int a,b;
    cin >>  >> b;
    if (a < b)
        printf("O JOGO DUROU %d HORA(S)",b-a);
    else
        printf("O JOGO DUROU %d HORA(S)",24-a+b);
        
    return 0;
}

6. 加薪

Acwing 669.加薪

  • %显示不出来,如果需要输出需要加转义字符,两个%%
#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    double salary;
    scanf("%lf",&salary);
    
    double x;
    if(salary <= 400) x = 0.15;
    else if(salary <= 800) x = 0.12;
    else if(salary <= 1200) x = 0.1;
    else if(salary <= 2000) x = 0.07;
    else x = 0.04;
    
    printf("Novo salario: %.2lf\n",salary*(1+x));
    printf("Reajuste ganho: %.2lf\n",salary*x);
    printf("Em percentual: %.0lf %%",x*100);// %显示不出来,如果需要输出需要加转义字符,两个%%
    
    return 0;
}

7. 动物

Acwing 670.动物

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string a,b,c;
    cin>>a>>b>>c;
    
    if(a=="vertebrado"&&b=="ave"&&c=="carnivoro")
        cout<<"aguia";
    else if(a=="vertebrado"&&b=="ave"&&c=="onivoro")
        cout<<"pomba";
    else if(a=="vertebrado"&&b=="mamifero"&&c=="onivoro")
        cout<<"homem";
    else if(a=="vertebrado"&&b=="mamifero"&&c=="herbivoro")
        cout<<"vaca";
    else if(a=="invertebrado"&&b=="inseto"&&c=="hematofago")
        cout<<"pulga";
    else if(a=="invertebrado"&&b=="inseto"&&c=="herbivoro")
        cout<<"lagarta";
    else if(a=="invertebrado"&&b=="anelideo"&&c=="hematofago")
        cout<<"sanguessuga";
    else
        cout<<"minhoca";
    return 0;
}

习题

1. 选择练习1

Acwing 657.选择练习1

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    
    if(b>c&&d>a&&c+d>=a+b&&c>0&&d>0&&a%2==0)
        cout<<"Valores aceitos"<<endl;
    else cout<<"Valores nao aceitos"<<endl;
    
    return 0;
}

2. DDD

Acwing 671.DDD

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int x;
    cin>>x;
    if(x==61)
        cout<<"Brasilia";
    else if(x==71)
        cout<<"Salvador";
    else if(x==11)
        cout<<"Sao Paulo";
    else if(x==21)
        cout<<"Rio de Janeiro";
    else if(x==32)
        cout<<"Juiz de Fora";
    else if(x==19)
        cout<<"Campinas";
    else if(x==27)
        cout<<"Vitoria";
    else if(x==31)
        cout<<"Belo Horizonte";
    else
        cout<< "DDD nao cadastrado";
    return 0;
}

3. 点的坐标

Acwing 662.点的坐标

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    double x,y;//使用int类型会出错
    cin >> x >> y;
    if(x>0&&y>0)
        cout<<"Q1";
    else if (x<0&&y>0)
        cout<<"Q2";
    else if(x<0&&y<0)
        cout<<"Q3";
    else if(x>0&&y<0)
        cout<<"Q4";
    else 
    {
        if(x==0&&y==0)
            cout<<"Origem";
        else if(!x)
            cout<<"Eixo Y";
        else if(!y)
            cout<<"Eixo X";
    }
    return 0;
}

4. 三角形类型

Acwing 666.三角形类型

#include<iostream>
using namespace std;

int main()
{
    double a,b,c;
    cin >> a >> b >>c;
    //把A设置为最大值
    if (a < b) 
    {
        double t=a;
        a=b;
        b=t;
    }
    if (a < c) 
    {
        double t=a;
        a=c;
        c=t;
    }
    if (b < c) 
    {
        double t=b;
        b=c;
        c=t;
    }
    
    if(a>=b+c) cout<<"NAO FORMA TRIANGULO"<<endl;
    else 
    {
        if(a*a == b*b + c*c)
            cout<<"TRIANGULO RETANGULO"<<endl;
        if(a*a > b*b + c*c)
            cout<<"TRIANGULO OBTUSANGULO"<<endl;
        if(a*a < b*b + c*c)
            cout<<"TRIANGULO ACUTANGULO"<<endl;
        if(a == b &&b == c)
            cout<<"TRIANGULO EQUILATERO"<<endl;
        if((a == b && a != c && b != c) || (a == c && a != b && c != b)|| (b == c && b != a && c != a))
            cout<<"TRIANGULO ISOSCELES"<<endl;
    }
    return 0;
}

5. 游戏时间2

Acwing 668.游戏时间2

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int a,b,c,d,x,y;
    cin>> a>>b>>c>>d;
    if (a<c)
    { 
        x = (c*60+d-a*60-b)/60;
        y = (c*60+d-a*60-b)%60;
    }
    else if(a==c)
    {
        if(b>d)
        {
        x = ((24+c)*60+d-a*60-b)/60;
        y = ((24+c)*60+d-a*60-b)%60;
        }
        else if(b==d)
        {
        x = 24;
        y = 0;
        }
        else if(b<d)
        {
        x = 0;
        y = d-b;
        }
    }
    else 
    {
        x = ((24+c)*60+d-a*60-b)/60;
        y = ((24+c)*60+d-a*60-b)%60;
    }
    
    printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",x,y);
    
    return 0;
}

6. 税

Acwing 672.税

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    double salary,x,tax;
    cin >> salary;
    if(salary>=0.00&&salary<=2000.00)
    {
        printf("Isento");
    }
    else if(salary>=2000.01&&salary<=3000.00)
    {
        x = (salary-2000)*0.08;
        printf("R$ %.2lf",x);
    }
    else if(salary>=3000.01&&salary<=4500.00)
    {
        x = 1000*0.08+(salary-3000)*0.18;
        printf("R$ %.2lf",x);
    }
    else if(salary>4500.00)
    {
        x = 1000*0.08+1500*0.18+(salary-4500)*0.28;
        printf("R$ %.2lf",x);
    }
    
    return 0;
}

7. 简单排序

Acwing 663.简单排序

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    int x = a,y = b,z = c;
    
    if(a > b)
    {
        int t = a;
        a = b;
        b = t;
    }
    if(c < a)
    {
        int t = a;
        a = c;
        c = t;
    }
    if(c < b)
    {
        int t = b;
        b = c;
        c = t;
    } 
    cout << a << endl << b << endl << c << endl << endl;
    cout << x << endl << y << endl << z << endl;
    
    return 0;
}

8. 一元二次方程式

Acwing 658.一元二次方程式

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    double a,b,c,x1,x2;
    cin >> a>>b>>c;
    if((b*b-4*a*c)<0||a == 0)
    {
        printf("Impossivel calcular");
    }
    else 
    {
        double m = sqrt(b*b-4*a*c);
        x1=(-b+m)/(2*a);
        x2=(-b-m)/(2*a);
        printf("R1 = %.5lf\n",x1);
        printf("R2 = %.5lf\n",x2);
    }
}

9. 平均数3

Acwing 661.平均数3

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    double n1,n2,n3,n4;
    scanf("%lf%lf%lf%lf",&n1,&n2,&n3,&n4);
    
    double x = (2*n1+3*n2+4*n3+n4)/10;
    
    printf("Media: %.1lf",x);
    if(x >= 7) cout<<"Aluno aprovado."<<endl;
    else if(x < 5.0)cout<<"Aluno reprovado."<<endl;
    else if(x >= 5&&x < 7)
    {
        cout<<"Aluno em exame."<<endl;
        double y,z;
        cin >> y;
        printf("Nota do exame: %.1lf",y);
        z = (x+y)/2;
        if (z>=5)printf("Aluno aprovado.\n");
        else printf("Aluno reprovado.\n");
        printf("Media final: %.1lf\n",z);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NqqGOGO

你的鼓励是我学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值