• 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;
      }
      

        

  • 相关阅读:
    hdu 2586 How far away ?
    zoj 3195 Design the city
    hust 1022 K-diff subsequence
    poj 2253 Frogger
    poj 1470 Closest Common Ancestors
    poj 2553 The Bottom of a Graph
    poj 1236 Network of Schools
    poj 3694 Network
    uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
    2014年百度之星程序设计大赛
  • 原文地址:https://www.cnblogs.com/ACM-Epoch/p/13777993.html
Copyright © 2020-2023  润新知