牛客 acm输入输出模式练习
https://www.nowcoder.com/exam/test/67432019/detail?pid=27976983#question
注意:只有部分个人觉得有意义的题目
- A+B(4)
计算一系列数的和
打开以下链接可以查看正确的代码
https:``//ac.nowcoder.com/acm/contest/5657#question
数据范围:数据组数满足 1≤�≤100 1≤t≤100 ,每组数据中整数个数满足 1≤�≤100 1≤n≤100 ,每组数据中的值满足 1≤���≤100 1≤val≤100
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
输入描述:
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例1
输入例子:
4 1 2 3 4
5 1 2 3 4 5
0
输出例子:
10
15
个人分析: 难点在于一行的个数是未知的,因此首先需要分行。
分行用到的函数或者结构有:getline,stringstream
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string line;
while (getline(cin,line)) {
stringstream ss;
ss << line;
long long res = 0;
int n = 0;
ss >> n;
if (n == 0) {
break;
}
else {
while (n--) {
int tmp; ss >> tmp;
res += tmp;
}
}
cout << res << endl;;
}
}
// 64 位输出请用 printf("%lld")
A+B(6)
计算一系列数的和
打开以下链接可以查看正确的代码
https:``//ac.nowcoder.com/acm/contest/5657#question
数据范围: 1≤�≤1000 1≤n≤1000 , 所有数都满足 1≤���≤1000 1≤val≤1000
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例1
输入例子:
4 1 2 3 4
5 1 2 3 4 5
输出例子:
10
15
难点: 难点在于不知道有多少行。
解决:判断line是否为空即可。
getline函数会自己判断是否为空(和cin类似),因此直接将getline写在while中即可。
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string line;
while (getline(cin,line)) {
stringstream ss;
ss << line;
int n; ss >> n;
int res = 0;
while (n--) {
int t = 0; ss >> t;
res += t;
}
cout << res << endl;
}
}
// 64 位输出请用 printf("%lld")
字符串排序(1)
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https:``//ac.nowcoder.com/acm/contest/5657#question
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
输入描述:
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
示例1
输入例子:
5
c d a bb e
输出例子:
a bb c d e
难点:
- c++自带的string
compare函数的比较规则 - 如何自定义比较规则。
解答:
- c++自带的string compare函数的比较规则
compare()比较时逐字符比较的,一旦能比较出结果,就不再比较了。 例如“abc”和“adf”,首先a和a比较,比不出结果;则b和d比较,结果就是“abc”小于“adf”,返回-1,即字典里“abc”在“adf”前面。例如“abc”和“abcd”比较,若“abc”都比完了,“abcd”还没完,说明“abc”小,返回值为-1,字典里“abc”靠前。总之记住这个比较规则和字典顺序一致即可。
- 自定义比较规则
见代码
使用的是lamda表达式
sort(vt.begin(), vt.end(), [](string a, string b)->bool {
int i = 0; int j = 0;
while (a[i] == b[j]) {
i++; j++;
if (i == a.size()) { return true; }
if (j == b.size()) { return false; }
}
return a[i] < b[j];
});
本题运行代码:
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n; cin >> n;
vector<string> vt;
while (n--) {
string tmp; cin >> tmp;
vt.emplace_back(tmp);
}
sort(vt.begin(), vt.end(), [](string a, string b)->bool {
return a.compare(b) < 0;
});
for (auto item : vt) {
cout << item << " ";
}
}
// 64 位输出请用 printf("%lld")
9.字符串排序(2)
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string line;
while (getline(cin, line)) {
stringstream ss;
ss << line;
string tmp;
vector<string> vt;
while (ss >> tmp) {
vt.emplace_back(tmp);
}
sort(vt.begin(), vt.end(), [](string a, string b)->bool {
return a.compare(b) < 0;
});
for (auto& item : vt) {
cout << item << " ";
}cout << endl;
}
}
// 64 位输出请用 printf("%lld")
字符串排序(3)
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https:``//ac.nowcoder.com/acm/contest/5657#question
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述:
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
示例1
输入例子:
a,c,bb
f,dddd
nowcoder
输出例子:
a,bb,c
dddd,f
nowcoder
难点:分割符不是常使用的 空格 、tab等如何操作
getline函数可以指定分隔符,默认是以\n为分割符的。
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string line;
while (getline(cin, line)) {
stringstream ss;
ss << line;
vector<string> vt;
string tmp;
while (getline(ss, tmp, ',')) {
vt.emplace_back(tmp);
}
sort(vt.begin(), vt.end(), [](string a, string b) ->bool {
return a.compare(b) < 0;
});
string show;
for (auto& item : vt) {
show.append(item); show.push_back(',');
}show.pop_back();
cout <<show<< endl;
}
}
// 64 位输出请用 printf("%lld")
11.自测本地通过提交为0
#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
}
// 64 位输出请用 printf("%lld")
牛客 acm输入输出模式练习的更多相关文章
- 牛客ACM赛 B [小a的旅行计划 ]
链接 B 小a的旅行计划 把\(n\)个数中选任意数分成\(a,b\)两个集合,集合无区别,要求不包含且有交,求方案数.\(n\leq 10^{13}\) 首先讨论\(a,b\)并集是否为全集: 若是 ...
- 牛客ACM赛 C 区区区间间间
链接 C 区区区间间间 给定长度为\(n\)序列,求\[\sum_{i=1}^{n} \sum_{j=i}^{n} max-min\] 其中\(max\),\(min\)为区间最大,最小值,\(n\l ...
- 8.30 牛客OI赛制测试赛1 F题 子序列
题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T,表示数据组数.对于每组数据,第一行两个整数N,k,含义如题所 ...
- 平衡二叉树 (牛客国庆day2)解锁二叉树打表姿势&&找规律套路
链接:https://www.nowcoder.com/acm/contest/202/F来源:牛客网 平衡二叉树,顾名思义就是一棵“平衡”的二叉树.在这道题中,“平衡”的定义为,对于树中任意一个节点 ...
- 算法题 21 findNSum (好未来,LeetCode,牛客网)
一.三数之和:LeetCode 15 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 G.路径-带条件的树的直径变形-边权最大,边数偶数的树上的最长路径-树形dp
链接:https://ac.nowcoder.com/acm/contest/558/G 来源:牛客网 路径 小猫在研究树. 小猫在研究路径. 给定一棵N个点的树,每条边有边权,请你求出最长的一条路径 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)
链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)
链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题
链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...
随机推荐
- 2023NOIP A层联测16 T3 货物运输
2023NOIP A层联测16 T3 货物运输 题目描述说这是一个仙人掌图,通常将问题转换为环和树的问题在使用圆方树来解决. 树解法 令 \(a_i=s_i-\frac{\sum s_i}{n}\) ...
- 总结:OI题目中常见的三种距离
设 \(A(x_1, y_1), B(x_2, y_2)\). 欧几里得距离 \[|AB| = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \] 一般模型:在一个坐标系上 ...
- php使用汉字作为进制转换
突然想到英文字符26个,大小写共52个,数字10个,加起来也不过62,再算上特殊字符,也就90个,可以看这篇文章 那可不可以扩大这个进制呢?我想到了汉字. 中文汉字,博大精深,我们就用常用汉字2500 ...
- MagicQuill,AI动态图像元素修改,AI绘图,需要40G的本地硬盘空间,12G显存可玩,Win11本地部署
最近由 magic-quill 团队开源的 MagicQuill 项目十分引人瞩目,这个项目可以通过定制的 gradio 客户端针对不同的图像元素通过提示词进行修改,从而生成新的图像.值得一提的是,这 ...
- 读书笔记-C#8.0本质论-02
10. 接口 10.1 显式与隐式接口实现的比较 10.1.1 隐式接口 namespace ConsoleApp1; internal static class Program { internal ...
- VScode之远程开发
之前使用过PyCharm的远程开发,很好用,不过还是有几个局限性: 只能用于Python语言: 本地和服务器都需要有一份代码,这两份代码是完全同步的: 一.配置免密远程登录 1.首先检查本地是否有已生 ...
- 【Amadeus原创】wordpress批量更新图片路径的方法
连上wordpress数据库,怼一句: UPDATE wp_posts SET post_content = REPLACE( post_content, '旧域名', '新域名' );
- 【数据结构】【冒泡排序法】Java写冒泡排序法
public class 冒泡 { public static int[] maopao(int[] arr){ for(int i=0;i<arr.length-1;i++){ for(int ...
- liquibase maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- getway网关跨域问题记录
一.问题产生环境 1.1 为什么会产生跨域问题? 跨域不一定都会有跨域题. 因为跨域问题是浏览器对于ajax请求的一种安全限制: 一个页面发起的 ajax请求,只能是与当前页域名相同的路径,这能有效的 ...