0%

【题解】一些总结

  • 主要内容
    • 没有主要的,全是次要的小东西

多点测试的几种输入方式

  • 没有说明有多少数据需要读入时;ctrl Z+Enter结束

    1
    2
    3
    4
    while(scanf("%d",&n) != EOF){}
    //scanf返回:成功读入参数的个数;读入失败返回-1

    while(gets(str) != NULL){}//谨此纪念因为把这个给忘了导致buaa机试爆零的惨痛教训QAQ
  • 输入数据满足某个条件时停止输入

    1
    2
    3
    4
    5
    while(scanf("%d%d",&a,&b) != EOF){ 
    if(a==0&&b==0) break;
    ...
    }
    while(scanf("%d%d",&a,&b)!=EOF,a||b){ }
  • 给出测试数据的组数

    1
    while(T--){}

常用头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
using namespace std;

int main() {
return 0;
}

cin读入加速

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
//之前做过一道题,即使加速了仍然可能tle,数据真的较大的话还是用scanf和printf吧
}

int最大值最小值

1
2
int maximum=INT_MAX;
int minimum=INT_MIN;

输出long long

1
printf("%lld",x);

long long的最大值最小值

1
2
LONG_LONG_MAX
LONG_LONG_MIN

scanf读单个字符

如果直接读入char字符的话,scanf的%c可能会读入一些空格回车等莫名其妙的 东西,用%s的话就会自动忽略那些莫名其妙的空格回车

所以用scanf读字符的话,建议读成字符串的形式,过滤掉空格回车

1
2
3
char op[2];
scanf("%s",op);//a
cout<<op[0]; //a

getchar()

读入单个字符,包括换行

读取一行字符串

char[]

1
2
char s[1001];
cin.getline(s,1000);//第二个参数为 允许输入的最大长度

string

1
2
string s;
getline(cin,s);

当同时使用cin>>getline(cin,str)的时候,在cin>>输入完成之后,getline(cin,str)(str是真正想要的串)之前,需要getline(cin,str)(这个str是一个打算用来存储回车符的串);

将回车符从输入流缓存中清除

1
2
3
4
int a;string line;
cin>>a;
getline(cin,line);//去掉换行
getline(cin,line);//真正读取这一串字符

输出格式

右对齐

1
2
3
4
5
int a = 123;
printf("%5d",a);//补空格
//输出: 123
printf("%05d",a);//补0
//输出:00123

保留小数

1
2
3
4
5
double d=12.3456;
printf("%.1f",d);//保留一位小数:采用"四舍六入五成双"规则
//输出:12.2

round(double x) //四舍五入,返回double类型

常用math函数

绝对值

fabs(double x)

取整

floor(double x) 下取整,返回double类型

ceil(double x)上取整,返回double类型

次方

pow(double r,double p): 返回$r^p$

平方

sqrt(double x) 返回double类型

对数

log(double x) ,求以自然对数为底的对数, 返回double类型

换地公式 $log_{a}b=log_{e}b/log_{e}a$

三角函数

1
2
sin(double x),cos(double x),tan(double x);//弧度制,返回double
asin(double x),acos(double x),atan(double x)