https://www.nowcoder.com/exam/test/67432019/detail?pid=27976983#question

注意:只有部分个人觉得有意义的题目

  1. 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

个人分析: 难点在于一行的个数是未知的,因此首先需要分行。

分行用到的函数或者结构有:getlinestringstream

#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

难点:

  1. c++自带的string compare函数的比较规则
  2. 如何自定义比较规则。

解答:

  1. c++自带的string compare函数的比较规则

compare()比较时逐字符比较的,一旦能比较出结果,就不再比较了。 例如“abc”和“adf”,首先a和a比较,比不出结果;则b和d比较,结果就是“abc”小于“adf”,返回-1,即字典里“abc”在“adf”前面。例如“abc”和“abcd”比较,若“abc”都比完了,“abcd”还没完,说明“abc”小,返回值为-1,字典里“abc”靠前。总之记住这个比较规则和字典顺序一致即可。

  1. 自定义比较规则

见代码

使用的是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输入输出模式练习的更多相关文章

  1. 牛客ACM赛 B [小a的旅行计划 ]

    链接 B 小a的旅行计划 把\(n\)个数中选任意数分成\(a,b\)两个集合,集合无区别,要求不包含且有交,求方案数.\(n\leq 10^{13}\) 首先讨论\(a,b\)并集是否为全集: 若是 ...

  2. 牛客ACM赛 C 区区区间间间

    链接 C 区区区间间间 给定长度为\(n\)序列,求\[\sum_{i=1}^{n} \sum_{j=i}^{n} max-min\] 其中\(max\),\(min\)为区间最大,最小值,\(n\l ...

  3. 8.30 牛客OI赛制测试赛1 F题 子序列

    题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T,表示数据组数.对于每组数据,第一行两个整数N,k,含义如题所 ...

  4. 平衡二叉树 (牛客国庆day2)解锁二叉树打表姿势&&找规律套路

    链接:https://www.nowcoder.com/acm/contest/202/F来源:牛客网 平衡二叉树,顾名思义就是一棵“平衡”的二叉树.在这道题中,“平衡”的定义为,对于树中任意一个节点 ...

  5. 算法题 21 findNSum (好未来,LeetCode,牛客网)

    一.三数之和:LeetCode 15 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. ...

  6. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  7. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 G.路径-带条件的树的直径变形-边权最大,边数偶数的树上的最长路径-树形dp

    链接:https://ac.nowcoder.com/acm/contest/558/G 来源:牛客网 路径 小猫在研究树. 小猫在研究路径. 给定一棵N个点的树,每条边有边权,请你求出最长的一条路径 ...

  8. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)

    链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...

  9. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)

    链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...

  10. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题

    链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...

随机推荐

  1. Java虚拟机类加载机制浅谈

    Java语言是一种编译后再经过解释器执行的过程, 解释器主要就是如何处理解释Class文件的二进制字节流.JVM主要包含三大核心部分:运行时数据区,类加载器和执行引擎. 虚拟机将描述类的数据从Clas ...

  2. PHP之JWT的token登录认证

    1.JWT简介 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该信息可以被验证和信任,因为 ...

  3. Vim之PHP语法检查

    在Linux下操作,一般都是使用vim进行文本编辑, 这个时候有可能不小心就会出现语法异常,导致程序错误 手动检查: 1) 编辑完成之后, 回到命令行下执行 php -l test.php 如果语法校 ...

  4. 【解决方案】Error running,Command line is too long

    一.现象 IDEA 提示 Error running,Command line is too long 二.原因 Java 命令行启动举例如下图,当命令行字符过多的时候,就会出现 Error runn ...

  5. cmu15545笔记-并发控制总结(Concurrency Control Summary)

    目录 总览 ACID 串行化与冲突操作 隔离级别 概念层级 二阶段锁 原理 级联回滚 强二阶段锁 死锁检测和避免 锁层级 实践应用 实现的隔离级别 OOC 原理 三个阶段 实现的隔离级别 处理幻读 M ...

  6. Excel使用IF{1,0}虚拟数组+VLOOKUP实现联合查询

    以此案例举例: 使用IF({1,0})建立虚拟数据的方法,整体输入的公式是: =VLOOKUP(E2&F2,IF({1,0},A:A&B:B,C:C),2,0) 输入完公式之后,需要按 ...

  7. tableau连接不上mysql或不显示mysql表的终极解决方法

    [报错一]连不上mysql An error occurred while communicating with MySQL The connection to the data source mig ...

  8. 解锁 Git Log 更多实用技巧

    目前,在软件开发的协作中,Git 无疑是版本控制的王者. 而其中的 git log 命令,犹如一把强大的历史探寻之剑,能够帮助我们深入洞察项目的演进历程. 本篇将为大家整理解读几个实用的 git Lo ...

  9. 黑苹果(Hackintosh) - 问题,修改CPU数量和内存数量后,系统重启失败

    1. 问题复现 安装完黑苹果后,内存默认的 1个处理器2个核心.2G内存,发现不够用. 于是,修改了 VMware 对此系统的 硬件配置 内存: 2G -> 8G 处理器:1个处理器 -> ...

  10. Qt/C++编写推流综合应用示例(文件推流/桌面推流/本地摄像头/网络摄像头/转发推流/视频分发)

    一.功能特点 1.1 文件推流 指定网卡和监听端口,接收网络请求推送音视频等各种文件. 实时统计显示每个文件对应的访问数量.总访问数量.不同IP地址访问数量. 可指定多种模式,0-直接播放.1-下载播 ...