1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int fenshu;
6 cin >> fenshu;
7 if (fenshu >= 86) {
8 cout << "VERY GOOD" << endl;
9 } else {
10 if (fenshu >= 60) {
11 cout << "GOOD" << endl;
12 } else {
13 cout << "BAD" << endl;
14 }
15 }
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int fenshu;
6 cin >> fenshu;
7 if (fenshu >= 90) {
8 cout << fenshu * 3 << endl;
9 } else if (fenshu >= 80) {
10 cout << fenshu * 2 << endl;
11 } else if (fenshu >= 70) {
12 cout << fenshu << endl;
13 } else {
14 cout << 50 << endl;
15 }
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 double x;
6 cin >> x;
7 double y = 0;
8 if (x < 5) {
9 y = -x + 2.5;
10 } else if (x < 10) {
11 y = 2 - 1.5 * (x - 3) * (x - 3);
12 } else {
13 y = x / 2 - 1.5;
14 }
15 printf("%.3f", y);
16 return 0;
17}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int zhongliang;
6 char flag;
7 cin >> zhongliang >> flag;
8 int youfei = 8;
9 if (zhongliang > 1000) {
10 zhongliang -= 1000;
11 youfei += zhongliang / 500 * 4;
12 if (0 != zhongliang % 500) {
13 youfei += 4;
14 }
15 }
16 if ('y' == flag) {
17 youfei += 5;
18 }
19 cout << youfei << endl;
20 return 0;
21}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int year;
6 cin >> year;
7 if (0 == year % 400) {
8 cout << "Y" << endl;
9 } else {
10 if (0 == year % 100) {
11 cout << "N" << endl;
12 } else {
13 if (0 == year % 4) {
14 cout << "Y" << endl;
15 } else {
16 cout << "N" << endl;
17 }
18 }
19 }
20 return 0;
21}
22#include <bits/stdc++.h>
23using namespace std;
24
25int main() {
26 int year;
27 cin >> year;
28 if (0 == year % 400 || 0 != year % 100 && 0 == year % 4) {
29 cout << "Y" << endl;
30 } else {
31 cout << "N" << endl;
32 }
33 return 0;
34}嵌套if-else结构允许在一个if或else代码块内部包含另一个if-else结构,这对于需要根据多级条件进行决策的情况非常有用。
1if (布尔表达式1) {
2 if (布尔表达式2) {
3 操作1
4 } else {
5 操作2
6 }
7} else {
8 if (布尔表达式3) {
9 操作3
10 } else {
11 操作4
12 }
13}1int score1 = 85;
2int score2 = 90;
3int score3 = 0;
4
5if (score1 >= 80) {
6 if (score2 >= 00) {
7 cout << "competition opportunities" << endl;
8 } else {
9 cout << "daily study" << endl;
10 }
11else {
12 if (score1 < 60) {
13 if (score3 < 60) {
14 cout << "extra homework" << endl;
15 } else {
16 cout << "daily study" << endl;
17 }
18 } else {
19 cout << "daily study" << endl;
20 }
21}1int score1 = 85;
2int score2 = 90;
3int score3 = 0;
4
5if (score1 >= 80)
6 if (score2 >= 00)
7 cout << "competition opportunities" << endl;
8 else
9 cout << "daily study" << endl;
10else
11 if (score1 < 60)
12 if (score3 < 60)
13 cout << "extra homework" << endl;
14 else
15 cout << "daily study" << endl;
16 else
17 cout << "daily study" << endl;
18}if-else链允许你根据多个条件连续检查并执行相应的代码块。它是通过将多个if和else结构连在一起形成的。
1if (布尔表达式1) {
2 操作1
3} else if (布尔表达式2){
4 操作2
5} else if (布尔表达式3){
6 操作3
7} else if (布尔表达式4){
8 操作4
9} else if (布尔表达式5){
10 操作5
11} else {
12 操作6
13}1int score = 85;
2if (score >= 90) {
3 cout << "Excellent" << endl;
4} else if (score >= 80) {
5 cout << "Good" << endl;
6} else if (score >= 70) {
7 cout << "generally" << endl;
8} else if (score >= 60) {
9 cout << "qualified" << endl;
10} else {
11 cout << "failed" << endl;
12}