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

NO.16十六届蓝桥杯备战|for循环|七道习题|ceil|floor|pow(C++)

for循环

for循环语法形式

for 循环是三种循环中使⽤最多的, for 循环的语法形式如下:

//形式1  
for(表达式1; 表达式2; 表达式3)  
语句;

//形式2  
//如果循环体想包含更多的语句,可以加上⼤括号  
for(表达式1; 表达式2; 表达式3)  
{  
	语句1;  
	语句2;  
	...  
}

表达式1 ⽤于循环变量的初始化
表达式2 ⽤于循环结束条件的判断
表达式3 ⽤于循环变量的调整

执⾏流程

![[Pasted image 20250212110204.png]]

⾸先执⾏ 表达式1 初始化循环变量,接下来就是执⾏ 表达式2 的判断部分, 表达式2 的结果如果
==0,则循环结束; 表达式2 的结果如果!=0则执⾏循环语句,循环语句执⾏完后,再去执⾏表 达式3,调整循环变量,然后再去 表达式2 的地⽅执⾏判断, 表达式2 的结果是否为0,决定循环是否继续。
整个循环的过程中, 表达式1 初始化部分只被执⾏1次,剩下的就是 表达式2 、循环语句、 表达式3在循环。

实践

练习
使⽤ for 循环在屏幕上打印1~10的值

#include <iostream>
using namespace std;

int main()
{
	int i = 0;
	for (i = 0; i <= 10; i++)
	{
		cout << i << " ";
	}

	return 0;
}

#include <iostream>
using namespace std;

int main()
{
	for (int i = 0; i <= 10; i++)
	{
		cout << i << " ";
	}

	return 0;
}

![[Pasted image 20250212125934.png]]
上⾯两种代码的差异是,代码1中for循环结束后,i的值还可以继续使⽤;但是代码2中for循环的外边是不能使⽤的,i只能在for循环中使⽤。

while和for对⽐

![[Pasted image 20250212130033.png]]

for 和 while 在实现循环的过程中都有初始化、判断、调整这三个部分,但是 for 循环的三个部分⾮常集中,便于代码的维护,⽽如果代码较多的时候 while 循环的三个部分就⽐较分散,所以从形式上 for 循环要更优⼀些。

练习

求和2

计算1~100之间3的倍数的数字之和

#include <iostream>
using namespace std;

int main()
{
	int sum = 0;
	for (int i = 1; i <= 100; i++)
	{
		if (i % 3 == 0)
			sum += i;
	}
	cout << sum << endl;

	return 0;
}

// 优化
#include <iostream>
using namespace std;

int main()
{
	int sum = 0;
	for (int i = 3; i <= 100; i += 3)
	{
		sum += i;
	}
	cout << sum << endl;

	return 0;
}
B2054 求平均年龄
#include <iostream>
using namespace std;
#include <cstdio>

int tmp;
int sum;

int main()
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> tmp;
        sum += tmp;
    }
    printf("%.2f\n", sum * 1.0 / n);
    
    return 0;
}
B2058 奥运奖牌计数
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a, b, c;
    int r1 = 0, r2 = 0, r3 = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b >> c;
        r1 += a;
        r2 += b;
        r3 += c;
    }
    int sum = r1 + r2 + r3;
    printf("%d %d %d %d\n", r1, r2, r3, sum);
    
    return 0;
}
B2065 鸡尾酒疗法
#include <iostream>
using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    int s1, s2;
    cin >> s1 >> s2;
    double p = s2 * 1.0 / s1;
    for (int i = 1; i < n; i++)
    {
        cin >> s1 >> s2;
        if (p - s2 * 1.0 / s1 > 0.05)
            cout << "worse" << endl;
        else if (s2 * 1.0 / s1 - p > 0.05)
            cout << "better" << endl;
        else
            cout << "same" << endl;
    }
    
    return 0;
}
B2066 救援
#include <iostream>  
#include <cmath>  
using namespace std;  

int n;
double x, y;  
int p;  

int main()  
{  
	cin >> n;  
	double t = 0;  
	for (int i = 0; i < n; i++)  
	{  
		cin >> x >> y >> p;  
		float dis = sqrt(x * x + y * y);  
		t += p * 1 + dis / 50 + p * 0.5 + dis / 50;  
	}  
	cout << (int)ceil(t) << endl;  
	
	return 0;  
}

![[Pasted image 20250213134826.png]]

向上取整算法

double d = 6.5
int q = (int)d;
double q = d - q;
if (p > 0.0)
	(int)(d + 1);
else
	(int)d;

ceil 和 floor 函数

  • ceil - 对⼀个浮点数向上取整,需要头⽂件 <cmath>
#include <iostream>  
#include <cmath>  
using namespace std;  

int main()  
{  
	cout << ceil(2.3) << endl;  
	cout << ceil(3.8) << endl;  
	cout << ceil(-2.3) << endl;  
	cout << ceil(-3.8) << endl;

	return 0;  
}

输出结果

3 
4  
-2  
-3
  • floor - 对⼀个浮点数向下取整,需要头⽂件 <cmath>
#include <iostream>  
#include <cmath>  
using namespace std;  

int main()  
{  
	cout << floor(2.3) << endl;  
	cout << floor(3.8) << endl;  
	cout << floor(-2.3) << endl;  
	cout << floor(-3.8) << endl;  
	
	return 0;  
}

输出结果

2 
3  
-3  
-4
B2070 计算分数加减表达式的值
#include <iostream>
using namespace std;

int n;

int main()
{
    cin >> n;
    double sn = 0;
    int flag = 1;
    for (int i = 1; i <= n; i++)
    {
        sn += flag * 1.0 / i;
        flag = -flag;
    }
    printf("%.4f", sn);
    
    return 0;
}
#include <iostream>
using namespace std;
#include <cmath>

int n;

int main()
{
    cin >> n;
    double sn = 0;
    for (int i = 1; i <= n; i++)
    {
        sn += pow(-1, i-1) * 1.0 / i;
    }
    printf("%.4f", sn);
    
    return 0;
}
B2069 求分数序列和
#include <iostream>
using namespace std;
#include <cstdio>

int n;

int main()
{
    cin >> n;
    int p = 1, q = 2;
    double sum = 0;
    for (int i = 1; i <= n; i++)
    {
        sum += q * 1.0 / p;
        int tmp = p + q;
        p = q;
        q = tmp;
        //或者q = q + p; p = q - p;
    }
    printf("%.4f\n", sum);
    
    return 0;
}

文章转载自:

http://eEgqsNxo.hrhwn.cn
http://XyrXprzB.hrhwn.cn
http://rgeI1x4W.hrhwn.cn
http://eUEmdodx.hrhwn.cn
http://qi0zOf9k.hrhwn.cn
http://fxBE4CyC.hrhwn.cn
http://kcRCjwt9.hrhwn.cn
http://EVEpQKdn.hrhwn.cn
http://a6OXkYYt.hrhwn.cn
http://ueSIQPTZ.hrhwn.cn
http://YKsLZiwp.hrhwn.cn
http://3XwHV6SL.hrhwn.cn
http://oECH7s9M.hrhwn.cn
http://SO1mZOVb.hrhwn.cn
http://kujVANGK.hrhwn.cn
http://ybp1uHbZ.hrhwn.cn
http://D1U3eo6E.hrhwn.cn
http://A9wsJrjd.hrhwn.cn
http://hfp4BnEK.hrhwn.cn
http://4taLlTuV.hrhwn.cn
http://qNRT7veW.hrhwn.cn
http://jhfOPzYO.hrhwn.cn
http://dSXqUQII.hrhwn.cn
http://S5vdA2L1.hrhwn.cn
http://cOEoI2VT.hrhwn.cn
http://410F8p8l.hrhwn.cn
http://sDh1Huna.hrhwn.cn
http://49tSePNx.hrhwn.cn
http://WGef0RbK.hrhwn.cn
http://NaWsurZc.hrhwn.cn
http://www.dtcms.com/a/14763.html

相关文章:

  • 以什么方式维护html网页的多语言版本比较好
  • 渗透测试--文件包含漏洞
  • 蓝桥杯算法日记|2.11二分算法
  • C语言之循环结构:直到型循环
  • 点大商城V2-2.6.6源码全开源uniapp +搭建教程
  • 安装WPS后,导致python调用Excel.Application异常,解决办法
  • 【C++ 真题】P1824 进击的奶牛
  • elementUI tree树形控件 根据数据动态设置禁用,全选时不可选中禁用数据
  • 股指期货和etf期权哪个更好交易?
  • 零基础学CocosCreator·第九季-网络游戏同步策略与ESC架构
  • 在 PyCharm 中接入deepseek的API的各种方法
  • CNN-BiGRU卷积神经网络双向门控循环单元多变量多步预测,光伏功率预测
  • 【Java常用】注解与反射_2.反射
  • 讯方·智汇云校华为授权培训机构的介绍
  • DeepSeek的开源核爆:当技术民主化重构AI权力版图
  • 常用数据格式:json、bson、msgpack
  • POI 的 Excel 读写操作教程
  • Ubuntu 22.04 - OpenLDAP安装使用(服务器+LAM+客户端)
  • 对正则表达式说不!!!
  • 【Android开发】华为手机安装包安装失败“应用是非正式版发布版本,当前设备不支持安装”问题解决
  • CentOS本机配置为时间源
  • 自定义基座实时采集uniapp日志
  • depcheck检查node.js项目中未使用和缺失依赖的工具
  • 【Apache Paimon】-- 作为一名小白,如何系统地学习 Apache paimon?
  • Ansible批量配置服务器免密登录步骤详解
  • 【pytest】获取所有用例名称并存于数据库
  • 联想电脑如何进入BIOS?
  • 新数据结构(9)——Java异常体系
  • AI编程01-生成前/后端接口对表-豆包(或Deepseek+WPS的AI
  • 【Vue3 入门到实战】15. 组件间通信