算法竞赛入门经典(第2版)学习笔记
第一章
1. 保留位数
计算8/5,并输出保留小数点后1位
#include <stdio.h>
int main(){
printf("%.1f\n",8.0/5.0);
}
百分号后小数点,然后是保留的位数,然后是浮点数f
整数输出用%d,浮点数输出用%f
但要注意整数/整数=整数,浮点数/浮点数=浮点数
#include <stdio.h>
#include <iomanip>
int main(){
cout << fixed << setprecision(5)<<8.0/5.0;
//精确到小数点后5位
}
使用标准输入输出格式时,导入头文件iomanip,然后使用cout<<fixed<<setprecision(5)精确到小数点后5位
2. 数学公式
头文件math包含多种数学公式,例如sqrt平方根,pow幂次方等
#include <math.h>
#include <iostream>
int main(){
printf("%.8f\n",1+2*sqrt(3)/(5-0.1));//平方根
cout<<pow(2.5,2);//幂次方
}
3. 输入输出
可以使用scanf和printf或者cin和cout输入输出
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int a,b;
scanf("%d",&a);
cin>>b;
printf("%d",a);
cout<<b;
}
scanf输入时变量前加 & 符号
注意scanf和printf的格式,%d、%f等表达的意义不同
第二章
1. 舍入取整
floor(x)返回的是小于或等于x的最大整数,四舍五入。如: floor(10.5) == 10 floor(-10.5) == -11
ceil(x)返回的是大于x的最小整数,上限。如: ceil(10.5) == 11 ceil(-10.5) ==-10
floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10
fix朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;
floor朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;
ceil朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;
round四舍五入到最近的整数,如round(-1.3)=-1;round(-1.52)=-2;round(1.3)=1;round(1.52)=2
2. 范围
int 都是32位整数,范围是-2147483648 ~ -2147483647(一般做题中10^9可以用int)
long long 是64位
注意范围,必要的时候需要选择使用long long 还是int
3. 文件读取
重定向
#define LOCAL
#include <stdio.h>
#define INF 100000000
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int x;
while(scanf("%d",&x)==1){
cout<<x<<endl;
}
}
fopen版
#include <stdio.h>
#define INF 100000000
int main(){
FILE *fin,*fout;
fin=fopen("data.in","rb");
fout=fopen("data.out","wb");
int x;
while(fscanf(fin,"%d",&x)==1){
cout<<x<<endl;
}
fprintf(fout,"%d",x);
fclose(fin);
fclose(fout);
return 0;
}
第三章
1. 数组
memcpy复制数组,memset对数组赋值
#include <string>
int main(){
memcpy(b,a,sizeof(a));
//将a全部复制到b中
memset(a,0,sizeof(a));
//清空a
}
2. ++
n++//加1前的值计算表达式
++n//加1后的值计算表达式
3. 字符串
使用scanf%s读取字符串时,变量前不加&,且遇到空白字符停下
#include <string>
int main(){
string s;
scanf("%s",s);
//读入字符串,且在空格前停止
for(int i=0;i<strlen(s);i++){
//strlen统计字符串的字符个数
if(strchr(s,'A')==NULL)return 0;
//strchr查找单个字符
if(strcmp(s,"hello"))cout<<"hello"<<endl;
//strcmp比较字符串
strcpy(a,b);
//strcpy赋值
strcat(a,b);
//strcat连接字符串
}
}
注意,字符串的赋值、比较、连接不可以使用"=","==","<=","+"等操作
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
string line;
int x;
while(getline(cin,line)){
stringstream ss(line);
while(ss>>x){
cout<<x<<endl;
}
return 0;
}
getline读取一行的数据,并且用这一行的数据创造一个字符串流sstream----ss
4. 局部变量、形参
#include <stdio.h>
using namespace std;
void swap(int &a,int &b){
int tmp=b;
b=a;
a=tmp;
}
int main(){
int a=1;b=0;
swap(a,b);
cout<<a<<" "<<b<<endl;
}
调用方法时需要在形参前加&或者*用来表示地址,表示这个参数按照传引用的方式传递,而不是传值,否则只是将参数的值传递过去不能起到真正对实参操作的目的
5.数组作为参数
传递指针*a和数组的大小
int sum (int *a,int n){
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
6. INT_MAX
#define INF 1000000000 取代 INT_MAX
min=-INF ; max=INF
7. a++和++a
当出现a++时,会先用a原始值进行计算,然后再给a加1
++a时,则正好相反
8.字符
isalpha判断字符是否是字母,类似的还有idigit,在ctype.h中定义
第四章
1. 框架
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
cout<<n<<m<<endl;
}
头文件<stdio.h>、<cstring>、<cmath>、<algorithm>等
2. 结构体
使用struct定义结构体
#include <stdio.h>
#include <iostream>
using namespace std;
struct point{
int x,y;
point(int n,int m){
x=n;
y=m;
}
point(){
x=0;
y=0;
}
};
point operator +(const point& a,const point &b){
return point(a.x+b.x,a.y+b.y);
}
ostream& opeartor <<(ostream &out,const point &p){
out<<"("<<p.x<<","<<p.y<<")"<<endl;
return out;
}
int main(){
point a,b(1,2);
a.x=3;
cout<<a+b<<endl;
}
3、数组传递参数