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. webpack 输出多个文件

    http://react-china.org/t/webpack/1870/2 webpack 文章 entry = { "button": "demo/button/i ...

  2. 11种常见sqlmap使用方法

    sqlmap是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题. 一.SQLMAP用于Access数据库注入 (1) 猜解是否能 ...

  3. Mac OS 10.10.3下Apache + mod_wsgi配置【一】

    [一] 首先,MAC是自带Apache的,在/private/etc/apache2路径下,能够使用apachectl -v查看版本号.我的版本号例如以下: Server version: Apach ...

  4. 玩转单元測试之WireMock -- Web服务模拟器

    WireMock 是一个灵活的库用于 Web 服务測试,和其它測试工具不同的是.WireMock 创建一个实际的 HTTPserver来执行你的 Web 服务以方便測试. 它支持 HTTP 响应存根. ...

  5. uva 10765 Doves and Bombs(割顶)

     题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标 ...

  6. 逆向碰到3des分析

    1.ios 某个app碰到涉及3des的解密函数. 2.底层调用的库函数. 3.对比CCCrypt的头文件 CCCryptorStatus CCCrypt( CCOperation op, /* kC ...

  7. ASP.NET MVC Model之二模型绑定

    Asp.net mvc中的模型绑定,或许大家经常用,但是具体说他是怎么一回事,可能还是会有些陌生,那么,本文就带你理解模型绑定.为了理解模型绑定,本文会先给出其定义,然后对通过比,来得出使用模型绑定的 ...

  8. 协方差矩阵与主成分分析PCA

    今天看论文,作者是用主成分分析(PCA)的方法做的.仔细学习了一下,有一篇博客写的很好,介绍的深入浅出! 协方差:http://pinkyjie.com/2010/08/31/covariance/ ...

  9. 【CEOI2002】【Poj 1038】Bugs Integrated, Inc.

    http://poj.org/problem?id=1038 发一下中文题面(今天考试直接被改了): 生记茶餐厅由于受杀人事件的影响,生意日渐冷清,不得不暂时歇业.四喜赋闲在家,整天抱着零食看电视,在 ...

  10. [Codeforces 496E] Distributing Parts

    [题目链接] https://codeforces.com/contest/496/problem/E [算法] 按右端点排序 , 每个乐曲优先选取的左端点最大的演奏家 用std :: set维护贪心 ...