本文共 1084 字,大约阅读时间需要 3 分钟。
1<<n
(左移相当于乘2) 来代替2的n次方 pow(2, n).memset
用法:#include#include //memset头文件 using namespace std;int main(){ int a[5]; memset(a, 0, sizeof(a)); for(int i = 0; i < 5; i ++) cout << a[i] << " "; cout << endl; cout << sizeof(a) << " " << sizeof(1) << endl; //输出20 4 return 0;}
浮点运算误差
进行浮点数比较的时候,应考虑到浮点误差。 另:判断一个数是不是整数,只需要它与它的整数部分比较floor(m+0.5) == m ?
sort( ) 快速排序+自定义排序规则
方法一
#include#include using namespace std;bool cmp(int a, int b){ return a > b;}int main(){ int buf[5] = { 1, 2, 3, 4, 5}; sort(buf, buf + 5, cmp); for(int i = 0; i < 5; i ++) cout << buf[i] << " "; return 0;}
方法二
struct Student{ char name[100]; int age; int grade; bool operator< (const Student& a) const//运算符重载,加const为了适应性更广 { ... }}buf[1000];
sort(buf, buf+n)即可
struct Student{ char name[100]; int age; int grade;}buf[1000];
int max_gys(int a, int b){ if(b == 0) return a; else return max_gys(b, a%b);}
最小公倍数是两数的乘积除以最大公约数
转载地址:http://okuin.baihongyu.com/