当前位置: 首页 > article >正文

蓝桥备赛(三)- 条件判断与循环(上)

一 、 if-else 语句

1.1 if语句

if语句的语法形式如下 :

if (表达式)

      语句;

1 . 表达式成立(为真) , 则语句执行 , 表达式不成立(为假) ,则语句不执行。

2 . 在C++中 , 0为假 , 非0为真

 题目 : 输入一个正整数 , 判断n是否为奇数

判断 %2 后的值是否为1 ?

为1 --> 奇数

#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	if(n%2 == 1)
		cout << "奇数" << endl; 
	return 0;
}

1.2 else 语句

⼀个正整数不是奇数,那就是偶数了,把上面的题目换一下:
题目:输入一个正整数 n ,如果 n 是奇数就打印 奇数 ,是偶数就打印 偶数 。 这里就需if...else... 语句了,语法形式如下:
#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	if(n%2 == 1)
		cout << "奇数" << endl; 
	else
		cout << "偶数" << endl;
	return 0;
}

注意:if 和 else 后面是没有分号的!!!

练习

输入一个数表示年龄,年龄 >= 18 岁就输出: 成年 ,否则就输出: 未成年
#include <iostream>
using namespace std;

int main()
{
	int age = 0;
	cin >> age;
	if(age >= 18)
		cout << "成年" << endl;
	else
	 	cout << "未成年" << endl;
	return 0;
}

 如果 age>=18 的时候,还需要打印: 可以谈恋爱了 , age < 18 时,还需提示 "不能早

" ,代码该怎么写? 这时候 if 和 else 控制的就是2条语句了,就得带上 {} 了
------>if  / else 后面默认只能控制一条语句 , 如果需要写 多条语句的时候 , 需要使用 { }
#include <iostream>
using namespace std;

int main()
{
	int age = 0;
	cin >> age;
	if(age >= 18)
	{
		cout << "成年" << endl;
		cout << "可以谈恋爱了" << endl; 		
	}

	else
	{
		cout << "未成年" << endl;
		cout << "不可以谈恋爱了" << endl; 
	}
	return 0;
}

 

1.3 嵌套if

if else 语句中, else 可以与另一个 if 语句连用,构成多重判断。

练习:判断数正负

B2035 判断数正负 - 洛谷

#include <iostream>
using namespace std;

int main()
{
	int N = 0;
	cin >> N;
	if(N > 0)
		cout << "positive" << endl;
	else if(N == 0)
		cout << "zero" << endl;
	else
		cout << "negative" << endl;
	return 0;
}

1.4 悬空else

先看一下一下的代码,思考输出的结果:

#include <iostream>
using namespace std;

int main()
{
	int a = 0;
	int b = 2;
	if(a == 1)
		if(b == 2)
			cout << "hehe" << endl;
	else
		cout << "haha" << endl;
	return 0;
}

----> a == 0 , 不为1 , 那就执行else字句 , 打印haha --> 真的吗?

这就是悬空else 的问题 : 如果有多个 if 和else , 记住一条规则 , else 总是跟最接近的else 匹配。 

或者如果我们希望else 和第一个if 匹配 , 可以这样修改代码 :

#include <iostream>
using namespace std;

int main()
{
	int a = 0;
	int b = 2;
	if(a == 1)
	{
		if(b == 2)
			cout << "hehe" << endl;
	}
	else
	{
		cout << "haha" << endl;	
	}
	return 0;
}

只要带上大括号 , 代码的逻辑就会更加清晰 , 所以在以后写代码的时候 , 需要注意括号的使用 , 让代码的可读性更高。 

1.5 练习

练习一:整除判断

登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
using namespace std;

int main()
{
	int m,n;
	cin >> m >> n;
	if(m%n == 0)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;
}

注意 :

1 . 判断相等是 == , = 是赋值操作符

2 . 注意输出的格式(大小写)

练习二:比较大小

B2039 整数大小比较 - 洛谷

#include <iostream>
using namespace std;

int main()
{
	long long int x,y;
	cin >> x >> y;
	if(x > y)
		cout << ">" << endl;
	else if(x == y)
		cout << "=" << endl;
	else
		cout << "<" << endl;
	return 0;
}

练习三:输出绝对值

B2036 输出绝对值 - 洛谷

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	float n;
	cin >> n;
	if(n < 0)
		printf("%.2f\n",-n);
	else
		printf("%.2f\n",n);
	return 0;
}

还有另一种解法:

1 . 有一个库函数 fabs 是用来求一个浮点数的绝对值的 , 但需要包含头文件 <cmath>

2 . 求整数绝对值 abs , 需要包含头文件<cstdlib>

#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	float n;
	scanf("%f",&n);
	n = fabs(n);
	printf("%.2f\n",n);
	return 0;
}

 

练习四:奇偶数判断

B2037 奇偶数判断 - 洛谷

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	if(n % 2)
		cout << "odd" << endl;
	 else
	 	cout << "even" << endl;
	return 0;
}

注意 : 这种写法可能存在问题 , 因为 n 的取值范围是 : -100 < n < 100 , 所以 n %2 可能为 -1 

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	if(n % 2 == 1)
		cout << "odd" << endl;
	else if(n % 2 == -1)
		cout << "odd" << endl;
	 else
	 	cout << "even" << endl;
	return 0;
}

这样写也是可以的 : 

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	if(n % 2 == 0)
		cout << "even" << endl;
	 else
	 	cout << "odd" << endl;
	return 0;
}

 

二 、关系操作符

2.1 关系操作符介绍

用于比较的表达式,称为 “关系表达式”(relational expression),里面使用的运算符就称为“关系运算符”(relational operator),主要有下面6个。

 a == b

a != b

a < b

a <= b

a > b

a >= b

1 .关系表达式通常返回  0 或 1 , 表示真假 , 比如 21 > 12 返回 1 , 12 > 20 返回 0

2 . 关系表达式常用于 if 或 while 结构

3 . == 是相等运算符 , = 是赋值运算符

 

 

2.2 关系操作符连用

需要避免 --> 多个关系运算符不宜连用 --> i < j < k

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int i = 3;
	int j = 7;
	int k = 5;
	if(i < j < k)
	 	cout << "hehe" << endl;
	else
		cout << "NO!" << endl; 
	return 0;
}

1 .  上面示例中,连续使用两个小于运算符。这是合法表达式,不会报错,但是通常达不到想要的结果, 即不是保证变量 j 的值在 i k 之间。因为关系运算符是从左到右计算,所以实际执行的是下面的表达式。

                                                          ( i < j ) < k

                                                  ----->       1 < k

                                                  ------>          1

2 . 如果真的项判断变量 j 的值是否在 i 和 k 之间 , 应该使用下面的写法 

                                                    i < j && j <k

2.3 浮点数比较相等

1 . 在比较浮点数的时候 , 由于浮点数在计算机中是以有限精度表示的 , 也就是说有些浮点数在内存中其实无法精确保存 , 这可能导致浮点数比较中的一些精度误差的问题 。

2 . 如果直接使用 == 来比较两个浮点数 , 很可能会由于这些微小的误差导致不准确的结果

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	double a = 0.1;
	double b = 0.2;
	double c = 0.3;
	
	if (a + b == c) 
		cout << "a + b == c" << endl;
	else
		cout << "a + b != c" << endl;
		
	return 0;
}

那浮点数怎么比较呢?

----> 这时候就要允许有误差存在,常见的写法如下: 

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double a = 0.1;
	double b = 0.2;
	double c = 0.3;
	
	if (fabs((a + b) - c) < 1e-9)
		// 1e-9 表示允许的误差范围
		cout << "a + b 约等于 c" << endl;
	else
		cout << "a + b 不等于 c" << endl;
	return 0;
}

 

2.4 练习

练习一:有一门课不及格的学生

B2044 有一门课不及格的学生 - 洛谷

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int s1,s2,s3;
	int c = 0;
	cin >> s1 >> s2 >> s3;
	if(s1 < 60)
		c++;
	if(s2 < 60)
		c++;
	if(s3 < 60)
		c++;
	if(c==1)
		cout << "1" << endl;
	else
		cout << "0" << endl;		
	return 0;
}

方法2 :

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int s1,s2,s3;
	cin >> s1 >> s2 >> s3;
	if((s1 < 60) + (s2 < 60) + (s3 < 60)==1)
		cout << "1" << endl;
	else
		cout << "0" << endl;		
	return 0;
}

练习二:等差数列的末项计算

B2032 等差数列末项计算 - 洛谷

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int a1,a2,n;
	cin >> a1 >> a2 >> n;
	if(n == 1)
		cout << a1 << endl;	
	else if(n == 2)
		cout << a2 << endl;	
	else
		cout << a2 + (n-2)*(a2-a1) << endl;		
	return 0;
}

 

三 、条件操作符

3.1 条件操作符介绍

条件操作符也叫 三目操作符,需要接受三个操作数的 ,形式如下:
                                               exp1 ? exp2 : exp3
exp1 为真  ---> 执行exp2
         为假  --->  执行exp3

 

3.2 练习

练习一:最大值输出

B2049 最大数输出 - 洛谷

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int a,b,c;
	int m = 0;
	cin >> a >> b >> c;
	m = (a > b ? a : b);
	if(m < c)
		m = c;
	cout << m << endl;
	return 0;
}
三⽬操作符不仅应用在比大小场景中,在后续的学习中我们会见到更加丰富的用法,但是⼀
般不会应用在非常复杂的逻辑中。

练习二:特殊计算

登录—专业IT笔试面试备考平台_牛客网

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	long long x,y,z;
	cin >> x >> y;
	if(y%x == 0)
		z = x + y;
	else
		z = y - x;
	cout << z << endl;
	return 0;
}

改为条件操作符:

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	long long x,y,z;
	cin >> x >> y;
	z = ((y%x == 0) ? x + y :y -x);
	cout << z << endl;
	return 0;
}

练习三:Apples Prologue/苹果和虫子

P5709 【深基2.习6】Apples Prologue / 苹果和虫子 - 洛谷

对题目进行基础分析 : 

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int m,t,s;
	//r -- 剩余的苹果数 
	int r = 0;
	cin >> m >> t >> s;
	if(s%t == 0)
		r = m - s / t;
	else
		r = m - s / t - 1;
	cout << r << endl;
	return 0;
}

t 可能为 0 , 但是在除法中 , 不允许除数为0   ----> 特殊处理 t == 0

 从数值分析 --->  m = 50  、 t = 1 、 s = 10000   ---> r < 0

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int m,t,s;
	//r -- 剩余的苹果数 
	int r = 0;
	cin >> m >> t >> s;
	if(t == 0)
	{
		cout << 0 << endl;
		return 0;		
	} 
	if(s%t == 0)
		r = ((m - s / t) > 0 ? (m - s / t) : 0);
	else
		r = ((m - s / t - 1 ) > 0 ? (m - s / t - 1) : 0);
	cout << r << endl;
	return 0;
}


http://www.kler.cn/a/558666.html

相关文章:

  • 【Arxiv 大模型最新进展】LEARNING HOW HARD TO THINK: 精准思考,智能分配算力(★AI最前线★)
  • 《深入探索Vben框架:使用经验与心得分享》
  • 数仓搭建实操(传统数仓oracle):DWD数据明细层
  • MySQL数据库——索引结构之B+树
  • MySQL要点总结二
  • centos9之ESXi环境下安装
  • OpenAI 周活用户破 4 亿,GPT-4.5 或下周发布,微软加紧扩容服务器
  • 智慧废品回收小程序php+uniapp
  • SMU2025-4
  • 计算机组成与接口5
  • 前端实现socket 中断重连
  • J4打卡—— ResNet 和 DenseNet结合实现鸟类分类
  • 解决phpstudy无法启动MySQL服务
  • SkyWalking集成Kafka实现日志异步采集经验总结
  • 【行业解决方案篇十八】【DeepSeek航空航天:故障诊断专家系统 】
  • BFS(广度优先搜索)的理解与代码实现
  • AI知识架构之AI大模型
  • Express + MongoDB 实现新增用户密码加密
  • C++单例模板类,继承及使用
  • http 协议在互联网中扮演着怎样的角色?