ACM常識總結
阿新 • • 發佈:2019-02-11
因為要參加ACM比賽,所以將自己之前所遇到的,用到的,一些常見的錯誤,常用的函式都寫出來。
輸出超限
到遇到此問題時,不要著急,你的答案很可能是對的,但是輸出格式不對,或者可能是下面這種情況:
while ( true ){ cin >> n ; }
和
while (cin >> n) {}
是有很大區別的,本質上利用的是 IO緩衝的特點
在輸入沒有回車之前,程式的 printf 輸出不會顯示(ggc和vc也有區別)。
按標準的做法應該儲存輸入的所有字元直至回車,再一次轉換輸出。
執行出錯
在c++11中,unsigned 和 int 是有區別的
例:
for (unsigned i = 9; i >= 0; i--) {
cout << num[i]<<" ";
}
是一個死迴圈,因為,unsigned中沒有 -1
所以會一直執行下去,而下一位就是
(32位作業系統 16位 0 至 65,535 ,64位作業系統 32位 0至4294967295)最大位數65535.
JAVA 因為外部庫多,功能豐富,所以常在ACM中使用,其具體使用格式位:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
}
}
java常見的型別轉換
- 如何將字串 String 轉換成整數 int?
方法一:
int i = Integer.parseInt([String]);
i = Integer.parseInt([String],[int radix]);
方法二:
int i = Integer.valueOf (my_str).intValue();
- 注:
- 字串轉成 Double, Float, Long 的方法大同小異.
- 如何將整數 int 轉換成字串 String ?
方法一:
String s = String.valueOf(i);
方法二:
String s = Integer.toString(i);
方法三:
String s = "" + i;
以下函式是C++ STL中函式
- 設定輸出位數
#include <iomanip>
cout << setiosflags(ios::fixed) << setprecision(site)<< num << endl;
- 求絕對值
#include <iostream>
abs(a-b);
- 擷取字串-splite
#include <string>
const char *spilt_char="/";
char *p;
p.strtok(str,spilt_char);
- 排序 sort函式
#include<algorithm>
//預設排序
sort(Rs.begin(),Rs.end());
//模板排序
#include<vector>
sort<vector>(Rs.begin(),Rs.end(),compare);
7 關於排序
bool cmp(int a,int b)
{
return abs(a)>abs(b);
}
sort(vec.begin(),vec.end(),cmp);
8 求字串長度
strlen(str)
9//cin.getline(字元指標,字元個數N,結束符);
//結束符(預設的是以'\n'結束)
while(cin.getline(a,100))
10 字串比較 //strcmp(字串1,字串2)
//s1<s2 <0 ; s1=s2 0 ;s1>s2 >0
- string物件取地址
&str.at();
- string怎麼通過cin獲取一行
string str;
getline(cin, str);
cout << str << endl;
或
string str;
get(str,length); //按指定長度獲取
- string物件中可存放的最大字串的長度
int max_size();
- 返回當前字串的大小
int size();
- 返回當前字串的長度
int length();
- 當前字串是否為空
bool empty();
- 把字串當前大小置為len,並用字元c填充空白的部分
void resize(int len,char c);
string的比較:
-
比較兩個字串是否相等
- 運算子”>”,”<”,”>=”,”<=”,”!=”均被過載用於字串的比較;
bool operator==(const string &s1,const string &s2);
- 比較當前字串和s的大小
int compare(const string , &s) ;
- 比較當前字串從pos開始的n個字元組成的字串與s的大小
int compare(int pos, int n , const string &s)
- 比較當前字串從pos開始的n個字元組成的字串與s中pos2開始的n2個字元組成的字串的大小
int compare(int pos, int n , const string &s,int pos2 , int n2);
- compare函式在>時返回1,<時返回-1,==時返回0
int compare(const char *s);
int compare(int pos, int n,const char *s);
int compare(int pos, int n,const char *s, int pos2) const;
string的交換:
- 交換當前字串與s2的值
void swap(string &s2);
string類的查詢函式:
查詢成功時返回所在位置,失敗返回string::npos的值
- 從pos開始查詢字元c在當前字串的位置
int find(char c, int pos = 0);
- 從pos開始查詢字串s在當前串中的位置
int find(const char *s, int pos = 0);
- 從pos開始查詢字串s中前n個字元在當前串中的位置
int find(const char *s, int pos, int n) ;
- 從pos開始查詢字串s在當前串中的位置
int find(const string &s, int pos = 0) ;
反向查詢函式
從pos開始從後向前查詢字元c在當前串中的位置
其餘與正向相同
int rfind(char c, int pos = npos) ;
int rfind(const char *s, int pos = npos);
int rfind(const char *s, int pos, int n = npos);
int rfind(const string &s,int pos = npos) const;