12/2

题型. 1.红黑树2.模拟大数3.前序中序推后序4.模拟题。

1135 Is It A Red-Black Tree (30 分)

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
(1) Every node is either red or black.
(2) The root is black.
(3) Every leaf (NULL) is black.
(4) If a node is red, then both its children are black.
(5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.

Input Specification:

Each input file contains several test cases. The first line gives a positive integer K (≤30) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.

Output Specification:

For each test case, print in a line “Yes” if the given tree is a red-black tree, or “No” if not.

Sample Input:

3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17

Sample Output:

Yes
No
No

红黑树的基本定义

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=10005;
const int INF=0x3f3f3f3f;
struct node
{
    int data,height;
    node* lchild;
    node* rchild;
};
int num[MAX];
node* newnode(int x)
{
    node* Node=new node;
    Node->data=x;
    Node->lchild=Node->rchild=NULL;
    return Node;
}
node*Insert(node* &root,int x)
{
    if(root==NULL)
    {
        root=newnode(x);
        return root;
    }
    if(abs(x)<abs(root->data))
    {
        Insert(root->lchild,x);
    }
    else if(abs(x)>=abs(root->data))
    {
        Insert(root->rchild,x);
    }
    return root;
}
bool judge1(node* root)
{
    if(root==NULL)
        return true;
    if(root->data<0)
    {
        if(root->lchild!=NULL&&root->lchild->data<0)
            return false;
        if(root->rchild!=NULL&&root->rchild->data<0)
            return false;
    }
    return judge1(root->lchild)&&judge1(root->rchild);
}
int getnum(node* root)
{
    if(root==NULL)
        return 0;
    int l=getnum(root->lchild);
    int r=getnum(root->rchild);
    if(root->data>0)
    {
        return max(l,r)+1;
    }
    else
        return max(l,r);
}
bool judge2(node* root)
{
    if(root==NULL)
        return true;
    int l=getnum(root->lchild);
    int r=getnum(root->rchild);
    if(l!=r)
        return false;
    return judge2(root->lchild)&&judge2(root->rchild);
}
int main()
{
   int n,m;
   scanf("%d",&n);
   for(int i=0;i<n;i++)
   {
       scanf("%d",&m);
       node* root=NULL;
       for(int j=0;j<m;j++)
       {
           scanf("%d",&num[j]);
           root=Insert(root,num[j]);
       }
       if(num[0]<0||judge1(root)==false||judge2(root)==false)
        printf("No\n");
       else
        printf("Yes\n");
   }
    return 0;
}

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k+1 digits a​i as a​k​​ ⋯a​1​​ a​0 with 0≤a​i​​ <10 for all i and a​k>0. Then N is palindromic if and only if a​i​​ =a​k−i for all i. Zero is written 0 and is also palindromic by definition.
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, you are supposed to find its paired palindromic number.

Input Specification:

Each input file contains one test case which gives a positive integer no more than 1000 digits.

Output Specification:

For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:
A + B = C
where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number – in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.

Sample Input 1:

97152

Sample Output 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 2:

196

Sample Output 2:

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

大数加法模拟

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=1000005;
const int INF=0x3f3f3f3f;
string rev(string x)
{
    reverse(x.begin(),x.end());
    return x;
}
string add(string x,string y)
{
    string c=x;
    int carry=0;
    for(int i=x.length()-1;i>=0;i--)
    {
        c[i]=(x[i]-'0'+y[i]-'0'+carry)%10+'0';
        carry=(x[i]-'0'+y[i]-'0'+carry)/10;
    }
    if(carry>0)
    {
        c="1"+c;
    }
    return c;
}
int main()
{
    string a,sum;
    int n=10;
    cin>>a;
    if(a==rev(a))
    {
        cout<<a<<" is a palindromic number."<<endl;
        return 0;
    }
    while(n--)
    {
        sum=add(a,rev(a));
        cout<<a<<" + "<<rev(a)<<" = "<<sum<<endl;
        if(sum==rev(sum))
        {
            cout<<sum<<" is a palindromic number."<<endl;
            return 0;
        }
        a=sum;
    }
    cout<<"Not found in 10 iterations."<<endl;
    return 0;
}

1138 Postorder Traversal (25 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 50,000), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the first number of the postorder traversal sequence of the corresponding binary tree.

Sample Input:

7
1 2 3 4 5 6 7
2 3 1 5 4 7 6

Sample Output:

3

简单题

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=1000005;
const int INF=0x3f3f3f3f;
int in[MAX],pre[MAX];
int n;
vector<int>post;
struct node
{
    int data;
    node* lchild;
    node* rchild;
};
node* create(int prel,int prer,int inl,int inr)
{
    if(prel>prer)
        return NULL;
    node* root=new node;
    root->data=pre[prel];
    int k;
    for(k=inl;k<=inr;k++)
    {
        if(in[k]==pre[prel])
            break;
    }
    int numleft=k-inl;
    root->lchild=create(prel+1,prel+numleft,inl,k-1);
    root->rchild=create(prel+numleft+1,prer,k+1,inr);
    return root;
}
void postorder(node* root)
{
    if(root==NULL)
        return ;
    postorder(root->lchild);
    postorder(root->rchild);
    post.push_back(root->data);

}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&pre[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&in[i]);
    }
    node* root=create(1,n,1,n);
    postorder(root);
    printf("%d",post[0]);
    return 0;
}

1137 Final Grading (25 分)

For a student taking the online course “Data Structures” on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. The final grade is calculated by G=(G​mid−term×40%+G​final×60%) if G​mid−term >G​final​​ , or G​final will be taken as the final grade G. Here G​mid−term and G​final
​ are the student’s scores of the mid-term and the final exams, respectively.
The problem is that different exams have different grading sheets. Your job is to write a program to merge all the grading sheets into one.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integers: P , the number of students having done the online programming assignments; M, the number of students on the mid-term list; and N, the number of students on the final exam list. All the numbers are no more than 10,000.
Then three blocks follow. The first block contains P online programming scores G​p 's; the second one contains M mid-term scores G​mid−term​​ 's; and the last one contains N final exam scores G​final​​ 's. Each score occupies a line with the format: StudentID Score, where StudentID is a string of no more than 20 English letters and digits, and Score is a nonnegative integer (the maximum score of the online programming is 900, and that of the mid-term and final exams is 100).

Output Specification:

For each case, print the list of students who are qualified for certificates. Each student occupies a line with the format:

StudentID G​p G​mid−term G​final G

If some score does not exist, output “−1” instead. The output must be sorted in descending order of their final grades (G must be rounded up to an integer). If there is a tie, output in ascending order of their StudentID’s. It is guaranteed that the StudentID’s are all distinct, and there is at least one qullified student.

Sample Input:

6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

Sample Output:

missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84

模拟题,写的有点乱,最后一个测试点答案错误,19分,头绪有点乱,明天更。。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=10005;
const int INF=0x3f3f3f3f;
int p,m,n;
map<string,int>Gp,Gmid,Gfin,G;
vector<string>st;
set<string>number;
string id;
struct num
{
    int g;
    int gp;
    int gmid;
    int gfin;
    string str;
}NUM[MAX];
int score;
bool cmp(num a,num b)
{
    if(a.g!=b.g)
        return a.g>b.g;
    else
        return a.str<b.str;
}
int main()
{
    scanf("%d%d%d",&p,&n,&m);
    for(int i=0;i<p;i++)
    {
        cin>>id>>score;
        if(score>=200)
        {
            Gp[id]=score;
            st.push_back(id);
        }
        else
            continue;
    }
    for(int i=0;i<n;i++)
    {
        cin>>id>>score;
        Gmid[id]=score;
    }
    for(int i=0;i<m;i++)
    {
        cin>>id>>score;
        Gfin[id]=score;
        if(Gfin[id]<60)
        {
            continue;
        }
        st.push_back(id);
        if(Gmid[id]!=0&&Gmid[id]>Gfin[id])
        {
            G[id]=Gmid[id]*0.4+Gfin[id]*0.6+0.5;
        }
        else
            G[id]=Gfin[id];
    }
    for(int i=0;i<st.size();i++)
    {
        if(Gp[st[i]]<200||Gfin[st[i]]<60)
            st.erase(st.begin()+i);
        else
            number.insert(st[i]);
    }
    int t=0;
    for(set<string>::iterator it=number.begin();it!=number.end();it++)
    {
        NUM[t].str=*it;
        NUM[t].g=G[*it];

        if(Gp[*it]==0)
        NUM[t].gp=-1;
        else
        NUM[t].gp=Gp[*it];

        if(Gmid[*it]==0)
        NUM[t].gmid=-1;
        else
        NUM[t].gmid=Gmid[*it];

        if(Gfin[*it]==0)
        NUM[t].gfin=-1;
        else
        NUM[t].gfin=Gfin[*it];

        t++;
    }
    sort(NUM,NUM+t,cmp);
    for(int i=0;i<t;i++)
    {
        cout<<NUM[i].str<<" "<<NUM[i].gp<<" "<<NUM[i].gmid<<" "<<NUM[i].gfin<<" "<<NUM[i].g<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值