主要内容:if判断语句综合练习
1.零食总价计算:
#include <iostream>
#include <iomanip>
using namespace std;
signed main()
{
int x,y;
cin>>x>>y;
if(x==1){
cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<"Total:"<<y*4.00<<endl;
}
else if(x==2){
cout<<"Total:"<<y*4.50<<endl;
}
else if(x==3){
cout<<"Total:"<<y*5.00<<endl;
}
else if(x==4){
cout<<"Total:"<<y*2.00<<endl;
}
else if(x==5){
cout<<"Total:"<<y*1.50<<endl;
}
return 0;
}
2.浮点数坐标判断
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
if(x>0){
if(y>0){
cout<<"Q1"<<endl;
}
else if(y<0){
cout<<"Q4"<<endl;
}
else if(y==0){
cout<<"Eixo X"<<endl;
}
}
else if(x<0){
if(y>0){
cout<<"Q2"<<endl;
}
else if(y<0){
cout<<"Q3"<<endl;
}
else if(y==0){
cout<<"Eixo X"<<endl;
}
}
else if(x==0){
if(y>0){
cout<<"Eixo Y"<<endl;
}
else if(y<0){
cout<<"Eixo Y"<<endl;
}
else if(y==0){
cout<<"Origem"<<endl;
}
}
return 0;
}
3.游戏时间计算(1)--只读小时
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if(a>12&&a<23){
if(b>12&&b<23){
cout<<"O JOGO DOROU "<<b-a<<" HORA(S)"<<endl;
}
else if(b>0&&b<12){
cout<<"O JOGO DOROU "<<24-a+b<<" HORA(S)"<<endl;
}
}
else if(a>0&&a<12){
if(b>12&&b<23){
cout<<"O JOGO DOROU "<<b-a<<" HORA(S)"<<endl;
}
else if(b>0&&b<12){
cout<<"O JOGO DOROU "<<b-a<<" HORA(S)"<<endl;
}
}
else if(a==b){
cout<<"O JOGO DOROU "<<24<<" HORA(S)"<<endl;
}
}
4.游戏时间计算(2)--小时分钟
#include <iostream>
using namespace std;
int main()
{
int A,B,C,D;
cin>>A>>B>>C>>D;
if(A==C){
if(D>=B){
cout<<"O JOGO DUROU "<<24<<" HORA(S) E "<<0<<" MINUTO(S)"<<endl;
}
else if(B<D){
cout<<"O JOGO DUROU "<<23<<" HORA(S) E "<<D-B<<" MINUTO(S)"<<endl;
}
}
else if(A<C){
if(B<=D){
cout<<"O JOGO DUROU "<<C-A<<" HORA(S) E "<<D-B<<" MINUTO(S)"<<endl;
}
else if(B>D){
cout<<"O JOGO DUROU "<<C-A-1<<" HORA(S) E "<<D+60-B<<" MINUTO(S)"<<endl;
}
}
else if(A>C){
if(B<=D){
cout<<"O JOGO DUROU "<<C+24-A<<" HORA(S) E "<<D-B<<" MINUTO(S)"<<endl;
}
else if(B>D){
cout<<"O JOGO DUROU "<<C+23-A<<" HORA(S) E "<<D+60-B<<" MINUTO(S)"<<endl;
}
}
return 0;
}
5.一元二次方程的计算
#include <bits/stdc++.h>
using namespace std;
int main()
{
double a,b,c,d;
cin>>a>>b>>c;
d=b*b-4*a*c;
cout<<setiosflags(ios::fixed)<<setprecision(5);
if(a==0||d<0){
cout<<"Impossivel calcular"<<endl;
}
else if(d>0){
cout<<"R1="<<(-b+sqrt(d))/(2*a)<<endl;
cout<<"R2="<<(-b-sqrt(d))/(2*a)<<endl;
}
return 0;
}
在此学习了一种新的小数保留方法:cout<<setiosflags(ios::fixed)<<setprecision( );
括号内既小数保留位数。