蓝桥备赛(六)- C/C++输入输出

devtools/2025/3/4 8:34:15/

一、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 。


http://www.ppmy.cn/devtools/164411.html

相关文章

Minio搭建并在SpringBoot中使用完成用户头像的上传

Minio使用搭建并上传用户头像到服务器操作,学习笔记 Minio介绍 minio官网 MinIO是一个开源的分布式对象存储服务器&#xff0c;支持S3协议并且可以在多节点上实现数据的高可用和容错。它采用Go语言开发&#xff0c;拥有轻量级、高性能、易部署等特点&#xff0c;并且可以自由…

工程化与框架系列(6)--前端模块化工程详解

前端模块化工程详解 &#x1f9e9; 前端模块化是现代Web开发的核心理念之一&#xff0c;它帮助我们组织和管理日益复杂的前端代码。本文将详细探讨前端模块化工程的各个方面&#xff0c;从基础概念到实际应用。 模块化概述 &#x1f31f; &#x1f4a1; 小知识&#xff1a;模…

每日一题——接雨水

接雨水问题详解 问题描述 给定一个非负整数数组 height&#xff0c;表示每个宽度为 1 的柱子的高度图。计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&#xff1a;6 解释&#…

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_str_rbtree_insert_value

ngx_str_rbtree_insert_value 声明 在 src\core\ngx_string.h void ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); ngx_str_node_t *ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *name,uint32_t …

PostgreSQL的基本使用

参考视频&#xff1a;零基础入门PostgreSQL教程 文章目录 一、PostgreSQL是什么&#xff1f;二、基本使用1.下载2.操作 一、PostgreSQL是什么&#xff1f; PostgreSQL 是一个免费的对象-关系数据库服务器&#xff0c;在灵活的BSD许可证下发行。 二、基本使用 1.下载 2.操作 …

在 Ubuntu 下通过 Docker 部署 Mastodon 服务器

引言 大家好&#xff0c;我是Hitch。今天咱们来聊聊如何在 Ubuntu 系统上通过 Docker 部署 Mastodon 服务器。Mastodon 是一个开源的社交网络平台&#xff0c;像 Twitter 但更自由。Docker 是一个强大的容器化工具&#xff0c;可以让我们轻松地打包和部署应用。接下来&#xf…

在已有的原生 App 里嵌入 Flutter 页面的方法

在原生 App 中嵌入 Flutter 页面&#xff0c;通常使用 Flutter 提供的**平台通道&#xff08;Platform Channels&#xff09;**来实现原生代码与 Flutter 之间的交互。具体实现方式依赖于原生 App 的平台&#xff08;如 Android 或 iOS&#xff09;&#xff0c;下面是两种常见的…

基于POI的Excel下拉框自动搜索,包括数据验证的单列删除

目录 目标 例子 1.搜索下拉框页 2.数据源页 3.效果 代码以及注意事项 1.代码 2.注意事项 1.基于Excel的话&#xff0c;相当于加入了一个【数据验证】 2.代码中的一些方法说明 目标 期望在Excel利用代码创建具备自动搜索功能的下拉框 例子 1.搜索下拉框页 2.数据源…