必备头文件

1
2
#include <bits/stdc++.h>
using namespace std;

可以一次性解决大部分头文件记不住的问题。
同时也存在运行时间增长的问题,但一般不做考虑
其文件内容如下

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

struct

可以灵活构建自己的struct,比如pair类型的数据,python都有定义,之前用c++都是使用双数组来存储,很不方便,容易超内存。

swap

在交换struct类型的数据时如果要保持交换效果到方法外,需要引用输入

1
2
3
4
5
6
7
8
9
10
11
12
void swap1(zuobiao &a, zuobiao &b){
int q,w,e;
q = a.x;
w = a.y;
e = a.n;
a.x = b.x;
a.y = b.y;
a.n = b.n;
b.x = q;
b.y = w;
b.n = e;
}

Debug

devC++默认是不开启debug功能的,一般使用也是轻量级的编译运行,但是ccf常常出现逻辑出错的问题,因此需要学会如何开启devC++的的不过功能,记录如下:

如何打开调试

1
2
3
4
1、点击菜单栏中的工具[T]
2、选择编译选项[C]
3、更改调试信息
点击【代码生成/优化】,将【连接器】中【产生调试信息】一项的No改为Yes,然后点击下方的确定即可。

如何打开鼠标查看变量

1
2
3
4
1、点击菜单栏中的工具[T],选择环境选项[v]
2、更改信息
找到【浏览Debug变量】,选中【查看鼠标指向的变量】,然后点击下方的确定即可。
使用方法:当开始调试以后,把鼠标划过代码中想查看的变量就可以在左边调试一栏中查询该变量的当前值。

调试方法

1
2
3
4
5
6
7
1、打开一个.c或.cpp文件,编译它,一方面,要确保这个代码能够编译通过,另一方面,在每次调试前都是需要先编译的。
2、设置“断点”
在要设置断点的那行代码开头处的数字上单击一下即可,可以设置多个断点,也可以只设置一个断点。如果设置了多个断点,程序会在断点与断点之间进行调试。如果只有一个断点,程序会从设置断点处开始,随着每次点击,一步一步进行下去,直到程序结束。
3、点击“调试”,开始进入调试
点击菜单栏中的【√】或者下方的【调试】都可以
4、如果程序需要输入,那么点击进入后,就会跳出黑框。输入数据,按“回车”。蓝色一行表示当前程序运行的位置。
5、调试开始后,可以点击调试一栏中的‘下一步’,让程序运行到想要的位置。

优化cin,加快读入速度

在代码中插入这段代码:

1
std::ios::sync_with_stdio(false);

很多人会说cin的速度比scanf慢很多, 其实不然, 其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用。而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱。
正因为这个兼容性的特性,导致cin有许多额外的开销。
只需要加上std::ios::sync_with_stdio(false)来关闭同步就好了, 速度甚至要优于scanf。
注意,这两个代码的头文件是 iostream
并且如果用了这两个,就不要用scanf ,getchar,gets,fgets,fscanf了。
好处:将cin cout速度提升至与scanf相差无几。
副作用:不能scanf cin之类的混用了。

cout的奇妙输出

如何输出两位小数

1
2
3
4
5
6
float a = 0.20001;
cout.setf(ios::fixed);
cout << "a=" <<fixed<< setprecision(2) << a <<endl; //输出结果为a=0.20
cout.unsetf(ios::fixed);
cout << "a=" << setprecision(2) << a <<endl; //输出结果为a=0.2
return 0;

为什么总是第二题60分呢?

原因1

数字大小超过了int类型的限制

原因2

使用了错误的算法,如双层for导致超时,考虑一下代码优化