pat浙大C语言刷题,PAT(C语言刷题) - osc_lv07cfd1的个人空间 - OSCHINA - 中文开源技术交流社区...

难题:A1114、A1119

0、让Dev C++支持c11 Tools->Compier Options->Add the following commands when calling the compiler:

选择该选项卡,让后添加:-std=c+11,即可

0、字符串与转换为数字的函数int stoi(string)

int a=stoi("23");

1、对于char temp[10],可以用while(scanf("%s",temp)!=EOF)判断读取结束,输入时要输入Enter,Ctrl+Z,Enter标志结束。 ----如:PAT乙级1007( 素数对猜想)、乙级1010(一元多项式求导)

2、字符数组和string相互转换

#include

#include

using namespace std;

int main(){

char str[]="hello";

//字符数组转为string

string s(str);

printf("%s\n",str);

//string转为字符数组用c_str()

printf("%s\n",s.c_str());

return 0;

}

4、平衡二叉树调整、判断一棵树是不是完全二叉树

#include

#include

#include

using namespace std;

struct Node{

int data,height;

Node* left,*right;

};

int getHeight(Node* root){

if(root==NULL){

return 0;

}

return root->height;

}

int getBalanceFactor(Node* root){

if(root==NULL) return 0;

return getHeight(root->left)-getHeight(root->right);

}

void updateHeight(Node* &root){

if(root!=NULL){

root->height=max(getHeight(root->left),getHeight(root->right))+1;

}

}

void L(Node* &root){

Node* temp=root->right;

root->right=temp->left;

temp->left=root;

updateHeight(root);

updateHeight(temp);

root=temp;

}

void R(Node* &root){

Node* temp=root->left;

root->left=temp->right;

temp->right=root;

updateHeight(root);

updateHeight(temp);

root=temp;

}

void insert(Node* &root,int a){

if(root==NULL){

root=new Node;

root->data=a;

root->height=1;

root->left=root->right=NULL;

return;

}

if(adata){

insert(root->left,a);

updateHeight(root);

if(getBalanceFactor(root)==2){

if(getBalanceFactor(root->left)==1){

R(root);

}else{

L(root->left);

R(root);

}

}

}else{

insert(root->right,a);

updateHeight(root);

if(getBalanceFactor(root)==-2){

if(getBalanceFactor(root->right)==-1){

L(root);

}else{

R(root->right);

L(root);

}

}

}

}

bool flag=false;

void print(Node* root){

if(root==NULL) return;

if(flag==false){

flag=true;

}else{

printf(" ");

}

printf("%d",root->data);

}

bool isCBT(Node* root){

queue q;

q.push(root);

bool flg=true;

while(!q.empty()){

Node* front=q.front();

q.pop();

print(front);

if(front!=NULL){

q.push(front->left);

q.push(front->right);

}else{

while(q.size()>0){

Node* tmp=q.front();

if(tmp!=NULL){

flg=false;

break;

}

q.pop();

}

}

}

return flg;

}

int main(){

int n;

scanf("%d",&n);

Node* root=NULL;

for(int i=0;i

int a;

scanf("%d",&a);

insert(root,a);

}

if(isCBT(root)){

printf("\nYES\n");

}else{

printf("\nNO\n");

}

return 0;

}

6、分割一个字符串的笨方法

#include

#include

#include

using namespace std;

string ans[10];

int cnt=0;

void split(string key){

int j=0,p;

for(int i=0;i

if(key[i]==' '){

p=0;

char temp[10];

//cout<

while(j

temp[p++]=key[j++];

}

temp[p++]='\0';

string tmp(temp);

ans[cnt]=tmp;

j=i+1;

cnt++;

}

}

p=0;

while(j

ans[cnt][p++]=key[j++];

}

cnt++;

}

int main(){

string key="test code debug sort keywords";

split(key);

for(int i=0;i

cout<

}

return 0;

}

4.LCA算法 定义:The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA. 1151 LCA in a Binary Tree (30 分)

5.根据前序遍历和后序遍历求中序遍历 因为构造的二叉树不唯一,可能有多个中序遍历。 1119 Pre- and Post-order Traversals

#include

#include

using namespace std;

vector ans;

int *pre, *post, unique = 1;//表示唯一

int findFromPre (int x, int l, int r) {

for (int i = l; i <= r; i++) {

if (x == pre[i]) {

return i;

}

}

return -1;

}

void setIn (int preL, int preR, int postL, int postR) {

if (preL == preR) {

ans.push_back(pre[preL]);

return;

}

if (pre[preL] == post[postR]) {//根节点

int x = findFromPre(post[postR - 1], preL + 1, preR);//从前序遍历中找到右子树的根节点位置x,则在前序遍历中该节点之前都是左子树,之后都是右子树

int leftNum=x-preL-1;

if (leftNum>0) {//存在左子树

setIn(preL + 1, x - 1, postL, postL + leftNum - 1);//递归地从左子树进行

ans.push_back(post[postR]);//访问根节点

setIn(x, preR, postL + leftNum, postR - 1);//递归地从右子树进行

} else {

unique = 0;//不存在左子树,则构造的右子树不唯一

ans.push_back(post[postR]);//访问根节点

setIn(x, preR, postL + leftNum, postR - 1);//递归地向右子树进行

}

}

}

int main() {

int n = 0;

scanf("%d", &n);

pre = new int [n];

post = new int [n];

for (int i = 0; i < n; i++) {

scanf("%d", &pre[i]);

}

for (int i = 0; i < n; i++) {

scanf("%d", &post[i]);

}

setIn(0, n - 1, 0, n - 1);

printf("%s\n", unique ? "Yes" : "No");

printf("%d", ans[0]);

for (int i = 1; i < ans.size(); i++) {

printf(" %d", ans[i]);

}

printf("\n");

return 0;

}

贪心算法题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值