PAT (Advanced Level) 1020 二叉树的建树及遍历 C++中关于.和::和:和->的区别 1024 字符串加法 模拟 1050 STL set容器

STPAT 1020

//根据后序遍历和中序遍历,得到层次遍历  bfs
#include<bits/stdc++.h>
using namespace std;
const int maxn=30+5;
int a[maxn],b[maxn],n;            //后序遍历   中序遍历
struct node{
    int weight;
    node* lchild;
    node* rchild;
};
node* init(int aL , int aR , int bL , int bR)
{
    if(aL > aR) {
        return NULL;
    }
    node* root=new node;
    root->weight=a[aR];
    int k;
    for(k = bL ; k <= bR ; k++)
    {
        if(b[k] == a[aR]) break;
    }
    int dis = k - bL;
    root->lchild = init(aL , aL + dis -1 , bL ,k-1);
    root->rchild = init(aL + dis , aR-1 , k+1 , bR);
    return root;
}
int num=0;
void bfs(node* root)
{
    queue<node*>pq;
    while(!pq.empty()) pq.pop();
    pq.push(root);
    while(!pq.empty())
    {
        node* now=pq.front();
        pq.pop();
        cout<<now->weight;
        num++;
        if(num<n) cout<<" ";
        if(now->lchild!=NULL) {
            pq.push(now->lchild);
        }
        if(now->rchild!=NULL) {
            pq.push(now->rchild);
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&b[i]);
    //递归建树
    node* root=init(0 , n-1 , 0 , n-1);
    bfs(root);
    return 0;
}

C++中关于.和::和:和->的区别:
1. A.B即A为对象或结构体;

2. A->B则A为指针,->是成员提取,A->B是提取A中的成员B,A只能是指向类、结构、联合的指针;

3. ::是作用域运算符,A::B表示作用域A中的名称B,A可以是名字空间、类、结构

4. :一般用来表示继承,也用于三目运算符

 

PAT 1024

#include<bits/stdc++.h>
using namespace std;
bool is_prind(string s)
{
	int len=s.length();
	for(int i=0;i<len;i++)
		if(s[i]!=s[len-1-i]) return false;
	return true;
}
string add(string s)
{
	char s1[1000];
	int len=s.length();
	string s2;
	s2.clear();
	s2=s;
	reverse(s2.begin(),s2.end());
	int last=0,cnt=0;
	for(int i=len-1;i>=0;i--)
	{
		int k=(s[i]-'0')+(s2[i]-'0')+(last);
		if(k>=10) {
			last=1;
			s1[cnt++]=k-10+'0';
		}
		else {
			last=0;
			s1[cnt++]=k+'0';
		}
	}
	if(last) {
		s1[cnt++]=last+'0';
	}
	s1[cnt]='\0';
	string s3;
	s3.clear();
	s3=(string)s1;
	reverse(s3.begin(),s3.end());
	return s3;
}
int main()
{
	char s[20];
	int k;
	scanf("%s%d",s,&k);
	string st;
	st.clear();
	st=(string)s;
	if(is_prind(st)) {
		cout<<st<<endl<<"0"<<endl;
		return 0;
	}
	bool flag=false;
	string solve;
	solve.clear();
	int num=0;
	solve=st;
 	for(int i=1;i<=k;i++)
	{
		string pos;
		pos.clear();
		pos=add(st);
		if(is_prind(pos)) {
			flag=true;
		}
		st.clear();
		st=pos;
		num=i;
		solve.clear();
		solve=pos;
		if(flag) break;
	}
	cout<<solve<<endl<<num<<endl;
	return 0;
}

PAT 1050

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+100;
int main()
{
	string a,b;
	getline(cin,a,'\n');
	getline(cin,b,'\n');
	set<char>str;
	str.clear();
	for(int i=0;i<b.length();i++)
		str.insert(b[i]);
	char s[maxn];
	int cnt=0;
	for(int i=0;i<a.length();i++)
	{
		if(str.count(a[i])==1) {
			continue;	
		}
		s[cnt++]=a[i];
	}
	s[cnt]='\0';
	for(int i=0;i<cnt;i++)
	{
		cout<<s[i];
	}
	cout<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好,用C语言编写一个程序,实现以上功能需要进行如下步骤: 1. 定义二叉树的结构体,包括左右子指针和节点值等信息。 2. 编写函数实现二叉树的建立,可以采用递归或非递归方式进行建立。 3. 编写函数实现二叉树遍历,可以采用递归或非递归方式进行遍历。 4. 编写函数实现二叉树的前序遍历,可以采用递归或非递归方式进行遍历。 5. 编写函数实现二叉树的后序遍历,可以采用递归或非递归方式进行遍历。 6. 编写函数实现计算二叉树的深度,可以采用递归方式进行计算。 7. 编写函数实现统计二叉树节点数,可以采用递归方式进行统计。 以下是示例代码: ``` #include <stdio.h> #include <stdlib.h> // 定义二叉树结构体 typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 建立二叉树 void createTree(TreeNode** root) { int val; scanf("%d", &val); if (val == -1) { *root = NULL; } else { *root = (TreeNode*)malloc(sizeof(TreeNode)); (*root)->val = val; createTree(&(*root)->left); createTree(&(*root)->right); } } // 遍历 void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } // 前序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } // 后序遍历 void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } // 计算深度 int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1; } // 统计节点数 int countNodes(TreeNode* root) { if (root == NULL) { return 0; } return countNodes(root->left) + countNodes(root->right) + 1; } int main() { TreeNode* root; // 建立二叉树 createTree(&root); // 遍历 printf("Inorder Traversal: "); inorderTraversal(root); printf("\n"); // 前序遍历 printf("Preorder Traversal: "); preorderTraversal(root); printf("\n"); // 后序遍历 printf("Postorder Traversal: "); postorderTraversal(root); printf("\n"); // 计算深度 int depth = maxDepth(root); printf("Depth of the tree is: %d\n", depth); // 统计节点数 int count = countNodes(root); printf("Number of nodes in the tree is: %d\n", count); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值