机试真题刷题day9(复试倒计时5天)

2017 计算机院

1.Special数

题目描述
设一个正整数既是平方数又是立方数时,称其为Special 数。输入包含多组测试数据,第一行输入测试数据的组数,按若在后续每行输入n (n<=109) 的时候,请输出1 到n 中包含的所有Special 数的个数。
Sample Inputs

2
1
64
Sample Outputs

1
2

1、思路

(1)直接暴力解,写一个函数判断是不是Special数;

(2)从1~n循环,使用上面的函数进行判断,时间复杂度会比较高。

2、代码

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

bool isSpecial(int x) {
    bool flag1 = false,flag2 = false; // 分别判断是否是平方数和立方数
    int bound = sqrt(x);
    for(int i = 1 ; i <= bound ; i++) {
        if(i*i == x)
            flag1 = true;
        if(i*i*i == x)
            flag2 = true;
    }
    if(flag1 && flag2)
        return true;
    else
        return false;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        int n;
        scanf("%d",&n);
        int cnt = 0;
        for(int i = 1 ; i <= n ; i++) {
            if(isSpecial(i))
                cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

2.字符串操作


 

 

 1、思路

(1)注意l和r是表示下标,下标从1开始…开始没注意到这个问题导致最后的结果都挺大的,感觉这里题目没描述清楚?(不确定orz);

(2)用到的库函数:逆序函数reverse(),排序函数sort(),交换函数swap()。

2、代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
    int n,m;
    scanf("%d %d\n",&n,&m);
    int a[n];
    for(int i = 0 ; i < n ; i++) {
        scanf("%d",&a[i]);
    }
    for(int i = 0 ; i < m ; i++) {
        int c,l,r;
        scanf("%d %d %d",&c,&l,&r);
        l--;  // 处理函数
        r--;
        if(c == 1) {
            reverse(a + l, a + r + 1);
        } else if(c == 2) {
            int len;
            scanf("%d",&len);
            for(int i = 0 ; i < len ; i++) {
                swap(a[l+i],a[r+i]);  // 注意第二个参数的下标
            }
        } else if(c == 3) {
            int x;
            scanf("%d",&x);
            for(int i = l ; i <= r ; i++) {
                a[i] = x;
            }
        } else if(c == 4) {
            sort(a + l , a + r + 1);
        } else if(c == 5) {
            int sum = 0;
            for(int i = l  ; i <= r ; i++)
                sum += a[i];
            printf("%d\n",sum);
        }
    }

    return 0;
}

3.二叉树

题目描述
输入二叉树的前序遍历和中序遍历结果,输出二叉树的后序遍历结果
输入格式
第一行为二叉树先序遍历结果
第二行为二叉树中序遍历结果
输出格式
二叉树后序遍历结果。

Example
Inputs

426315
623415

Outputs

632514

        王道上面的模板题,找到规律根据前序和中序建树就可以了。

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

struct TreeNode {
    char data;
    TreeNode *lchild;
    TreeNode *rchild;
    TreeNode(char x) {
        this->data = x;
        this->lchild = NULL;
        this->rchild = NULL;
    };
};

TreeNode* BuildTree(string pre,string in) {
    if(pre.size() == 0)
        return NULL;
    char t = pre[0];
    TreeNode *root = new TreeNode(t);
    int pos = in.find(t);
    // 递归建立左子树
    root->lchild = BuildTree(pre.substr(1,pos),in.substr(0, pos));
    // 递归建立右子树
    root->rchild = BuildTree(pre.substr(pos+1),in.substr(pos+1));
    return root;
}

void PostOrder(TreeNode* root) {
    if(root == NULL) return;
    PostOrder(root->lchild);
    PostOrder(root->rchild);
    cout << root->data;
}

int main()
{
    string preOrder,inOrder;
    cin >> preOrder >> inOrder;
    TreeNode* t = BuildTree(preOrder, inOrder);
    PostOrder(t);
    cout << endl;
    return 0;
}

 

2019 计算机院

1.二进制

题目描述
32位二进制数X,对其进行X+1,X+3 操作,并输出。注意不能忽略前导0。(x<=2^32 - 3)

输入
第一行,一个整数T,代表测试数据组数。
接着T行,输入32为二进制数

输出
对每组测试数据,输出两行,第一行为X+1,第二行为X+3.

测试样例
输入

2
00000000000000000000000000000000
00000000000000000000000000000001

输出

00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000010
00000000000000000000000000000100

 1、思路:先把二进制x转换为十进制数,再分别进行+1,+3操作,把得到的结果转换为二进制输出,注意前导0不能省略。

2、代码

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

int main()
{
    int t;
    cin >> t;
    while(t--) {
        string x;
        cin >> x;
        long res = 0;
        long res1,res2;
        // 二进制转十进制
        for(int i = 0 ; i < x.size() ; i++) {
            res += res * 2 +  (x[i] - '0');
        }
        res1 = res + 1;
        res2 = res + 3;
        int num1[32] = {0} , num2[32] = {0};
        // 十进制转二进制
        for(int i = 0 ; i < 32 ; i++) {
            num1[i] = res1 % 2;
            res1 /= 2;
            num2[i] = res2 % 2;
            res2 /= 2;
        }
        for(int i = 31 ; i >= 0 ; --i) {
            cout << num1[i];
        }
        cout << endl;
        for(int i = 31 ; i >= 0 ; --i) {
            cout << num2[i];
        }
        cout << endl;
    }
    return 0;
}

2.二叉树

题目描述
对二叉树,计算任意两个结点的最短路径长度。

输入
第一行输入测试数据组数T
第二行输入n,m 。n代表结点的个数,m代表要查询的数据组数
接下来n行,每行输入两个数,代表1~n结点的孩子结点,如果没有孩子结点则输入-1.根节点为1.
接下来m行,每行输入两个数,代表要查询的两个结点

输出
每组测试数据输出m行,代表查询的两个结点之间的最短路径长度

测试样例
输入

1
8 4
2 3
4 5
6 -1
-1 -1
-1 7
-1 -1
8 -1
- 1 -1
1 6
4 6
4 5
8 1
输出

2
4
2
4

1、思路

(1)树的题好像套路都有点类似,前面几年也考的差不多,看来还是要掌握,先看看佬们的代码;

(2)树中两个节点的最短路径求法

①先找出两个节点的最近公共祖先;(这个好像前几年考过了)

②分别计算这两个节点到最近公共祖先的距离,加起来就得到两个节点的最短路径。

 2、代码

#include <iostream>
#include <cstdio>
#include <stack>
#include <string.h>
using namespace std;

const int N = 1005;

int father[N];

int main()
{
    int t;
    cin >> t;
    while(t--) {
        int n,m;
        cin >> n >> m;
        memset(father,0,sizeof(father));
        for(int i = 1 ; i <= n ; i++) {
            int a,b;
            cin >> a >> b;
            if(a != -1) father[a] = i;
            if(b != -1) father[b] = i;
        }
        while(m--) {
            int x,y;
            cin >> x >> y;
            stack<int> s1,s2;  // 分别存储x,y到最近公共祖先的节点
            while(x) {
                s1.push(x);
                x = father[x];
            }
            while(y) {
                s2.push(y);
                y = father[y];
            }
            while(!s1.empty() && !s2.empty() && s1.top() == s2.top()) {
                s1.pop();
                s2.pop();
            }
            cout << s1.size() + s2.size() << endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值