两学一做山东网站/网站空间租用

两学一做山东网站,网站空间租用,怎么优化网站排名具体怎么做,公司注销查询系统一、for循环 1.1 for 循环语法形式 for 循环是三种循环中使用最多的 , for 循环的语法形式如下: 1.2 执行流程 for 循环中 , 表达式1(初始化)只执行一次 ! 1.3 实践 练习:使用 for 循环在屏幕…

一、for循环

1.1  for 循环语法形式

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

1.2 执行流程

for 循环中 , 表达式1(初始化)只执行一次 !

1.3 实践

练习:使用 for 循环在屏幕上打印1~10的值
#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 0;for(i = 1;i<=10;i++){cout << i << " ";}return 0;
}

1.4 while 和 for 对比

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

1.5 练习

练习一 : 求和2

计算1~100之间3的倍数的数字之和
思路 :
1 . 产生 1 ~100 的所有数字 --> for
2 . 判断是否是 3 的倍数  ------> if(i % 3 == 0)
3 . 如果是3的倍数,就求和 ---> sum +=i
#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 0;int sum = 0;for(i = 1 ;i<=100 ; i++){if(i % 3 == 0)sum += i;}cout << sum << endl;return 0;
}

如果直接产生3的倍数 的数字 就省去了多余的循环和判断 

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 0;int sum = 0;for(i = 3 ;i<=100 ; i+= 3){sum += i;}cout << sum << endl;return 0;
}

两个方法相比 , 第二个较优一些 , 执行的次数比代码一少! 

练习二 : 求平均年龄

B2054 求平均年龄 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n = 0;cin >> n;int age = 0;int sum = 0;for(int i = 1 ;i <= n ; i++){cin >> age;sum +=  age;}printf("%.2lf\n",sum*1.0/n);return 0;
}

练习三 : 奥运奖牌计数

B2058 奥运奖牌计数 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int a, b , c ;//每一天的奖牌数 
int p1, p2, p3;
int sum;
int main()
{int n = 0;cin >> n;for(int i = 1 ; i<= n ; i++){//读取每一天的奖牌数cin >> a >> b >> c;p1 += a;p2 += b;p3 += c;} sum = p1 + p2 + p3;cout << p1 << " " << p2 << " " << p3 << " " <<  sum << endl;return 0;
}

练习四 : 鸡尾酒疗法

B2065 鸡尾酒疗法 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int a,b; //a:总病例数 b:有效病例数 
float x,y; //x:鸡尾酒疗法有效率 y:改进后的 
int main()
{int n = 0;//1.输入n cin >> n;//2.读取第一组数据 cin >> a >> b;x = b*1.0 / a;//3.读取 n - 1 组的对照组数据for(int i = 1;i <= n-1 ; i++){cin >> a >> b;y = b * 1.0 / a;if(y - x > 0.05)cout << "better" << endl;else if(x - y > 0.05)cout << "worse" << endl;elsecout << "same" << endl;	} return 0;
}

练习五 :救援

B2066 救援 - 洛谷

在解决问题之前 , 先了解一下两个库函数ceil 和 floor 函数 : 

ceil : 对于一个浮点数向上取整 , 需要头文件 <cmath>

ceil - C++ Reference

#include <iostream>
#include <cstdio>
#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;
}

floor : 对一个浮点数向下取整 , 需要头文件 <cmath>

floor - C++ Reference

#include <iostream>
#include <cstdio>
#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;
}

解题思路:

1 . 输入n

2. 循环 n 次

           每次处理一个屋顶 (计算举例,计算时间,时间累加)

3. 输出时间 --> 向上取整

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;int p;
int n;
double x,y;
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;
}

练习六 :计算分数加减表达式的值

B2070 计算分数加减表达式的值 - 洛谷

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

还可以使用库函数 pow 函数 ---> pow(x,y) ,x的y次方 , 需要包含头文件<cmath> 

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

 还有一种方法 , 可以通过判断奇偶  来决定 sn是+= 还是-=

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

练习七 :求分数序列和

B2069 求分数序列和 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;double sum = 0;double p = 1;double q = 2;cin >> n;for(int i = 1 ; i<=n;i++){//1.产生1项 累计求和 sum += q / p;//2.为下一项准备 p 和 qq = q + p; p = q - p;}printf("%.4lf\n",sum);return 0;
}

二、do-while 循环

2.1 do-while语法形式

在循环语句中 , do while 语句的使用最少 , 它的语法如下 : 

 1. while 和 for 这两种循环都是先判断,条件如果满足就进入循环,执行循环语句,如果不满足就跳 出循环;

2. do while 循环则是 先直接进入循环体,执行循环语句 ,然后再执行  while 后的判断表达式,表达式为真,就会进行下⼀次,表达式为假,则不再继续循环。

2.2 执行流程

2.3 实践

使用 do while 循环在屏幕上打印1~10的值
#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 1;do{cout << i << " ";i++;}while(i <= 10);return 0;
}

2.4 练习

练习一 : 统计正整数的位数

输入一个正整数,计算这个整数是几位数?
例如:
输入:1234 输出:4
输入:12 输出:2
#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n = 0;cin >> n;int c = 0;do{c++;n/=10;}while(n);cout << c << endl;return 0;
}

这里并非必须使用 do while 语句 , 但是这个代码就比较适合使用 do while 循环 , 因为即使 n 是 0 , 也是 1 位数 , 需要统计位数。

以上的代码 , c 每 ++ , n 就删掉一位数(n % 10) 

练习二 :球弹跳高度的计算

B2076 球弹跳高度的计算 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{double h;cin >> h;double sum = h;int i = 1;do{h/=2;sum += 2*h;i++;}while(i<=9);cout << sum << endl;cout << h/2 << endl;return 0;
}

三 、break 和 continue 语句

3.1 break 和 continue 介绍

1 . 在循环执行的过程中 , 如果某些状况发生的时候 , 需要提前终止循环 , 这是非常常见的现象 。 C++ 中提供了break 和 continue 两个关键字 ,就是应该到循环中 。

2 . break 的作用是用于永久的终止循环 , 只要break 被执行 , 直接就会跳出循环 , 继续往后执行 。

3 . continue 的作用是跳出本次循环 continue 后边的代码 , 在 for 循环和 while 循环中 有所差异 。

4 . 注意break 和 continue 在各循环语句中的使用方式基本相同 , 这里仅以while 循环举例。 

3.2 break 举例

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 1;while(i <= 10){if(i == 5)break;cout << i << " ";i++;}return 0;
}

3.3 continue 举例

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 1;while(i <= 10){if(i == 5)continue;cout << i << " ";i++;}return 0;
}

如果在for 循环中使用continue 呢 ?

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int i = 0;for(i = 1;i<=10 ; i++){if(i == 5)continue;cout<< i << " ";}return 0;
}

这时候的 continue 在 while 循环和 for 循环中略有差异!!!

3.4 练习:质因数分解

B2084 质因数分解 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;for(int i = 2;i < n ; i++){//找能整除的最小因数 if(n % i == 0){cout << n / i << endl;break;}}return 0;
}

四、循环嵌套

4.1 循环嵌套的使用

前面我们学习了三种循环 : while , do while , for , 这三种循环往往会嵌套在一起才能更好解决问题 , 就是我们所说的 : 循环嵌套 。 这三种循环都可以任意嵌套使用

 比如 : 打印一个乘法口诀表

观察后 : 我们发现 每一项就是 ---> 列 * 行 = 乘积

#include <iostream>
#include <cstdio>
using namespace std;int main()
{for(int i = 1;i <=9 ; i++){for(int j = 1 ; j <= i ;j++){printf("%d*%d=%2d ",i,j,i*j);}printf("\n");}return 0;
} 

4.2 练习

练习一:乘法表

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

#include <iostream>
#include <cstdio>
using namespace std;int main()
{for(int i = 1;i <=9 ; i++){for(int j = 1 ; j <= i ;j++){printf("%d*%d=%2d ",j,i,i*j);}printf("\n");}return 0;
} 

练习二:包含数字9的数

包含数字9的数_牛客题霸_牛客网

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int c = 0;for(int i = 1 ; i<= 2019 ; i++){//判断i是否包含9//把i的每一位都拆出来int n = i;while(n){if(n % 10 == 9){c++;break;}n /= 10;} }cout << c << endl;return 0;
}

注意 : 在多层嵌套的循环中也可以使用 break , 但是要注意 , 一个break 只能跳出自己所在的循环 , 无法一次性跳出所有的循环 。 

练习三:斐波那契数列

B2064 斐波那契数列 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;int a = 0;while(n--)//n次询问{cin >> a;int x = 1;int y = 1;int z = 1;while(a > 2){z = x + y ;x = y;y = z;a--;	}	cout << z << endl;} return 0;
}

练习四:求出e的值

B2079 求出 e 的值 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;double sum = 1;for(int i = 1;i<=n;i++){//算出i的阶乘long long r = 1;for(int j=1;j<=i;j++){r *= j;}sum += 1.0 / r; }printf("%.10lf\n",sum);return 0;
}

优化 : 我们发现会有重复类乘的情况 ,比如求3! = 1*2*3 , 在算4! 的时候 又把 1*2*3 算了一遍 , 会有一点的时间消耗。

----> 计算阶乘的时候 , 是有连续性的 , 我们可以借助这个特点 , 对代码进行优化

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;double sum = 1;long long r = 1;for(int i = 1;i<=n;i++){r *= i;sum += 1.0 / r; }printf("%.10lf\n",sum);return 0;
}

练习五:三角形

信息学奥赛一本通(C++版)在线评测系统

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n = 0;cin >> n;//输出n行 for(int i = 1 ; i<= n;i++){//打印1行for(int j = 1 ; j <= i ; j++){cout<< "*";	} cout << endl;}return 0;
}

对于嵌套循环 , 一定要明白 , 每一个循环所对应的目标是什么 。 

练习六:画矩形

B2083 画矩形 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int a,b;
char c;
int f;
int main()
{cin >> a >> b >> c >> f;if(f == 0){for(int i = 1;i<=a;i++){//打印1行for(int j = 1;j<=b;j++){if(i == 1 || i == a || j== 1 || j== b)cout << c;elsecout << " ";	}	cout << endl;}	}else{//直接打印 a行b列的实心图形for(int i = 1;i<=a;i++){//打印1行for(int j = 1;j<=b;j++){cout << c;	}	cout << endl;}}return 0;
}

我们还可以对以上代码进行优化 : 

1 . 无论什么情况 , 第1行、第1列、第a行、第b列 都会打印c符

2 . 不同的是 , f为0时 , 中间的空格打印空字符  ,这里我们就可以用 else if ... else 语句来控制

#include <iostream>
#include <cstdio>
using namespace std;int a,b;
char c;
int f;
int main()
{cin >> a >> b >> c >> f;for(int i = 1;i<=a;i++){//打印1行for(int j = 1;j<=b;j++){if(i == 1 || i == a || j== 1 || j== b)cout << c;else if(f == 0)cout << " ";elsecout << c;	}	cout << endl;}return 0;
}

练习七:第n小的质数

B2085 第 n 小的质数 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;int i = 2;int c = 0;while(1){//判断i是否为素数int flag = 1;//假设i是素数for(int j = 2 ; j<= i-1;j++){if(i % j == 0){flag = 0;				break;	}}if(flag == 1)c++;if(c == n){break;	}i++; }cout << i << endl;return 0;
}

"Time Limit Exceeded"(TLE,超时)是⼀个在编程竞赛和在线评测平台(如LeetCode、
Codeforces、HackerRank等)中常见的错误信息。它意味着程序在执行过程中超过了给定的最大运行时间限制,而未能在规定时间内得出结果。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;int main()
{int n = 0;cin >> n;int c = 0;int i = 2;while(1){int flag = 1;//判断i是否是素数for(int j = 2;j <= sqrt(i) ;j++){if(i % j == 0){flag = 0;break;	}	}	if(flag == 1)c++;if( c == n){break;}i++;} cout << i << endl;return 0;
}

练习八 : 水仙花数

信息学奥赛一本通(C++版)在线评测系统

水仙花数 : 自幂数 --> 每个位上的数字的n次幂之和等于它本身

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;int main()
{for(int i = 100 ; i<= 999 ; i++){//判断i是否为水仙花数 int tmp = i;int r = 0;while(tmp){r += pow(tmp % 10,3);			tmp /= 10;}if(r == i)cout << i << endl;}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/72012.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VMware Fusion 虚拟机Mac版 安装CentOS 7 系统

介绍 CentOS是Community Enterprise Operating System的缩写&#xff0c;也叫做社区企业操作系统。是企业Linux发行版领头羊Red Hat Enterprise Linux的再编译版本&#xff08;是一个再发行版本&#xff09;&#xff0c;而且在RHEL的基础上修正了不少已知的 Bug &#xff0c;相…

如果更换ip地址会怎么样?网络ip地址怎么更换

IP地址&#xff0c;作为网络设备的数字身份证&#xff0c;其稳定性和安全性对于网络通讯至关重要。然而&#xff0c;在某些特定情况下&#xff0c;我们可能需要更换设备的IP地址&#xff0c;以满足安全、隐私或网络管理的需求。那么&#xff0c;如果更换IP地址会怎么样&#xf…

网络通信/IP网络划分/子网掩码的概念和使用

文章目录 概述子网的考题子网掩码的历史有/无类地址子网划分!子网掩码超网技术/CIDR子网掩码和路由IP子网掩码定义 网络规划网络规划-拆子网网络规划-组超网子网划分案例 区分于其他特殊IP地址IP地址和网络地址子网掩码和网络地址子网掩码和广播地址 子网间的通信其他 概述 本…

C++11相较于C++98的新特性介绍:列表初始化,右值引用与移动语义

一&#xff0c;列表初始化 1.1C98中传统的{} C98中一般数组和结构体可以使用{}进行初始化&#xff1a; struct Date {int _year;int _month;int _day; };int main() {int a[] { 1,2,3,4,5 };Date _date { 2025,2,27 };return 0; } 1.2C11中的{} C11以后想统一初始化方式&…

uniapp中使用leaferui使用Canvas绘制复杂异形表格的实现方法

需求&#xff1a; 如下图&#xff0c;要实现左图的样式&#xff0c;先实现框架&#xff0c;文字到时候 往里填就行了&#xff0c;原来的解决方案是想用css,html来实现&#xff0c;发现实现起来蛮麻烦的。我也没找到合适的实现方法&#xff0c;最后换使用canvas来实现&#xff…

大模型与呼叫中心融合:未来发展的潜力何在?

大模型与呼叫中心的结合&#xff0c;为企业带来了前所未有的发展机遇。通过提升服务效率、优化营销效果、降低运营成本、增强数据管理与分析能力、提升客户体验以及推动行业创新与变革&#xff0c;大模型呼叫中心正在重塑客户服务与营销的未来。 大模型与呼叫中心的结合具有巨…

vue3+ts+uniapp+unibest 微信小程序(第二篇)—— 图文详解自定义背景图页面布局、普通页面布局、分页表单页面布局

文章目录 简介一、自定义背景图布局1.1 效果预览1.2 实现思路1.3 custom-page 组件全量代码1.4 页面使用 二、普通页面布局2.1 效果预览2.2 实现思路2.3 公共样式部分2.4 页面使用 三、分页表单页面布局3.1 效果预览3.2 实现思路3.3 页面代码 简介 开发工具&#xff1a;VsCode…

python-leetcode-乘积最大子数组

152. 乘积最大子数组 - 力扣&#xff08;LeetCode&#xff09; class Solution:def maxProduct(self, nums: List[int]) -> int:if not nums:return 0max_prod nums[0]min_prod nums[0]result nums[0]for i in range(1, len(nums)):if nums[i] < 0:max_prod, min_prod…

RISC-V架构的平台级中断控制器(PLIC:platform-level interrupt controller)详解

英文缩写 英文缩写中文含义PLICplatform-level interrupt controller&#xff0c;平台级中断控制器SMTsimultaneous multi-threading&#xff0c;并发多线程HARTRISC-V架构中的硬件线程SMTsimultaneous multi-threading&#xff0c;多线程执行M-MODEmachine mode&#xff0c;机…

[Web 安全] PHP 反序列化漏洞 —— PHP 序列化 反序列化

关注这个专栏的其他相关笔记&#xff1a;[Web 安全] 反序列化漏洞 - 学习笔记-CSDN博客 0x01&#xff1a;PHP 序列化 — Serialize 序列化就是将对象的状态信息转化为可以存储或传输的形式的过程&#xff0c;在 PHP 中&#xff0c;通常使用 serialize() 函数来完成序列化的操作…

航空装配自动化神器Ethercat转profient网关搭配机器人精准控制

生产管理系统通过网关与装配机器人连接&#xff0c;加快航空器机身的装配速度&#xff0c;减少人为误差。 航空制造对装配线的精度和效率有着极高的要求。某航空制造厂使用的耐达讯Profinet转EtherCAT协议网关NY-PN-ECATM&#xff0c;将其生产管理系统与装配机器人连接&#xf…

什么是MySql的主从复制(主从同步)?

主页还有其他面试题总结&#xff0c;有需要的可以去看一下&#xff0c;喜欢的就留个三连再走吧~ 1.什么是MySql的主从复制原理&#xff1f; 主从复制的核心就是二进制binlog&#xff08;DDL&#xff08;数据定义语言&#xff09;语句和DML&#xff08;数据操纵语言&#xff09…

【人工智能】蓝耘智算平台盛大发布DeepSeek满血版:开创AI推理体验新纪元

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀ 蓝耘智算平台 蓝耘智算平台核心技术与突破元生代推理引擎快速入门&#xff1a;三步调用大模型接口&#xff0c;OpenAI SDK无缝兼容实战用例文…

【网络编程】几个常用命令:ping / netstat / xargs / pidof / watch

ping&#xff1a;检测网络联通 1. ping 的基本功能2. ping 的工作原理3. ping 的常见用法4. ping 的输出解释5. ping 的应用场景6. 注意事项 netstat&#xff1a;查看网络状态 1. netstat 的基本功能2. 常见用法3. 示例4. 输出字段解释5. netstat 的替代工具6. 注意事项 xargs&…

【数据结构】(11) Map 和 Set

一、Map 和 Set 的简介 1、Set 和 Map Map 和 Set 是集合类框架学习的最后一部分。Map 和 Set 都是接口&#xff0c;需要通过 TreeSet、HashSet 和 TreeMap、HashMap 实例化。注意&#xff0c;Set 实现了 Collection&#xff0c;Map 并没有。 Set 存放的是键&#xff08;Key&a…

【HTML— 快速入门】HTML 基础

准备工作 vscode下载 百度网盘 Subline Text 下载 Sublime Text下载 百度网盘 vscode 下载 Sublime Text 是一款轻量好用的文本编辑器&#xff0c;我们在写前端代码时&#xff0c;使用 Sublime Text 打开比使用记事本打开&#xff0c;得到的代码体验更好&#xff0c;比 vscode…

鸿蒙开发深入浅出01(基本环境搭建、页面模板与TabBar)

鸿蒙开发深入浅出01&#xff08;基本环境搭建、页面模板与TabBar&#xff09; 1、效果展示2、下载 DevEco Studio3、创建项目4、新建页面模板5、更改应用信息6、新建以下页面7、Index.ets8、真机运行9、图片资源文件 1、效果展示 2、下载 DevEco Studio 访问官网根据自己的版本…

鸿蒙next 点击穿透实现

点击穿透可以参考华为开发的保留文章,该章节只能在developer preview版本下查看 点击穿透 主要的方法是hitTestBehavior // xxx.ets Entry Component struct HitTestBehaviorExample {build() {// outer stackStack() {Button(outer button).onTouch((event) > {console.i…

27.[前端开发-JavaScript基础]Day04-函数基本使用-递归-变量作用域-函数式编程

一、JavaScript函数 1 认识JavaScript函数 程序中的foo、bar、baz 认识函数 函数使用的步骤 2 函数的声明和调用 声明和调用函数 函数的参数 有参数的函数练习 函数的返回值 函数的练习 arguments参数&#xff08;JS高级再学习&#xff09; 3 函数的递归调用 函数中调用函数…

添加成对约束后的标签传播算法研究:使用Python语言编写算法,在空手道数据集下验证算法的准确性,在一定程度上解决非对齐问题

背景&#xff1a; 辅导的过程中遇到了一个比较新颖的问题&#xff0c;下面是我对这个问题的分析和简要思路介绍。 思路分析&#xff1a; 这算机器学习下面的无监督学习&#xff0c;标签传播算法简称LPA&#xff0c;传统的标签传播算法会出现非对齐问题&#xff0c;一句话描述就…