【注意数据范围】
大数用__int64 a;(横线是两行)
printf(“%I64d”,a);
【memset函数】
头文件#include
#define N 10000000
int f[N+10];
void Getprime() {
memset(f, 0, sizeof(f));
f[1] = 1; //1不是素数,特殊处理下
for(int i = 2; i * i <= N; i++) {
if(f[i] == 1)
continue;
for(int j = i; j * i <= N; j++)
f[j*i]=1;
}
}
//筛选完后,f[i]=0,表示i为素数,f[i]=1,表示i不是素数。
【数学函数】
1.pow函数
//要加入头文件 math.h
pow(x,y);//其作用是计算x的y次方。x、y及函数值都是double型
2.double const Pi = acos(-1);
【sort的使用】
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b) {
//按从大到小排序
if(a > b) return true;
return false;
}
//结构体延伸
struct Node{
int x,y;
};
Node p[1010];
//先按x从小到大排序,如果x相同则按y从大到小排序
bool cmp(Node a,Node b) {
if(a.x < b.x) return true;
if(a.x == b.x && a.y > b.y) return true;
return false;
}
【最大公约数】
int gcd(int a,int b){return b?gcd(b,a%b):a;}
【取余公式 bjfuoj-1070】
(a+b)%c = (a%c+b%c)%c
(a*b)%c = ((a%c)*(b%c))%c
【快速幂取余】
BJFUOJ 1056
快速幂模板
__int64 powhaha(__int64 n,__int64 m) {
__int64 ans=1;
while(m>0) {
if(m&1) {
ans*=n;
}
n*=n;
m>>1;
}
return ans;
}
【sprintf 和 sscanf 函数】
sprintf是把格式化的数据写入某个字符串缓冲区。也就是将一些东西输出到字符串,其用法是sprintf(buf,”%d%d%d%d%d”,a,b,c,d,e);
例子:char* who = “I”; char* whom = “CSDN”; sprintf(s, “%s love %s.”, who, whom);
//产生:”I love CSDN. ” 这字符串写到s中
sprintf(s, “%10.3f”, 3.1415626);
//产生:” 3.142”
sscanf()是将一些东西输入到字符串,利用它可以从字符串中取出整数、浮点数和字符串等等
【字符串流函数】
将一个字符串转化为数:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
inline T fromString(const string &str) {
istringstream is(str);
T v;
is>>v;
return v;
}
int main() {
string s="465153";
int a=fromString<int>(s);
cout<<a<<endl;
return 0;
}
将一个值转化为string字符
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
inline string toString(const T &v) {
ostringstream os;
os << v;
return os.str();
}
int main() {
string s;
int a=415641;
s=toString(a);
cout<<a<<endl;
return 0;
}