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

NO.15十六届蓝桥杯备战|while循环|六道练习(C++)

while循环

while语法形式

while 语句的语法结构和 if 语句⾮常相似,但不同的是 while 是⽤来实现循环的, if 是⽆法实现循环的。
下⾯是 while 循环的语法形式:

//形式1  
while ( 表达式 )
	语句;  

//形式2
//如果循环体想包含更多的语句,需要加上⼤括号  
while ( 表达式 )  
{  
	语句1;
	语句2; 
	...  
}
执⾏流程

![[Pasted image 20250211171920.png]]

⾸先上来就是执⾏判断表达式,表达式的值为 0 ,循环直接结束;表达式的值不为 0 ,则执⾏循环语句,语句执⾏完后再继续判断,是否进⾏下⼀次判断。

实践

题⽬
使⽤ while 循环在屏幕上打印1~10的值

#include <iostream>
using namespace std;

int main()
{
	int i = 1;
	while (i <= 10)
	{
		cout << i << " ";
		i++;
	}

	return 0;
}

![[Pasted image 20250211172629.png]]

练习

反向输出一个四位数
#include <iostream>
using namespace std;

char a, b, c, d;

int main()
{
    cin >> a >> b >> c >> d;
    cout << d << c << b << a;
    
    return 0;
}
#include <iostream>
using namespace std;

int n;

int main()
{
    cin >> n;
    while (n)
    {
        cout << n % 10;
        n /= 10;
    }
    
    return 0;
}
  1. 要想得到 n 的最低位,可以使⽤ n % 10 的运算,得到的余数就是最低位,如:1234 % 10 得到4
  2. 要想去掉n的最低位,找出倒数第⼆位,则使⽤ n = n / 10 操作就可以去掉最低位的,如: n=1234/10 得到 123 ,123相较于1234就去掉了最低位, 123%10 就得到倒数第⼆位 3 。
  3. 循环1和2两个步骤,在n变成0之前,就能到所有的位。
    1234%10 = 4
    1234/10 = 123
    123%10 = 3
    123/10 = 12
    12%10 = 2
    12/10 = 1
    1%10 = 1
    1/10 = 0
数位之和
#include <iostream>
using namespace std;

int n;

int main()
{
    int sum = 0;
    cin >> n;
    while (n)
    {
        sum += n % 10;
        n /= 10;
    }
    cout << sum << endl;
    
    return 0;
}
小乐乐求和
#include <iostream>
using namespace std;
  
int n;
  
int main()
{
    cin >> n;
    int i = 1;
    long long sum = 0;
    while (i <= n)
    {
        sum += i;
        i++;
    }
    cout << sum << endl;
  
    return 0;
}
#include <iostream>
using namespace std;

long long n;
  
int main()
{
    cin >> n;
    long long sum = 0;
    sum = n * (n + 1) / 2;
    cout << sum << endl;

    return 0;
}

注意数据范围,合理设置变量数据类型,1 ≤ n ≤ 10^9,那么求和的结果在int类型的变量中是⽆法保存的。

B2078 含 k 个 3 的数
#include <iostream>
using namespace std;

long long m, k;

int main()
{
    cin >> m >> k;
    long long i = 0;
    while (m)
    {
        if (m % 10 == 3)
            i++;
        m /= 10;
    }
    if (i == k)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    
    return 0;
}

还是数据范围的问题,使⽤long long类型是合适的。

B2077 角谷猜想 - 洛谷
#include <iostream>
using namespace std;
#include <cstdio>

long long x;

int main()
{
    cin >> x;
    while (x != 1)
    {
        if (x % 2 == 1)
        {
            printf("%lld*3+1=%lld\n", x, x * 3 + 1);
            x = x * 3 + 1;
        }
        else
        {
            printf("%lld/2=%lld\n", x, x/2);
            x /= 2;
        }
    }
    cout << "End" << endl;
    
    return 0;
}
#include <iostream>  
using namespace std;  
int main()  
{  
	long long n = 0;  
	
	cin >> n;  
	while (n != 1)  
	{  
		if (n % 2 == 1)  
		{  
			cout << n << "*3+1=" << n * 3 + 1 << endl;  
			n = n * 3 + 1;  
		}  
		else  
		{  
			cout << n << "/2=" << n / 2 << endl;  
			n /= 2;  
		}  
	}  
	cout << "End" << endl;  
	
	return 0;  
}
B2080 计算多项式的值
#include <iostream>
using namespace std;
#include <cstdio>

double x;
int n;

int main()
{
    cin >> x >> n;
    double ret = 1;
    double tmp = 1;
    while (n--)
    {
        tmp *= x;
        ret += tmp;
    }
    printf ("%.2lf\n", ret);
    
    return 0;
}

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

相关文章:

  • 信息安全工程师-快速记忆GB17859中的五个安全保护等级
  • 重读《Java面试题,10万字208道Java经典面试题总结(附答案)》
  • clickhouse集群搭建
  • 20250213 隨筆 雪花算法
  • 【AI-34】机器学习常用七大算法
  • 力扣LeetCode: 1552 两球之间的磁力
  • Webpack代码分割、分割策略性能优化详解
  • rust学习一、入门之搭建简单开发环境
  • 【网络法医】恶意软件分析
  • Ubuntu20.04桥接网络和静态IP配置
  • 能用GPT解决的,就不用自己动手
  • 深入探讨复变函数:核心概念与关键应用
  • Django html模板的继承
  • 【H5自适应】高端科技类pbootcms网站模板 – 三级栏目、下载与招聘功能支持
  • kibana es 语法记录 elaticsearch
  • 稀土抑烟剂——为纺织品安全加持,保护您的每一寸触感
  • 系统巡检脚本分享:守护服务器的“健康卫士”
  • 【系统架构设计师】操作系统 - 进程管理 ② ( 进程状态 | 三态模型 | 五态模型 | 进程状态 划分依据 | PCB 程序控制块 的 组织方式 )
  • JavaScript 网页设计案例:经典与创新的完美结合
  • 【第5章:深度生成模型— 5.1 变分自编码器(VAE)与生成对抗网络(GAN)的基础理论】