文章目录
题目汇总
1.浙江大学软件学院2020年保研上机模拟练习 7-2 Distance of Triples
2.Partitions
问题:用最大不超过m的正整数来划分n,有多少种方式?提示:递归
3.pat甲级1103 Integer Factorization (30 分)
【剪枝有难度,满分不容易】
4,Recycling of Shared Bicycles (30 分)
【建图+floyd+dfs】
5,1160 Forever (20 分)
【暴力搜索+剪枝】
20分得到12分,测试点2,3没过。
6, Postfix Expression (25 分)
【后缀表达式加括号】
1, 考察递归函数设计,需要对递归函数熟悉,才能加对括号
2, 类似习题:[1130 Infix Expression (25分)]
3,注意在表达式树中只有树只有四种形态。没有左空右不空那种
//我写的答案
void postOrder(int cur){
if(cur==-1) return;
if(nodes[cur].lchild==-1&&nodes[cur].rchild!=-1){
cout<<"("<<nodes[cur].data;
postOrder(nodes[cur].rchild);
cout<<")";
return;
}
cout<<"(";
postOrder(nodes[cur].lchild);
postOrder(nodes[cur].rchild);
cout<<nodes[cur].data<<")";
}
// 分析,写的比较好的答案
// 参考link: https://blog.youkuaiyun.com/cwtnice/article/details/107009254
string f(int x){
if(v[x].l==-1 && v[x].r==-1){ //左右孩子都为空
return "("+v[x].data+")";
}
if(v[x].l==-1 && v[x].r!=-1){ //左孩子为空右孩子非空
return "("+v[x].data+f(v[x].r)+")";
}
return "("+f(v[x].l)+f(v[x].r)+v[x].data+")"; //左右都非空
}