C++需要用到的基础东西
- 模板(采用万能头文件):
- 直接抄就是了,学校OJ是不会卡万能头文件的
#include <bits/stdc++.h>//万能头文件 using namespace std; int main() { return 0; }
- 输入输出:
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b;//用cin >> 来代替scanf cout << a << b << endl;//用cout << 来 代替printf,另外使用cout << endl来打回车 return 0; }
- 排序(采用sort):
#include <bits/stdc++.h> using namespace std; int main() { int a[100] = {3, 2, 1}; sort(a, a + 3);//有多少个元素就+多少。 for(int i = 0; i < 3; i++) cout << a[i] << " "; cout << endl; return 0; }
- 还有一些STL的使用,比如:vector, queue, map, set等,需要的时候自己去搜。
- 代码:
-
#include <bits/stdc++.h> using namespace std; bool cmp(int x, int y) { return x > y; } int main() { string a; cin >> a; cout << "输入的字符串是 : " << a << endl; cout << "它的长度是 : " << a.size() << endl; cout << "它的每一位是 : "; for(int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; cout << "-------------------" << endl; double x; cin >> x; cout << "输入的浮点数是 : " << x << endl; cout << "四舍五入后保留两位小数为 : " << fixed << setprecision(2) << x << endl; cout << "输入想要保留的小数位数k为 :"; int k; cin >> k; cout << "四舍五入后保留k位小数为 : " << fixed << setprecision(k) << x << endl; cout << "-------------------" << endl; int m, n; cin >> m >> n; cout << "他们的最大公约数为 : " << __gcd(m, n) << endl; cout << "他们的最小公倍数为 :" << m * n / __gcd(m, n) << endl; cout << "-------------------" << endl; int num[] = {123,321,876,125}; cout << "原数组为 : "; for(int i = 0; i < 4; i++) { cout << num[i] << " "; } cout << endl; sort(num + 0, num + 4); cout << "从小到大排序为 : "; for(int i = 0; i < 4; i++) { cout << num[i] << " "; } cout << endl; sort(num + 0, num + 4, cmp); cout << "从大到小排序为 : "; for(int i = 0; i < 4; i++) { cout << num[i] << " "; } cout << endl; cout << endl << "---------完---------" << endl; return 0; }