做個小筆記,pat時注意
1.map、set的自動排序如何解決
- 使用 unordered_map,unordered_set,則無序
- 重載排序,修改排序的函數
bool operator < (const node &a)const{
return cnt!=a.cnt?cnt>a.cnt:num<a.num;
}
2.lower_bound()和upper_bound()
lower:返回第一個大於等於的位置
upper:返回第一個大於的位置
3.最大公約數和最小公倍數
最大公約數:
int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
最小公倍數:
int d=gcd(a,b);
a/d*b;
4.sscanf和ssprintf
#include<stdio.h>
sscanf(str,"%d",&n);//將str以“%d”的格式寫到n中(從左至右)
sprintf(str,"%d",n)//將n以“%d”的格式寫到str中(從右至左)