PAT甲级考前整理之一网址:https://www.cnblogs.com/jlyg/p/7525244.html,主要总结了前面131题的类型以及易错题及坑点。

PAT甲级考前整理三网址:https://www.cnblogs.com/jlyg/p/10364727.html主要是讲132题开始的题目。

考前注意:

  1、写函数(有返回值的函数)容易忘记返回值,可能本地运行没问题,但是提交了就会有问题。

  2、不要把strlen()函数写到for、while的循环中,有时候会超时,最好是 int len = strlen(str);提前求出来。

  3、用sort比较的时候,比较函数 int comp(const ST& st1,const ST& st2);如果在comp中调用ST的fun函数,fun函数必须加上const,例子 int fun()const{return 0;}

  4、二位数组初始化不要直接赋值,比如int a[10][10] ={0},是错误的,应该使用memset(a,0,sizeof(a));(一维数组也最好不要直接复制,通过循环复制最好)

  5、不要使用gets,PAT系统不支持。可以使用fprintf,使用fprintf注意最后一个字符是'\n',特别是比较的时候就不相等了。使用这一类函数时,注意需要把前一个输入的'\n'使用getchar去掉。

string mygets()
{
char str[];
fgets(str,sizeof(str),stdin);
int len = strlen(str);
if(str[len-]=='\n') str[len-] = '\0'; \\最后一行可能没有换行符
return str;
}

  6、使用freopen("1.txt","r",stdin);来减少测试的时间。因为PAT里有定义ONLINE_JUDGE,所以你需要背一下,写错就没办了。用以下代码就无需在提交的时候修改就能提交(节约大量时间呀!!)

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif // ONLINE_JUDGE
return ;
}

  7、输出时注意单复数,比如单数是a、is,复数是加s、are。

  8、不要还没输入完就用break跳出,不然会有问题,具体可以看我以下代码和代码中的注释,例题PAT1122


  9、输出id和编号的时候,如果你存储是数字,请加上前面补足比如4位整型,%04d,用%d的时候就有问题了,比如0001输出就是1。

  10、格式输入了解一下,比如时间"10:20:50"你可以用scanf("%d:%d:%d",&h,&m,&s)。这样可以省去一部分转换的时间。

  11、数组开的过大可能会有问题

  12、若图比较复杂,使用dfs会爆栈(段错误),此时可以选择bfs。(PAT真题1091)

   13、rounded up是四舍五入,不是向上取整。

  14、二分法给的数据可能左端比右端大,可以做一下1012就知道了。

  15、对数字字符串操作时,要注意数字0端是高位,而不是低位,你可能需要倒转字符串之类的。(PAT真题1024)

  16、字符串数字,转换成数字需要去前导零,但是要考虑“0”的情况(PAT整体1038)

  17、排序或者二叉树如果没有说明不同,就说明存在有相同的值。

  STL总结:

    1.vector

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
void display(vector<int> vi)
{
for(int i=;i<vi.size();++i)
printf("%d ",vi[i]);
printf("\n");
}
int main()
{
vector<int> vi;
vi.push_back();
vi.push_back();
display(vi);
//插入
vi.insert(vi.begin()+,);
display(vi);
//删除
vi.erase(vi.begin()+);
display(vi);
//清空
vi.clear();
return ;
}

      2.queue与priority_queue

#include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std; int main()
{
//优先队列会自动排序
priority_queue<int,vector<int>,greater<int> >pq;
pq.push();
pq.push();
pq.push();
while(!pq.empty())
{
//queue的话是front函数
int data = pq.top();
pq.pop();
printf("%d ",data);
}
printf("\n");
return ;
}

    3.map

#include<iostream>
#include<cstdio>
#include<map>
using namespace std; int main()
{
map<int,int> mii;
mii.insert(pair<int,int>(,));
mii[] = ;
printf("删除前:%d\n",mii[]);
//删除key=2的值
mii.erase();
printf("删除后:%d\n",mii[]);
return ;
}

    4.string

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std; int main()
{
char str[] = "hello world";
string s(str);
printf("%s\n",s.c_str());
int index = s.find(' ');
string s2 = s.substr(,index);
string s3 = s.substr(index+,s.length()-index-);
printf("%s\n%s\n",s2.c_str(),s3.c_str());
//字符串追加
string newstr = "";
newstr += s2;
newstr += "-";
newstr += s3;
printf("%s\n",newstr.c_str());
//字符串比较
s2 = "aabc",s3="aacc";
printf("%d\n",s2.compare(s3)); }

    5.set

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std; void display(char* str,set<int> si)
{
printf("%s: ",str);
set<int>::iterator it = si.begin();
for(;it != si.end();it++)
printf("%d ",*it);
printf("\n");
}
int main()
{
set<int> s1;
set<int> s2;
s1.insert();
s1.insert();
s1.insert();
s2.insert();
s2.insert();
s2.insert();
s2.insert();
display("s1",s1);
display("s2",s2);
//erase的参数是key
s2.erase();
display("s2",s2);
set<int> s3,s4;
//交集
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,s3.begin()));
//并集
set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s4,s4.begin()));
display("交集",s3);
display("并集",s4);
return ;
}

    

    6、map、set、vector、queue、stack的遍历

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std; int main()
{
int n = ;
vector<int> vi;
for(int i=;i<n;++i)
vi.push_back(i);
printf("vector的遍历:");
for(int i=;i<n;++i)
printf("%d ",vi[i]);
puts("");
printf("set的遍历:");
set<int> si;
for(int i=;i<n;++i)
si.insert(i);
for(set<int>::iterator it=si.begin();it!=si.end();it++)
printf("%d ",*it);
printf("\nmap的遍历:");
//map和set的遍历只能用iterator的方式
map<int,int> mii;
for(int i=;i<n;++i)
mii[i] = i;
for(map<int,int>::iterator it=mii.begin();it!=mii.end();it++)
printf("[%d %d] ",it->first,it->second);
printf("\nqueue的遍历:");
//queue和statck遍历只能通过读取和pop的方式
queue<int> qi; //先进先出
for(int i=;i<n;++i)
qi.push(i);
while(!qi.empty())
{
printf("%d ",qi.front());
qi.pop();
}
printf("\nstack的遍历:");
stack<int> st;
for(int i=;i<n;++i)
st.push(i);
while(!st.empty())
{
printf("%d ",st.top());
st.pop();
}
puts("");
return ;
}

  一些树的算法:

  如何判断一颗树是完全二叉树,例题可以看PAT1123,可以用一个队列,每次加入一个节点的左右节点,并且pop出该节点,如果该节点为空,则跳出循环,如果该树是完全二叉树,则队列中应该都是null,如果存在非空,就说明不是完全二叉树

bool isCBT(Node* root)
{
queue<Node*> q;
q.push(root);
Node* node;
while(node=q.front())
{
q.push(node->left);
q.push(node->right);
q.pop();
}
while(!q.empty())
{
if(q.front()) return false;
else q.pop();
}
return true;
}

  二叉树的层级遍历(非递归)

bool bFirst = true;
void level_travel(Node* root)
{
queue<Node*> q;
q.push(root);
Node* node;
while(!q.empty()&&(node=q.front()))
{
if(bFirst) bFirst = false;
else printf(" ");
printf("%d",node->val);
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
q.pop();
}
}

  判断图是否遍历所有点以下是错误的例子(PAT1126)

void dfs(int curI,int istep)
{
if(istep==n)
{
flag = true;
return;
}
for(int nextI=1;nextI<=n;++nextI)
{
if(Map[curI][nextI]&&!bVis[nextI])
{
bVis[nextI] = true;
dfs(nextI,istep+1);
}
}
}

如 图

      1

3      2    4

从2节点遍历,1,3,4,它的step为2

应该用以下判断:判断sum是否等于顶点个数。

int sum=0;
void dfs(int curI,int istep)
{
++sum;
for(int nextI=1;nextI<=n;++nextI)
{
if(Map[curI][nextI]&&!bVis[nextI])
{
bVis[nextI] = true;
dfs(nextI,istep+1);
}
}
}

  如何优化BST搜索的效率,还在思考(当给出一个BST时,当测试数据极端时,二叉树等同于线性结构,此时如何优化搜索效率)。

  二叉树的遍历可以看https://www.cnblogs.com/jlyg/p/10354622.html,主要整理了几种二叉树遍历的类型,如已知中序和前序求后序?已知前序和后序求中序?已知中序完全二叉树求广度?已知先序的搜索二叉树求后序?

  有时间可以看一下AVL树的模板:https://www.cnblogs.com/jlyg/p/7521434.html

  红黑树只要了解性质就好了https://www.cnblogs.com/jlyg/p/10361506.html,实现比较繁琐肯定不考。

  其他:

  Dijkstra模板:https://www.cnblogs.com/jlyg/p/7404082.html

  Pim模板(从没考过):https://www.cnblogs.com/jlyg/p/10371948.html

  并查集的例题:https://www.cnblogs.com/jlyg/p/7356992.html

  DFS和BFS肯定是要必会的,这里就不多讲了。DFS注意剪纸就好了,如果map超时就用vector保存路径。

PAT甲级考前整理(2019年3月备考)之二,持续更新中.....的更多相关文章

  1. PAT甲级考前整理(2019年3月备考)之三,持续更新中.....

    PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...

  2. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  3. IDEA 2019.2破解激活教程(激活到2089年8月,亲测有效,持续更新中...)

    本来笔者这边是有个正版激活码可以使用的,但是,2019.9月3号的时候,一些小伙伴反映这个注册码已经失效了,于是拿着自己的 IDEA, 赶快测试了一下,果不其然,已然是不能用了. 好在,笔者又找到了新 ...

  4. IntelliJ IDEA 2019.2.1 破解教程, 最新激活码(激活到2089年8月,亲测有效,持续更新中...)

    当前最新版本 IDEA 2019.2.1 本来笔者这边是有个正版激活码可以使用的,但是,2019.9月3号的时候,一些小伙伴反映这个注册码已经失效了,于是拿着自己的 IDEA, 赶快测试了一下,果不其 ...

  5. 系列文章:老项目的#iPhone6与iPhone6Plus适配#(持续更新中,更新日期2014年10月12日 星期日 )

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020399.html ,转载请注明出处. ********************************** ...

  6. 一些JavaSE学习过程中的思路整理(主观性强,持续更新中...)

    目录 一些JavaSE学习过程中的思路整理(主观性强,持续更新中...) Java书写规范 IDEA的一些常用快捷键 Java类中作为成员变量的类 Java源文件中只能有一个public类 Java中 ...

  7. 常见 git 需求整理(持续更新中)

    首发于 语雀文档 突然感觉自己对 git 还是挺熟悉的,因为团队里新来的七八号应届生来问我 git 问题,基本没有答不上的情况,但为了能更好地对知识进行整理,还是记录一下为好. (希望能)持续更新.. ...

  8. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  9. AtCoder整理(持续更新中……)

    做了那么久的atcoder觉得自己的题解发的很乱 给有想和我一起交流atcoder题目(或者指出我做法的很菜)(或者指责我为什么整场比赛只会抄题解)的同学一个索引的机会??? 于是写了个爬虫爬了下 A ...

随机推荐

  1. linux工具一览

    http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/sar.html

  2. Office 针式打印机如何调节边距

    1 右击针式打印机,选择"打印机属性"   2 点击"打印机参数设置"选项卡,之前打印出来如果发现上下距离不合适,可以通过调节但也纸页顶距来调整   该参数值可 ...

  3. cocos2dx-3.0(21) 移植android平台 说多了都是泪

    ----我的生活,我的点点滴滴! ! 网上3.0的教程真心少.能够说没有吧,大多都是2.x 或者 3.0測试版之类的,因为我心大,没有照着2.x去搞,后来搞完后总结了一下,发觉事实上3.0的移植and ...

  4. 机器学习笔记——SVM

    SVM(Support Vector Machine).中文名为 支持向量机.就像自己主动机一样.听起来异常神气.最初总是纠结于不是机器怎么能叫"机",后来才知道事实上此处的&qu ...

  5. 斜率优化专题1——bzoj 1597 [Usaco2008 Mar] 土地购买 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/24387147 [原题] 1597: [Usaco2008 Mar]土地购买 Time ...

  6. Android系统之路(初识MTK) ------ OTA打包ROM安装系统img等到ZIP

    在做OTA升级包的时候,我编译了好多次都没过.老是IO异常.刚開始以为是我 make 的错误.后来多次检查 Error 发现是我的配置信息写错了,与驱动project师一起检查源代码, 改动配置信息后 ...

  7. Struts2默认拦截器栈及内建拦截器使用具体解释

    Struts2内建拦截器介绍:   alias (别名拦截器):同意參数在跨越多个请求时使用不同别名,该拦截器可将多个Action採用不同名字链接起来,然后用于处理同一信息.  autowiring  ...

  8. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...

  9. Window 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll。尝试重新安装该程序以解决此问题。

    现象: 解决办法: 方法一:缺什么补什么 http://www.greenxf.com/soft/125654.html 把api-ms-win-crt-runtime-l1-1-0.dll下载到电脑 ...

  10. bzoj1036 [ZJOI2008]树的统计Count——LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1036 LCT水题! 然而没有1A(咬牙)! 注意值有负数,所以取 max 的话要把作为“哨兵 ...