1. if 语句基础
基本语法
if (condition) {
// 条件为真时执行的代码
}
示例:
int age = 20;
if (age > 18) {
cout << "允许进入" << endl;
}
if-else 语句
if (condition) {
// 条件为真时执行
} else {
// 条件为假时执行
示例:
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// 输出 "Good evening."
2. 多重条件判断
else if 语句
if (condition1) {
// condition1 为真时执行
} else if (condition2) {
// condition1 为假且 condition2 为真时执行
} else {
// 所有条件都为假时执行
}
示例:
int score = 85;
if (score >= 90) {
cout << "优秀" << endl;
} else if (score >= 80) {
cout << "良好" << endl;
} else if (score >= 60) {
cout << "及格" << endl;
} else {
cout << "不及格" << endl;
}
// 输出 "良好"
3. 课堂练习
【入门】求任意三位数各个数位上数字的和
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
cin >>n;
m=n/1%10+n/10%10+n/100%10;
cout<<m<<endl;
}
【入门】两位数运算
#include <bits/stdc++.h>
using namespace std;
int n;
double a, b;
int main()
{
cin >>n;
a = (double)(n / 10 % 10);
b = (double)(n / 1 % 10);
cout <<fixed <<setprecision(1);
cout <<a / b <<endl;
return 0;
}
【入门】算算和是多少
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
cin >>n;
m = (n / 100 % 10) * 1 +
(n / 10 % 10) * 10 +
(n / 1 % 10) * 100;
// cout <<n <<" " <<m <<endl;
// 12行为调试代码:可以判断中途变量是否有问题
cout <<n + m <<endl;
return 0;
}
【入门】倒序输出一个四位整数
#include <bits/stdc++.h>
using namespace std;
char a, b, c, d;
int n, m;
int main()
{
cin >>a >>b >>c >>d;
///输入的字符数码转换成数字需要减去'0'
cout <<(d - '0') * 1000 +
(c - '0') * 100 +
(b - '0') * 10 +
(a - '0') * 1 <<endl;
// 1000
// 0001
// cin >>n;
// m = (n / 1 % 10) * 1000 +
//
(n / 10 % 10) * 100 +
//
//
(n / 100 % 10) * 10 +
(n / 1000 % 10) * 1;
// cout <<m <<endl;
return 0;
}
【入门】谁的年龄大
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
cin >>n;
m = (n / 1 % 10) * 10 + (n / 10 % 10) * 1;
if (n > m)
{
cout <<n <<endl;
}
else
{
cout <<m <<endl;
}
return 0;
}
//Ctrl + a //全选
//Ctrl + c //复制
//Ctrl + v //粘贴
//win(Ctrl旁边) + v //打开剪切板
//Ctrl + ?/ //快速注释
//F11 //在Dev c++中是快速编译运行
【入门】最多能倒多少杯水
#include <bits/stdc++.h>
using namespace std;
int n, x; //输入单位不一样,必须换成一样再计算
int main()
{
cin >>n >>x;
n *= 1000; // n = n * 1000; L单位转换成mL
cout <<(n + (x - 1)) / x <<endl;
//计算n/x向上取整的方法
// if (n % x > 0)
// {
//
cout <<n / x + 1 <<endl;
// }
// else
// {
//
cout <<n / x <<endl;
// }
return 0;
}
【入门】扩建鱼塘问题
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
cin >>n >>m;
if (n > m)
{
cout <<n * n - n * m <<endl;
}
else
{
cout <<m * m - n * m <<endl;
}
return 0;
}
【入门】心系南方灾区
#include <bits/stdc++.h>
using namespace std;
int m, n;
int main()
{
cin >>m >>n;
cout <<(m + n - 1) / n <<endl;
return 0;
}
【入门】冷饮的价格(2)
#include <bits/stdc++.h>
using namespace std;
int n;
double ans;
int main()
{
cin >>n;
cout <<fixed <<setprecision(1);
if (n >= 30)
{
cout <<n * 1.0 <<endl;
}
else if (20 <= n && n <= 29)
{
cout <<n * 1.2 <<endl;
}
else if (10 <= n && n <= 19)
{
cout <<n * 1.5 <<endl;
}
else if (n < 10)
{
cout <<n * 1.8 <<endl;
}
return 0;
}
}
【入门】找出最经济型的包装箱型号
#include <bits/stdc++.h>
using namespace std;
int w;
int main()
{
cin >>w;
if (w < 10) cout <<"A" <<endl; //只有一句话可以不加大括号
else if (w < 20) cout <<"B" <<endl;
else if (w < 40) cout <<"C" <<endl;
else if (w < 50) { cout <<"D" <<endl; } //也可以这样加大括号
else cout <<'E' <<endl; //只输出一个字母也可以用单引号
return 0;
}
闰年判断
#include <bits/stdc++.h>
using namespace std;
int y;
int main()
{
cin >>y;
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
cout <<"yes" <<endl;
}
else
{
cout <<"no" <<endl;
}
}
return 0
没有回复内容