#include<iostream>
#include<cmath>
using namespace std;
int main() {
int cmark, mmark;
cin >> cmark >> mmark; //读入语文成绩和数学成绩
if (cmark >= 75 && mmark >= 85) {
cout << "有资格" << endl; //依据语文和数学成绩是否同时满足条件输出有无参选资格
} else {
cout << "无资格" << endl;
}
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int cmark, mmark;
cin >> cmark >> mmark; //读入语文成绩和数学成绩
if (cmark >= 75) { //先判断语文成绩是否达到条件
if (mmark >= 85) { //在语文成绩满足条件的前提下再去判断数学成绩是否满足条件
cout << "有资格" << endl;//语文和数学成绩都满足条件,输出“有资格”
}
}
else {
cout << "无资格" << endl;//判断语文成绩不满足条件之后可直接输出“无资格”,不需要再判断数学成绩是否满足条件
}
return 0;
}
| 逻辑与 | 逻辑或 | 逻辑非 |
|---|---|---|
| && | || | ! |
真值表如下,经过“逻辑非”运算,其结果刚好与原来相反。
| A | !A |
|---|---|
| 0 | 1 |
| 1 | 0 |
真值表如下,若参加运算的某个条件不成立,其结果为不成立,只有参见运算的两个条件都成立,其结果才成立。
| A | B | A&&B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
真值表如下,若参加运算的某个条件成立,其结果就成立,只有参加运算的条件都不成立,其结果才不成立。
| A | B | A||B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| 问题 | 条件表达式 |
|---|---|
| 判断一个数n是否同时能被2和3整除 | if(n%2==0&&n%3==0)或者if(n%6==0) |
| 判断一个数x是否在区间[1,5]之间 | if(x>=1&&x<=5)或者if(!(x<1||x>5)) |
| 判断一个数x是否等于0 | if(x==0)或者if(!x)或者if(x!=0) |
#include<iostream>
using namespace std;
int main() {
int year;
cin>>year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout<<year<<"是闰年"<<endl;
else
cout<<year<<"不是闰年"<<endl;
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a,b,c;
float p;
float s;
cin>>a>>b>>c;
if(a+b>c&&a+c>b&&b+c>a){
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"三角形的面积为:"<<s<<endl;
}else{
cout<<"不能构成三角形"<<endl;
}
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a,b,c;
float p;
float s;
cin>>a>>b>>c;
if(a+b<=c||a+c<=b||b+c<=a){
cout<<"不能构成三角形"<<endl;
}else{
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"三角形的面积为:"<<s<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
bool found,flag=false;
found=true;
cout<<flag<<" "<<found<<endl;
flag=5;
found=0;
cout<<flag<<" "<<endl;
return 0;
}
| 条件表达式 | 作用 |
|---|---|
| int maxn=(a>b)?a:b; | 将a,b两个变量的较大值赋予整型变量maxn |
| cout<<((num%2=0)?"even":"odd")<<endl; | num是偶数时输出even,是奇数时输出odd |
#include<iostream>
using namespace std;
int main() {
char ch;
cin >> ch;
ch = (ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch;
cout << ch << endl;
return 0;
}
| 重量(kg) | 收费标准(元/kg) |
|---|---|
| weight<=10 | 0.80 |
| 10<weight<=20 | 0.75 |
| 20<weight<=300 | 0.70 |