C++ 语法基础课 习题2 —— printf 语句与判断结构
admin
2024-01-21 04:17:31

文章目录

  • 例题
    • 1. 倍数
    • 2. 零食
    • 3. 区间
    • 4. 三角形
    • 5. 游戏区间
    • 6. 加薪
    • 7. 动物
  • 习题
    • 1. 选择练习1
    • 2. DDD
    • 3. 点的坐标
    • 4. 三角形类型
    • 5. 游戏时间2
    • 6. 税
    • 7. 简单排序
    • 8. 一元二次方程式
    • 9. 平均数3

例题

1. 倍数

Acwing 665.倍数

#include
using namespace std;int main()
{int a,b;cin>>a>>b;if(a%b==0||b%a==0) cout<<"Sao Multiplos"<

2. 零食

Acwing 660.零食

#include
#include
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
using namespace std;int main()
{double x;cin >> x;if(x>=0&&x<=25) cout<<"Intervalo "<<"[0,25]"<25&&x<=50) cout<<"Intervalo "<<"(25,50]"<50&&x<=75) cout<<"Intervalo "<<"(50,75]"<75&&x<=100) cout<<"Intervalo "<<"(75,100]"<

4. 三角形

Acwing 664.三角形

#include
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
#include
using namespace std;int main()
{int a,b;cin >>  >> b;if (a < b)printf("O JOGO DUROU %d HORA(S)",b-a);elseprintf("O JOGO DUROU %d HORA(S)",24-a+b);return 0;
}

6. 加薪

Acwing 669.加薪

  • %显示不出来,如果需要输出需要加转义字符,两个%%
#include
#include
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
#include
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";elsecout<<"minhoca";return 0;
}

习题

1. 选择练习1

Acwing 657.选择练习1

#include
#includeusing 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"<

2. DDD

Acwing 671.DDD

#include
#includeusing 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";elsecout<< "DDD nao cadastrado";return 0;
}

3. 点的坐标

Acwing 662.点的坐标

#include
#include
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
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"<if(a*a == b*b + c*c)cout<<"TRIANGULO RETANGULO"< b*b + c*c)cout<<"TRIANGULO OBTUSANGULO"<

5. 游戏时间2

Acwing 668.游戏时间2

#include
#includeusing namespace std;int main()
{int a,b,c,d,x,y;cin>> a>>b>>c>>d;if (a 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(bx = 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
#includeusing 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
#includeusing 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
#include
#include
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
#include
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."<= 5&&x < 7){cout<<"Aluno em exame."<> 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);}
}

相关内容

热门资讯

国庆黄金周景区情况:大同古城半... 文| 芙昕 编辑 | 芙昕 国庆长假,很多人都计划着出门走走,可一到了那些热门景点,看到的往往不是山...
来大东北一共分两步:先“冷藏”... 还在被“东北=冰窖”的刻板印象吓退? 南方的“小土豆”们 别急着裹紧小棉袄 这个冬天 让“气候缓冲带...
第三届“长城之约”活动在河北涞... 11月15日,第三届"长城之约"全球推广活动暨世界文化遗产对话15日在河北省保定市涞源县启幕。 本次...
巴厘岛:时光在此停驻 (自由行... 曾几何时,世人只知巴厘岛而不知印尼。巴厘岛的美太过耀眼,以至于人们常常忘记——它只是印尼万千岛屿中最...