机试真题刷题day8(倒计时6天)

这篇文章展示了多个计算机科学题目,涵盖求导数的计算、链表操作、三元组问题、HDLC协议处理、01序列还原、矩阵操作、字符串处理和特定数字查找等算法和数据结构的应用。这些题目旨在测试编程和逻辑思维能力。
摘要由CSDN通过智能技术生成

2015 计算机院

第一题:求导数
描述:
求函数 f(x) = a*x^3 + b*x^2 + c*x + d x = x0 处的一阶导数。
输入:
a b c d x0
输出:
f'(x0)
样例输入:
1 1 1 1 1
样例输出:
6

第二题:LIST

题目描述

在该LIST上实现3种操作

         1、append x在该LIST末尾添加x,x是32位整数

         2、pop删除该LIST末尾的数

         3、find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i = -1表示寻找倒数第一个

输入

首先一个数t表示以下有t个m

第一行输入一个m,表示有m条操作,接下来每行输入一条操作

输出

当输入find i时输出找到的数

样例输入

2 
5 
append 1 
append 2 
find 1 
find -1 
pop 
6 
append 1 
append 2 
append 3 
append 4 
find -2 
find 2

样例输出

1
2
3
2

 1、思路

(1)find i,注意判断i>0和i<0的两种不同情况,i<0时找倒数第i个,倒数第i个 = 正数第len - i + 1个;

(2)开始想复杂了,用链表模拟,写半天函数报一堆错,C/C++语法好多都忘记了…后来还是用数组模拟的。

2、代码

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

const int N = 10000;

int main()
{
    int t;  // t组测试数据
    cin >> t;
    while(t--)
    {
        int m;  // m条操作
        cin >> m;
        long num[N];
        int cnt = 0;
        while(m--)
        {
            string command;
            cin >> command;
            if(command == "append")
            {
                long x;  // x是32位整数
                cin >> x;
                num[cnt++] = x;  // 把x插入list末尾
            }
            else if(command == "pop")
            {
                cnt--;
            }
            else if(command == "find")
            {
                int i;
                cin >> i;
                if(i > 0)
                {
                    cout << num[i-1] << endl;
                }
                else  // 寻找倒数第i个
                {
                    i = -i;
                    cout << num[cnt - i + 1] << endl;
                }
            }
        }
    }

    return 0;
}

2016 计算机院

1.三元组
问题描述
给你一个长度为 m 的数组(数组元素从 0 m-1 ),如果数组里有 a[i]+a[j]==a[k](i,j,k
于等于 0 并且小于 m) ,便称之为三元组。现在给你一个数组,让你求三元组的个数。
例如 m 2 ,里面的元素为( 0,0
那么三元组为
a[0],a[0],a[0]
a[0],a[0],a[1]
a[0],a[1],a[0]
a[0],a[1],a[1]
a[1],a[0],a[0]
a[1],a[0],a[1]
a[1],a[1],a[0]
a[1],a[1],a[1]
输出答案为 8. Input
输入正整数 N ,表示 N 测试 。接着输入 N 组数据,每组输入 m(1<=m<=50) ,表示数组
长度,然后输入这个数组。
Output
对每组输入数据,输出三元组的个数。
Sample Input
2
2
0 0
5
1 1 1 2 1
Sample Output
8
16
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n;
    scanf("%d\n",&n);
    while(n--) {
        int m;
        scanf("%d\n",&m);
        int a[m];
        for(int i = 0 ; i < m ; i++) {
             scanf("%d",&a[i]);
        }
        int cnt = 0;  // 直接暴力三重循环就完事儿
        for(int i = 0 ; i < m ; i++) {
            for(int j = 0 ; j < m ; j++) {
                for(int k = 0 ; k < m ; k++) {
                    if(a[i] + a[j] == a[k])
                        cnt++;
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

 2.寻找变化前 01 序列

问题描述
给你一个 01 序列, HDLC 协议处理的话,如果出现连续的 5 1 会补 1 0 。例如
1111110 ,会变成 11111010
现在给你一个经过 HDLC 处理后的 01 序列,你需要找到 HDLC 处理之前的 01 序列。
例如给你 11111010
你需要输出 1111110
Input
输入正整数 N ,表示 N 例测试。接着输入 N 组数据,每组输入经过 HDLC 处理过的 01
列(长度小于 100 )。
Output
对每组输入数据,输出 HDLC 处理前的 01 序列。
Sample Input
2
11111010
1111100
Sample Output
1111110
111110
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

const int N = 10000;

int main()
{
    int n;
    scanf("%d\n",&n);
    while(n--){
        string str;
        cin >> str;
        int cnt = 0;
        for(unsigned int i = 0 ; i < str.size() ; i++) { // 直接用int i 报错
            if(str[i] == '1')
                cnt++;
            else { // 遇到0
                cnt = 0;
            }
            cout << str[i];
            if(cnt == 5) {
                i++;
                cnt = 0;
            }
        }
        cout << endl;
    }
    return 0;
}

 3.寻找 i*j=m 的个数

问题描述
3*3 的矩阵内容。
1 2 3
2 4 6
3 6 9
a[i][j](1<=i<=n,1<=j<=n)=i*j
问一个这样 n*n 的矩阵里面,里面 m 出现的次数。
例如 n 3,m 6.
那么出现的次数就是 2
Input
输入正整数 N ,表示 N 例测试( N<=20 )。接着输入 n n<=10^5 ), m <=10^9 )。
Output
对每组输入数据,输出 m 出现的次数。
Sample Input
2
3 6 3 3
Sample Output
2
2

 

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

int main()
{
    int t;
    cin >> t;
    while(t--) {
        int n,m;
        cin >> n >> m;
        int matrix[n][n];
        int cnt = 0;
        for(int i = 0 ; i < n ; i++) {
            for(int j = 0 ; j < n ; j++) {
                if((i+1) * (j+1) == m)
                    cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
4.字符串处理
问题描述
有以下三种操作。
1 COPY l r 0<=l<=r<n ), n 代表 s 串的长度。这个表示将 s 串从 l r 的序列复制到
剪贴板 t 里面,覆盖 t 字符串。
例如 s abcde t pqr
执行 COPY 1 2 变为
s abcde t bc
2 CUT l r 0<=l<=r<n ), n 代表 s 串的长度。这个表示将 s 串从 l r 的序列剪切到
剪贴板 t 里面 ( 删除 s 串中的 l r 的序列 ) ,覆盖 t 字符串。
例如 s abcde t pqr
执行 CUT 1 2 变为
s ade t bc
3 PASTE p 0<=p<n ), n 代表 s 串的长度。这个表示将 t 串插入到 s p 位置的后
面。 t 保持不变。
例如 s abcde t pqr 执行 PASTE 1 变为
s abpqrcde t pqr
Input
输入正整数 N ,表示 N 例测试。首先给你 s 串,再给你一个 m ,然后给你 m 个操作。
Output
对每个操作,输出操作后的 s 串。
Sample Input
abcde
5
CUT 1 2
COPY 0 1
PASTE 1
PASTE 1
CUT 1 3
Sample Output
ade
ade
adade
adadade
aade

        常用字符串函数还是不咋记得住……还是用少了,考前必看。

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

int main()
{
    int t;
    cin >> t;
    while(t--) {
        string s,command,t;
        cin >> s;
        int n;
        cin >> n;
        for(int i = 0 ; i < n ; i++) {
            cin >> command;
            if(command == "COPY") {
                int l,r;
                cin >> l >> r;
                t = s.substr(l,r-l+1);  // 还是字符串函数不熟..
            }else if(command == "CUT") {
                int l,r;
                cin >> l >> r;
                t = s.substr(l,r-l+1);
                s = s.erase(l,r-l+1);
            } else {
                int p;
                cin >> p;
                s = s.insert(p+1,t);
            }
            cout << s << endl;
        }
    }
    return 0;
}

 2018 计算机院/软件院

1.二进制数字调转
题目描述:一个2^32的数字n,将其转换成二进制数,再倒转,求倒转的二进制数对应的十进制数。
例如:123
0000 0000 0000 0000 0000 0000 0111 1011
1101 11100000 0000 0000 0000 0000 0000
3724541952
举例:
输入:123
输出:3724541952
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    long long x;
    cin >> x;
    int a[32] = {0};
    int cnt = 0;
    while(x != 0) {
        a[cnt++] = x % 2;
        x /= 2;
    }
    long long res = 0;
    for(int i = 0 ; i < 32 ; i++) {
        res = res * 2 + a[i];
    }
    cout << res << endl;
    return 0;
}

 2.输出数字

题目描述:不同数字的输出形状如下:黑色部分是1,白色部分是0。

输入:长度为1-20的字符串

输出:0和1组合的数字形状,

举例:

输入:01

输出:111001

101001

101001

101001

111001

        注意最后是横向输出,即先输出每个数字的第一行,再依次输入后面的2、3、4、5行。

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

const int N = 20;

// 定义0~9的点阵表示
int a0[5][3]= {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};
int a1[5][3]= {{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,0,1}};
int a2[5][3]= {{1,1,1},{0,0,1},{1,1,1},{1,0,0},{1,1,1}};
int a3[5][3]= {{1,1,1},{0,0,1},{1,1,1},{0,0,1},{1,1,1}};
int a4[5][3]= {{1,0,1},{1,0,1},{1,1,1},{0,0,1},{0,0,1}};
int a5[5][3]= {{1,1,1},{1,0,0},{1,1,1},{0,0,1},{1,1,1}};
int a6[5][3]= {{1,1,1},{1,0,0},{1,1,1},{1,0,1},{1,1,1}};
int a7[5][3]= {{1,1,1},{0,0,1},{0,0,1},{0,0,1},{0,0,1}};
int a8[5][3]= {{1,1,1},{1,0,1},{1,1,1},{1,0,1},{1,1,1}};
int a9[5][3]= {{1,1,1},{1,0,1},{1,1,1},{0,0,1},{1,1,1}};



int main()
{
    string str;
    cin >> str;
    int t;
    int a[N];
    int cnt = 0;
    for(int i = 0 ; i < str.size() ; i++) {
        t = str[i] - '0';
        a[cnt++] = t;  // 把每个数字存入数组
    }
    for(int i = 0 ; i < 5 ; i++) {
        for(int j = 0 ; j < str.size() ; j++) {
            if(a[j] == 0) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a0[i][k];
                }
            } else if(a[j] == 1) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a1[i][k];
                }
            } else if(a[j] == 2) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a2[i][k];
                }
            } else if(a[j] == 3) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a3[i][k];
                }
            } else if(a[j] == 4) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a4[i][k];
                }
            } else if(a[j] == 5) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a5[i][k];
                }
            } else if(a[j] == 6) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a6[i][k];
                }
            } else if(a[j] == 7) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a7[i][k];
                }
            } else if(a[j] == 8) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a8[i][k];
                }
            } else if(a[j] == 9) {
                for(int k = 0 ; k < 3 ; k++) {
                    cout << a9[i][k];
                }
            }
        }
        cout << endl;
    }


    return 0;
}

3.发财数

题目描述:一个数字如果是由8个及8个以上的质因数乘积而成的数为发财数

输入:数字n

输出:10000以内的第几个发财数

举例:输入:1

输出:256 

        直接看佬的思路:2018年北邮计算机院复试上机题目_反身而诚、的博客-CSDN博客

(1)要求第任意个发财数,可预先求出所有的发财数保存在数组中,题目需要第几个发财数直接取即可,所以发财数的数量应该不超过10^5;
(2)首先本地上不设发财数的数量上限,计算尽可能多的发财数,然后取第10000个发财数,发现其值为330912,所以本题可直接取上限为400000;
(3)打印素数表最快的方法是欧拉线性筛法,直接写上线性筛模板;
(4)判断x是否是发财数时,用x除最小的素数,每除一次计数自增,直到除到自身是素数并且计数值小于8。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值