PAT(C语言刷题)


难题: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<cstdio>
#include<string>
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;
}

3.红黑树
甲级1135
什么是红黑树

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

1123 Is It a Complete AVL Tree
1066 Root of AVL Tree

#include<cstdio>
#include<algorithm>
#include<queue>
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(a<root->data){
		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<Node*> 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<n;i++){
		int a;
		scanf("%d",&a);
		insert(root,a);
	}
	if(isCBT(root)){
		printf("\nYES\n");
	}else{
		printf("\nNO\n");
	}
	
	return 0; 
}

5、读取输入的小技巧
1022 Digital Library

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

#include<string>
#include<iostream>
#include<cstdio>
using namespace std;

string ans[10];
int cnt=0; 
void split(string key){
	int j=0,p;
	for(int i=0;i<key.length();i++){
		if(key[i]==' '){
			p=0;
			char temp[10];
//			cout<<j<<" "<<i<<" "<<cnt<<endl;
			while(j<i){
				temp[p++]=key[j++];	
			}
			temp[p++]='\0';
			string tmp(temp);
			ans[cnt]=tmp;
			j=i+1;
			cnt++;		
		}
	}
	p=0;
	while(j<key.length()){
		ans[cnt][p++]=key[j++];
	}
	cnt++;
}
int main(){

	string key="test code debug sort keywords";
	
	split(key);
	for(int i=0;i<cnt;i++){
		cout<<ans[i]<<endl;	
	}
	
	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 <cstdio>
#include <vector>
using namespace std;
vector<int> 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;
}

贪心算法题

1033 To Fill or Not to Fill


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM