蓝桥备赛(六)- C/C++输入输出
一、OJ题目输入情况汇总
OJ(online judge)

接下来会有例题 , 根据一下题目 , 对这些情况进行分析
1.1 单组测试用例
单在 --> 程序运行一次 , 就处理一组
练习一:计算 (a+b)/c 的值
B2009 计算 (a+b)/c 的值 - 洛谷
#include <iostream>
#include <cstdio>
using namespace std;int main()
{int a,b,c;cin >> a >> b >> c;cout << (a+b)/c << endl;return 0;
}

练习二:与 7 无关的数
B2081 与 7 无关的数 - 洛谷
这题也是多个测试点 , 但是运行一次 , 仅仅处理一组数据!

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;int i = 1 ;int sum = 0;while(i <= n){if(i % 7 != 0 && i % 10 != 7 && i / 10 % 10 != 7)sum += (i*i);i++; }cout << sum << endl;return 0;
}
1.2 多组测试用例
多组 : 程序运行一次 , 处理多组 。就像我们前面做的题目 , n次访问。

测试数据组数已知:
练习一:多组输入 a+b||
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;int a,b;while(n--){cin >> a >> b;cout << a + b << endl;}return 0;
}
练习二:斐波那契数列
B2064 斐波那契数列 - 洛谷

#include <iostream>
#include <cstdio>
using namespace std;int main()
{int n;cin >> n;int a = 0;int ret[40] = {0,1,1};for(int i = 3; i<30; i++){ret[i] = ret[i-1] + ret[i-2];}while(n--){cin >> a;cout<< ret[a] << endl;}return 0;
}
练习三:制糊串
B3769 [语言月赛202305] 制糊串 - 洛谷

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;int main()
{string s,t;cin >> s >> t;int q = 0;cin >> q;int l1,r1,l2,r2;while(q--){cin >> l1 >> r1 >> l2 >> r2;string s1 = s.substr(l1 -1 , r1 - l1 + 1); string s2 = t.substr(l2 -1 , r2 - l2 + 1); //比较if(s1 > s2) cout << "erfusuer" <<endl;else if(s1 < s2)cout << "yifusuyi" << endl;elsecout << "ovo" << endl;}return 0;
}
题目中说“有q次询问” , 意思是程序要处理q组数据 , (也就是对应q次循环) , 需要针对每一次的访问 , 给出一个结果。
就是之前的单组测试 变成了 q 组测试 , 在之前代码加上while 循环即可 。当进行q次访问的时候 , 使用while(q--) , 是非常方便的 !!!
测试数据组数未知:
练习一:多组输入a+b
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;int main()
{int a , b;while(cin >> a >> b){cout << a + b << endl;}return 0;
}
这道题的难点就是 , 怎么去判断读取结束呢?
1 . cin >> a ; 会返回一个流对象的引用 , 即cin本身 在C++中 , 流对象可以被用作布尔值来检查流的状态。流的状态良好 --> true ; 发生错误(如遇到输入结束符或类型不匹配) -->false
2. 所以在while(cin >> a >> b) 语句中 , 流成功读取两个值 , 返回流对象cin 被转化为 true,循环继续 。如果读取失败 , 返回false , 循环停止 。
练习二:数字三角形
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;int main()
{int n;while(cin >> n){for(int i = 1;i <= n;i++){for(int j = 1;j <=i ; j++){cout << j << " ";}cout << endl;}}return 0;
}
练习三:定位查找
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;const int N = 25;
int arr[N];
int main()
{int n = 0;int m = 0;int i = 0;while(cin >> n){//输入n组数据,存储在数组arr for( i = 0;i < n;i++){cin >> arr[i];}//处理数据cin >> m;for( i = 0;i < n;i++){if(arr[i] == m){cout << i << endl;break;//找到第一个,直接结束这组数据查找 } } //走到这里,没有找到m//如果不加i==n,那么无论什么情况都会打印No if(i == n)cout << "No" << endl; } return 0;
}
注意 , 打印No的时候 , 需要条件 , 否则无论是否找到 , 都会打印No;
方法二 : 用一个flag ,记录是否找到
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;const int N = 25;
int arr[N];
int main()
{int n = 0;int m = 0;while(cin >> n){int flag = 0; //输入n组数据,存储在数组arr for(int i = 0;i < n;i++){cin >> arr[i];}//处理数据cin >> m;for(int i = 0;i < n;i++){if(arr[i] == m){cout << i << endl;flag = 1;break;//找到第一个,直接结束这组数据查找 } } //走到这里,没有找到mif(flag == 0)cout << "No" << endl; } return 0;
}
注意 : 这里的flag 需要放在while 循环体内 否则 , 一次循环结束 , flag 将不会被重置为0
特殊值结束测试数据:
练习一:字符统计
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;//方法一
int main()
{int ch;int Letters = 0;int Digits = 0;int Others = 0;while((ch = getchar()) != '?'){if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))Letters++;else if(ch >= '0' && ch <= '9') Digits++;elseOthers++;}cout << "Letters=" << Letters << endl;cout << "Digits=" << Digits << endl;cout << "Others=" << Others << endl;return 0;}
灵活运用库函数 ---> isalpha 、 isdigit
记得加上头文件 <cctype>
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;//方法二 -- 借助库函数isalpha、isdigit
int main()
{int ch;int Letters = 0;int Digits = 0;int Others = 0;while((ch = getchar()) != '?'){if(isalpha(ch))Letters++;else if(isdigit(ch)) Digits++;elseOthers++;}cout << "Letters=" << Letters << endl;cout << "Digits=" << Digits << endl;cout << "Others=" << Others << endl;return 0;}
方法三:直接输入整个字符 , 但问号不需要统计 , 那么可以借助 pop_back() , 来删掉问好
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;//方法三
int main()
{string s;int Letters = 0;int Digits = 0;int Others = 0;getline(cin,s);//此字符串是以?结束的,不想统计?s.pop_back() ; for(auto e:s){ if(isalpha(e))Letters++;else if(isdigit(e)) Digits++;elseOthers++;}cout << "Letters=" << Letters << endl;cout << "Digits=" << Digits << endl;cout << "Others=" << Others << endl;return 0;}
练习二:多组数据a+b III
登录—专业IT笔试面试备考平台_牛客网

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;int main()
{int a,b;while(cin >> a >> b){if(a==0 && b==0)break;cout << a+b << endl; }return 0;
}
方法二:借助逗号表达式
1) 从左到右依次计计算
2)整个表达式的结果是 最后一个表达式的结果
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;int main()
{int a,b;while(cin >> a >> b,a&&b){cout << a+b << endl; }return 0;
}
以下是逗号表达式的举例讲解 :

二、输入中的特殊技巧
2.1 技巧1:含空格字符串的特殊处理方式

根据我们已经所学的知识 , 处理含有空格的字符串 可以使用 : fgets , scanf , getchar , getline 。具体使用需要据题目分析 ,有时候只是为了读取单词 , 忽略空格 。
练习:统计数字字符个数
B2109 统计数字字符个数 - 洛谷

方法一:一次性处理整个字符串 ;然后再遍历字符串 , 如果是数字 , 就用计数器统计出来。
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;//方法一:一次性处理整个字符串
int main()
{string s;int c = 0;getline(cin,s);for(auto e:s){if(isdigit(e))c++;}cout << c << endl;return 0;
}
方法二 : 逐个字母处理
1 . cin 读取字符串的时候 , 遇到空格就会停止 ;借助这一个特点 , 在while (cin >> s ) 中 , 整个字符串如果遇到空格就会跳过 , 继续处理下一个单词 。
2 . 当cin 读取失败后 , 返回false , while 循环结束
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;//方法二:逐个单词处理
int main()
{int c = 0;string s;//cin 读取字符串的时候,不会读取空格//遇到空格就停止//运用cin的特性,while这里遇到空格就会跳过 while(cin >> s){for(auto e:s){if(isdigit(e))c++;}}cout << c << endl;return 0;
}
2.2 技巧2:数字的特殊处理方式
当我们程序运行的时候 , 在控制台输入 123 的时候 , 这时候的 123 是3个字符,123是一个字符序列 , 程序会根据代码中的数据类型 , 可能将123解析成整型 , 也可能将123 解析成字符串 。

练习:小乐乐改数字
小乐乐改数字_牛客题霸_牛客网


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;int main()
{int n = 0;cin >> n;int ret = 0;int i = 0;//记录处理到多少位while(n){if(n % 10 % 2 != 0)ret += pow(10,i);n /= 10;i++;}cout << ret << endl;
}

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;int main()
{string s;cin >> s;for(int i = 0; i < s.size() ;i++){if(s[i] % 2 == 0){s[i] = '0'; } elses[i] = '1';} cout << stoi(s) << endl;return 0;
}
三、scanf/printf 和 cin/cout 的区别
scanf 和 printf 是C语言中标准输入输出函数 , 而 cin 和 cout 是 C++语言中的标准输入输出流对象 。各自有优缺点 , 整体上来说 , cin 和 cout 会更加方便 ; 但是有时候不得不使用scanf 和 printf !!!
3.1 使用上的差异

1 ) scanf 和 printf 不能自动识别输入数据的类型 , 需要手动指定格式字符串 , 容易出现格式错误 。使用时候 需要确保 格式字符串和变量类型匹配 , 否则会出现未定义行为。
2 )cin 和 cout 会根据变量类型自动处理输入输出 , 避免格式化错误 。 相对scanf 和 printf,C++ 会更加易用。
3 ) scanf 和 printf : 格式化输出更加精确直观 , 非常适合复杂格式的输入输出 , 比如需要特定的格式化输出的时候 。
#include <iostream>
#include <cstdio>
using namespace std;int main()
{float a = 3.50;double d = 16.50;cout << "cout: " <<a << " "<< d <<endl;printf("printf: %f %lf\n", a, d);return 0;
}


3.2 性能差异
案例演示
结论 : scanf 和 printf 通常比cin 和 cout 快

案例一:数字游戏

使用cin / cout :
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;int t, x;
int main()
{cin >> t;while (t--){cin >> x;int ret = 0;while (x){int count = 0, high = 0;int tmp = x;while (tmp){//计算最右边的1代表的值int low = tmp & -tmp;//如果low中剩余的1就是最后一个1//就是最左边的1if (tmp == low){high = low;}//去掉最右边的1tmp -= low;count++;}if (count % 2 == 0){x -= high;}else{x ^= 1;}ret++;}cout << ret << endl;}return 0;
}

使用scanf / printf :
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;int t, x;
int main()
{scanf("%d", &t);while (t--){scanf("%d", &x);int ret = 0;while (x){int count = 0, high = 0;int tmp = x;while (tmp){//计算最右边的1代表的值int low = tmp & -tmp;//如果low中剩余的1就是最后一个1//就是最左边的1if (tmp == low){high = low;}//去掉最右边的1tmp -= low;count++;}if (count % 2 == 0){x -= high;}else{x ^= 1;}ret++;}printf("%d\n", ret);}return 0;
}

案例二:求第 k 小的数
P1923 【深基9.例4】求第 k 小的数 - 洛谷

使用cin / cout :
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;const int N = 5000010;
int arr[N];
int main()
{int n, k;cin >> n >> k;for (int i = 0; i < n; i++){cin >> arr[i];}sort(arr, arr + n);cout << arr[k] << endl;return 0;
}

使用scanf / printf :
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;const int N = 5000010;
int arr[N];
int main()
{int n, k;cin >> n >> k;for (int i = 0; i < n; i++){scanf("%d", &arr[i]);}sort(arr, arr + n);cout << arr[k] << endl;return 0;
}

上面两个案例中,输入的数据量都比较大,在输入数据的时候如果使用 cin ,都会出现超时的问题,但是换成是 scanf 的方式就能正确的通过。这就是因为两者性能上的差异导致的。
优化方式:
ios::sync_with_stdio(false); // 取消C风格I/O的同步
cin.tie(0); // 解除cin与cout的绑定
总结一下其实就是两个点:
1. C++中为了支持混合使用 cin/cout 和 scanf/printf ,C++ 标准库默认会将 cin 、 cout
等 C++ 流对象与 stdin 、 stdout 等 C 标准库的流对象同步在⼀起。 这种同步操作意味着次使⽤ cin 或 cout 时,都会自动刷新 C 标准库的缓冲区,以确保 C++ 和 C 的 I/O 是一致的。这就导致了性能的下降。2. 在默认情况下, cin 和 cout 之间存在⼀种绑定关系。这种绑定意味着,每当从 cin 读取数据时 ,任何之前通过 cout 输出的内容都会被强制刷新到屏幕上。 这种绑定也可能导致性能问题,特别是在需要频繁读取⼤量数据的情况下。
所以,对于数据量较大 , 使用scanf / printf 。
相关文章:
蓝桥备赛(六)- C/C++输入输出
一、OJ题目输入情况汇总 OJ(online judge) 接下来会有例题 , 根据一下题目 , 对这些情况进行分析 1.1 单组测试用例 单在 --> 程序运行一次 , 就处理一组 练习一:计算 (ab)/c 的值 B2009 计算 (ab)/c …...
企微审批中MySQL字段TEXT类型被截断的排查与修复实践
在MySQL中,TEXT类型字段常用于存储较大的文本数据,但在一些应用场景中,当文本内容较大时,TEXT类型字段可能无法满足需求,导致数据截断或插入失败。为了避免这种问题,了解不同文本类型(如TEXT、M…...
[ISP] AE 自动曝光
相机通过不同曝光参数(档位快门时间 x 感光度 x 光圈大小)控制进光量来完成恰当的曝光。 自动曝光流程大概分为三部分: 1. 测光:点测光、中心测光、全局测光等;通过调整曝光档位使sensor曝光在合理的阈值内࿰…...
小程序画带圆角的圆形进度条
老的API <canvas id"{{canvasId}}" canvas-id"{{canvasId}}" style"opacity: 0;" class"canvas"/> startDraw() {const { canvasId } this.dataconst query this.createSelectorQuery()query.select(#${canvasId}).bounding…...
16. LangChain实战项目2——易速鲜花内部问答系统
需求简介 易束鲜花企业内部知识库如下: 本实战项目设计一个内部问答系统,基于这些内部知识,回答内部员工的提问。 在前面课程的基础上,需要安装的依赖包如下: pip install docx2txt pip install qdrant-client pip i…...
代码的解读——自用
代码来自:https://github.com/ChuHan89/WSSS-Tissue?tabreadme-ov-file 借助了一些人工智能 run_pipeline.sh 功能总结 该脚本用于执行一个 弱监督语义分割(WSSS) 的完整流程,包含三个阶段: Stage1:训…...
蓝桥杯试题:DFS回溯
一、题目要求 输入一个数组n,输出1到n的全排列 二、代码展示 import java.util.*;public class ikun {static List<List<Integer>> list new ArrayList<>();public static void main(String[] args) { Scanner sc new Scanner(System.in);…...
FPGA开发,使用Deepseek V3还是R1(8):FPGA的全流程(简略版)
以下都是Deepseek生成的答案 FPGA开发,使用Deepseek V3还是R1(1):应用场景 FPGA开发,使用Deepseek V3还是R1(2):V3和R1的区别 FPGA开发,使用Deepseek V3还是R1&#x…...
一个py文件搞定mysql查询+Json转换+表数据提取+根据数据条件生成excel文件+打包运行一条龙
import os import argparse import pymssql import json import pandas as pd from datetime import datetime from pandas.io.formats.excel import ExcelFormatter import openpyxl# 投注类型映射字典 BET_MAPPING {1: WIN, 2: PLA, 3: QIN, 4: QPL,5: DBL, 6: TCE, 7: QTT,…...
微服务学习(1):RabbitMQ的安装与简单应用
目录 RabbitMQ是什么 为什么要使用RabbitMQ RabbitMQ的安装 RabbitMQ架构及其对应概念 队列的主要作用 交换机的主要作用 RabbitMQ的应用 通过控制面板操作(实现收发消息) RabbitMQ是什么 RabbitMQ是一个开源的消息队列软件(消息代理…...
【RAG】Embeding 和 Rerank学习笔记
Q: 现在主流Embeding模型架构 在RAG(Retrieval-Augmented Generation)系统中,嵌入模型(Embedding Model) 是检索阶段的核心组件,负责将查询(Query)和文档(Document&#…...
【Delphi】如何解决使用webView2时主界面置顶,而导致网页选择文件对话框被覆盖问题
一、问题描述: 在Delphi 中使用WebView2控件,如果预先把主界面置顶(Self.FormStyle : fsStayOnTop;),此时,如果在Web页面中有使用(<input type"file" id"fileInput" acc…...
【量化金融自学笔记】--开篇.基本术语及学习路径建议
在当今这个信息爆炸的时代,金融领域正经历着一场前所未有的变革。传统的金融分析方法逐渐被更加科学、精准的量化技术所取代。量化金融,这个曾经高不可攀的领域,如今正逐渐走进大众的视野。它将数学、统计学、计算机科学与金融学深度融合&…...
iOS 使用消息转发机制实现多代理功能
在iOS开发中,我们有时候会用到多代理功能,比如我们列表的埋点事件,需要我们在列表的某个特定的时机进行埋点上报,我们当然可以用最常见的做法,就是设置代理实现代理方法,然后在对应的代理方法里面进行上报&…...
16.3 LangChain Runnable 协议精要:构建高效大模型应用的核心基石
LangChain Runnable 协议精要:构建高效大模型应用的核心基石 关键词:LCEL Runnable 协议、LangChain 链式开发、自定义组件集成、流式处理优化、生产级应用设计 1. Runnable 协议设计哲学与核心接口 1.1 协议定义与类结构 #mermaid-svg-PlmvpSDrEUrUGv2p {font-family:&quo…...
Starrocks入门(二)
1、背景:考虑到Starrocks入门这篇文章,安装的是3.0.1版本的SR,参考:Starrocks入门-CSDN博客 但是官网的文档,没有对应3.0.x版本的资料,却有3.2或者3.3或者3.4或者3.1或者2.5版本的资料,不要用较…...
【北京迅为】itop-3568 开发板openharmony鸿蒙烧写及测试-第1章 体验OpenHarmony—烧写镜像
瑞芯微RK3568芯片是一款定位中高端的通用型SOC,采用22nm制程工艺,搭载一颗四核Cortex-A55处理器和Mali G52 2EE 图形处理器。RK3568 支持4K 解码和 1080P 编码,支持SATA/PCIE/USB3.0 外围接口。RK3568内置独立NPU,可用于轻量级人工…...
Electron一小时快速上手
1. 什么是 Electron? Electron 是一个跨平台桌面应用开发框架,开发者可以使用 HTML、CSS、JavaScript 等 Web 技术来构建桌面应用程序。它的本质是结合了 Chromium 和 Node.js,现在广泛用于桌面应用程序开发。例如,以下桌面应用都使用了 El…...
算法004——盛最多水的容器
力扣——盛最多水的容器点击即可跳转 当我们选择1号线和8号线时,下标为 1 和 8 形成容器的容积的高度是由 较矮的决定的,即下标为 8 的位置; 而宽度则是 1到8 之间的距离,为 8-17,此时容器的容积为 7 * 7 49。 当我…...
Java Web-Filter
Filter 在 Java Web 开发中,Filter(过滤器)是 Servlet 规范中的一个重要组件,它可以对客户端与服务器之间的请求和响应进行预处理和后处理。以下从多个方面详细介绍 Java Web 中的 Filter: 一、概念和作用 概念&…...
LeetCode 热题100 438. 找到字符串中所有字母异位词
LeetCode 热题100 | 438. 找到字符串中所有字母异位词 大家好,今天我们来解决一道经典的算法题——找到字符串中所有字母异位词。这道题在 LeetCode 上被标记为中等难度,要求我们在字符串 s 中找到所有是 p 的异位词的子串,并返回这些子串的…...
DeepSeek-R1训练时采用的GRPO算法数学原理及算法过程浅析
先来简单看下PPO和GRPO的区别: PPO:通过奖励和一个“评判者”模型(critic 模型)评估每个行为的“好坏”(价值),然后小步调整策略,确保改进稳定。 GRPO:通过让模型自己生…...
Qt基于信号量QSemaphore实现的生产者消费者模型
在 Qt 中,信号量(QSemaphore)是一种用于控制对共享资源访问的同步工具。它允许一定数量的线程同时访问共享资源,适合用于生产者-消费者模型。 代码实现 #include <QCoreApplication> #include <QThread> #include &…...
七星棋牌 6 端 200 子游戏全开源修复版源码(乐豆 + 防沉迷 + 比赛场 + 控制)
七星棋牌源码 是一款运营级的棋牌产品,覆盖 湖南、湖北、山西、江苏、贵州 等 6 大省区,支持 安卓、iOS 双端,并且 全开源。这个版本是 修复优化后的二开版本,新增了 乐豆系统、比赛场模式、防沉迷机制、AI 智能控制 等功能&#…...
CSDN博客导出设置介绍
在CSDN编辑博客时,如果想导出保存到本地,可以选择导出为Markdown或者HTML格式。其中导出为HTML时有这几种选项:jekyll site,plain html,plain text,styled html,styled html with toc。分别是什…...
_ 为什么在python中可以当变量名
在 Python 中,_(下划线)是一个有效的变量名,这主要源于 Python 的命名规则和一些特殊的使用场景。以下是为什么 _ 可以作为变量名的原因和常见用途: --- ### 1. **Python 的命名规则** Python 允许使用字母ÿ…...
使用haproxy实现MySQL服务器负载均衡
一、环境准备 主机名IP地址备注openEuler-1192.168.121.11mysql-server-1openEuler-2192.168.121.12mysql-server-2openEuler-3192.168.121.13clientRocky-1192.168.121.51haproxy 二、mysql-server配置 [rootopenEuler-1 ~]# yum install -y mariadb-server [rootopenEuler…...
音视频-WAV格式
1. WAV格式说明: 2. 格式说明: chunkId:通常是 “RIFF” 四个字节,用于标识文件类型。(wav文件格式表示)chunkSize:表示整个文件除了chunkId和chunkSize这 8 个字节外的其余部分的大小。Forma…...
apload-lab打靶场
1.提示显示所以关闭js 上传<?php phpinfo(); ?>的png形式 抓包,将png改为php 然后放包上传成功 2.提示说检查数据类型 抓包 将数据类型改成 image/jpeg 上传成功 3.提示 可以用phtml,php5,php3 4.先上传.htaccess文件࿰…...
通用查询类接口数据更新的另类实现
文章目录 一、简要概述二、java工程实现1. 定义main方法2. 测试运行3. 源码放送 一、简要概述 我们在通用查询类接口开发的另类思路中,关于接口数据的更新,提出了两种方案: 文件监听 #mermaid-svg-oJQjD6jQ8T19XlHA {font-family:"tre…...
