文中引用了参考资料中的部分内容,本文参考资料详见文末“参考资料”一节,感谢资料分享者。

语言:c++。

  • 如果忘记了想添加的头文件怎么写,可以使用c++通用头文件库:<bits/stdc++.h>,其中包含了c++所有头文件(降低编译速度)。
  • 平台指明了每个语言用的编译器版本,如c++(clang++11)
  • 问:编程题最后一分钟从本地IDE复制到网页中了,但是没有点“保存并调试”就因为时间到了自动交卷了(就差一秒真的很难受),请问这种情况还会有内容提交上去吗?答:会自动提交的
  • 写不出来试一试暴力解法,或者猜一下。
  • 某些公司的考试注意事项:考试过程中除了编程题允许切换跳出页面,考试过程中,编程题可以本地IDE。准备本地的IDE

输入输出

  • 对于传统ACM的OJ模式题目,你的程序需要从标准输入读取数据,然后打印到标准输出上。

  • 开辟数组空间最好根据题目的数据范围要求来,这里可能会造成runtime error,如果代码中递归的深度太深也可能爆栈空间导致 runtime error。

  • 关于cin cout和scanf printf。做题的时候尽量使用scanf printf。在数据量比较大的情况下cin cout比scanf printf慢挺多。一旦遇到大数据量,光是读入就有可能跪掉。你或许可以使用std::ios::sync_with_stdio(false); 这条语句关掉scanf和cin的同步,加快效率。但是即使这样cin还要慢,而且一旦使用了这条语句,scanf和cin混用可能就会造成一些奇怪的错误。

循环输入输出处理

1、一般需要循环输入输出:通常来说OJ对于每道题里面有.in和.out文件,分别表示测试数据的输入和输出。如果某些编程题的所有数据都只做在一个.in和一个.out中,这样就会变成多组测试了,所以需要提交的代码中循环处理。

2、如果测试数据是多组的,但是恰巧你代码里面需要些标记数组,map,set等,在循环内一定记得清空,不然可能会产生前面的测试样例影响了后续数据的答案。

关于输出格式

1、行末空格:比如我输出需要打印多个数需要使用空格分隔的时候,我们循环使用printf(“%d “,x);这种会很方便,但是这样会导致行末多一个空格,后台系统会严格比对你的输出和.out文件,这样也会被判错误

2、换行问题,对于每个样例,建议输出完全之后都换行一下。对于一些题目,可能就是不换行就导致了后面输入数据错位,那就肯定不可能过了。

C++ cin/cout

键盘上的输入会先存放在缓存区中。cin会从缓存区中读出数据。读出后若缓存仍有数据,下一次的cin不会等待去请求键盘输入而是直接读缓存区残留的内容。

  • cin可将读取的数据存储到char, int, string中。结束条件:遇到空格,回车。cin从第一个不是结束条件的地方开始读,直到遇到结束条件。对结束符的处理:丢弃。
1
2
3
4
5
6
//输入“  1”
int a; cin >> a; //a的值为1
//输入
//"a
// d c"
string a,b,c; cin>>a>>b>>c; // a = "a", b = "d", c = "c";
  • 检测输入结束,文件结束标志EOF(End Of File):

std::cin.eof() tests for end-of-file(hence eof), not for errors. For error checking use !std::cin.good(), the built-in conversion operator (if(std::cin)) or the boolean negation operator (if(!std::cin)).

1
2
while(!cin.efo()){}

多练习语言的不同的数据格式的读取与输出的方法。

C printf

C/C++ %s %d %u 基本概念与用法

1
2
3
4
%d              十进制有符号整数 
%f 浮点数
%s 字符串
%c 单个字符

可以在”%”和字母之间插进数字表示最大场宽。

%3d 表示输出3位整型数, 不够3位右对齐。
%9.2f 表示输出场宽为9的浮点数, 其中小数位为2, 整数位为6, 小数点占一位, 不够9位右对齐。
%8s 表示输出8个字符的字符串, 不够8个字符右对齐。

printf("%.2f%c", 1/100.0, i==N ? '\n' : ' ');

printf("No solution\n")

OJ在线编程常见输入输出练习

两数相加 1 输入描述:

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。

输出描述:

输出a+b的结果

输入

1 5
10 20

输出

6
30

  • 使用一个istream对象作为条件时,其效果是检测流的状态。当遇到文件结束符(end-of-file),或遇到一个无效输入时,istream对象的状态会变为无效。while会一直执行直到遇到条件结束符(或输入错误)。

代码:

#include <iostream>
using namespace std;
int main() {
    int a,b; while(cin >> a >> b)//注意while处理多个case
        cout << a+b << endl;
}
两数相加 5

输入描述:

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输入

2
4 1 2 3 4
5 1 2 3 4 5

输出

10
15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
int n{};
cin >> n;
while(cin >> n){
int t{n}, sum{}, a{};
while(t--){
cin >> a;
sum += a;
}
cout << sum << endl;
}
}
字符串排序(2) | cin.get()的使用

输入描述:

多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100

输入

a c bb
f dddd
nowcoder

输出

a bb c
dddd f
nowcoder

  • cin.get() 用于读取字符,会返回读到的空格与回车。会消费掉一个字符
1
2
//输入”  1“;
string a = cin.get(); // a的值为” “
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<string> vtc;
string elem;
while(cin >> elem){
vtc.push_back(elem);
if(cin.get() == '\n'){
sort(vtc.begin(), vtc.end());
for(const string &i:vtc){
cout << i<< " ";
}
cout << endl; //注意,要加上endl
vtc.clear();//注意,要清vector
}
}
}
字符串排序(3) | getline的使用

输入

a,c,bb
f,dddd
nowcoder

输出

a,bb,c
dddd,f
nowcoder

  • stringstream:stream class to operate on strings.
1
2
3
#include <sstream> //istringstream
string line; cin >> line;
istringstream inPut(line);

参考:

What’s the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

std::stringstream | cplusplus.com

1
istream& getline (istream&  is, string& str, char delim);

Extracts characters from is and stores them into str until the delimitation character delim is found。遇到delim,会将它extracted and discarded。

Note that any content in str before the call is replaced by the newly extracted sequence.

参考:std::getline (string) | cplusplus

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <sstream> //iostringstream
#include <vector>
#include <string> //getline
#include <algorithm>

using namespace std;

int main(){
string str;
while(cin>>str){
istringstream inPut(str);
vector<string> vtc;
while(getline(inPut, str, ',')){
vtc.push_back(str);
}
sort(vtc.begin(), vtc.end());
for(int i{}; i<vtc.size()-1;++i){
cout << vtc[i] << ',';
}
cout << *(--vtc.end());
cout <<endl;
vtc.clear();
}
}
getline的使用(2)
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <utility> //move
using namespace std;
int main(){
string tmp;
vector<string> vtc;
getline(cin, tmp);
vtc.push_back(move(tmp))
}

打印数组-如何让最后一个元素跟着'\n',其它元素跟着' '空格

题目描述:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。 现在要求输出所有在m和n范围内的水仙花数。

输入描述:输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

输出描述:从小到大排列在一行内输出,之间用一个空格隔开; 如果给定的范围内不存在水仙花数,则输出no; 每个测试实例的输出占一行。

样例输入:

100 120
300 380

样例输出:

no
370 371

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int main(){
int n, m;
while(cin >> n >> m){
vector<int> vtc;
for(int i = n; i<=m; ++i){
int t = i, ret = 0;
while(t){
ret += pow(t%10, 3);
t /=10;
}
if(i == ret){
vtc.push_back(i);
}
}
if(vtc.empty()) cout << "no" << endl;
else{
for(int i = 0; i<vtc.size()-1; ++i){
cout << vtc[i] << ' ';
}
cout << *(vtc.rbegin()) << endl;
}

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>

int main(){
int m,n;
while(scanf("%d%d",&m,&n)!=EOF){
int t=0;
for(int i=m; i<=n; i++){
int a=i/100;
int b=i%100/10;
int c=i%10;

if(i==a*a*a+b*b*b+c*c*c && t==0){
printf("%d ",i);
t++;
}
else if(i==a*a*a+b*b*b+c*c*c && t==1){
printf("%d ",i);
}
}
if(t!=0){ printf("\n"); }
if(t==0){ printf("no\n"); }
}
return 0;
}
处理带中括号的

输入示例

[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]

重定向标准输入输出(c++)

How to redirect cin and cout to files?

使用C++标准库中的std::fstream 而不是C库中的freopen。

Here is a short code snippet for shadowing cin/cout useful for programming contests:

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>

using namespace std;

int main() {
ifstream in("input.txt");
ofstream out("output.txt");

int a, b;
in >> a >> b;
out << a + b << endl;
}

This gives additional benefit that plain fstreams are faster than synced stdio streams. But this works only for the scope of single function.

Global cin/cout redirect can be written as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>

void f()
{
std::string line;
while(std::getline(std::cin, line)) //input from the file in.txt
{
std::cout << line << "\n"; //output to the file out.txt
}
}
int main()
{
std::ifstream in("in.txt");
std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
std::cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!

std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!

std::string word;
std::cin >> word; //input from the file in.txt
std::cout << word << " "; //output to the file out.txt

f(); //call function


std::cin.rdbuf(cinbuf); //reset to standard input again
std::cout.rdbuf(coutbuf); //reset to standard output again

std::cin >> word; //input from the standard input
std::cout << word; //output to the standard input
}

Std io streams can also be easily shadowed for the scope of single file, which is useful for competitive programming:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>

using std::endl;

std::ifstream cin("input.txt");
std::ofstream cout("output.txt");

int a, b;

void read() {
cin >> a >> b;
}

void write() {
cout << a + b << endl;
}

int main() {
read();
write();
}

练习:

OnlineJudge-OJ输入输出基础必练0813新增三题

【已完结】编程语言初学练习赛-总赛场

c++工程师历年企业笔试真题汇总

你应该找不到更全的各岗位(工程师,产品运营职能等)考题集合了

https://blog.csdn.net/newson92/article/details/114979396

https://blog.csdn.net/haiki66/article/details/107748410

https://www.zhihu.com/question/326625230

https://zhuanlan.zhihu.com/p/366109852

https://zhuanlan.zhihu.com/p/185806858

https://www.nowcoder.com/courses/7/1/1

https://zhuanlan.zhihu.com/p/228305263

参考资料

  1. (经验贴)那些年在编程题中踩过的坑
  2. C++输入cin详解
  3. C++输入cin详解