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 计算机院
问题描述给你一个长度为 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 Input220 051 1 1 2 1Sample Output816
#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你需要输出 1111110Input输入正整数 N ,表示 N 例测试。接着输入 N 组数据,每组输入经过 HDLC 处理过的 01 序列(长度小于 100 )。Output对每组输入数据,输出 HDLC 处理前的 01 序列。Sample Input2111110101111100Sample Output1111110111110
#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 32 4 63 6 9即 a[i][j](1<=i<=n,1<=j<=n)=i*j 。问一个这样 n*n 的矩阵里面,里面 m 出现的次数。例如 n 为 3,m 为 6.那么出现的次数就是 2Input输入正整数 N ,表示 N 例测试( N<=20 )。接着输入 n ( n<=10^5 ), m ( <=10^9 )。Output对每组输入数据,输出 m 出现的次数。Sample Input23 6 3 3Sample Output22
#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;
}
问题描述有以下三种操作。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 为 bc2 ) 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 为 bc3 ) PASTE p ( 0<=p<n ), n 代表 s 串的长度。这个表示将 t 串插入到 s 串 p 位置的后面。 t 保持不变。例如 s 为 abcde t 为 pqr 执行 PASTE 1 变为s 为 abpqrcde t 为 pqrInput输入正整数 N ,表示 N 例测试。首先给你 s 串,再给你一个 m ,然后给你 m 个操作。Output对每个操作,输出操作后的 s 串。Sample Inputabcde5CUT 1 2COPY 0 1PASTE 1PASTE 1CUT 1 3Sample Outputadeadeadadeadadadeaade
常用字符串函数还是不咋记得住……还是用少了,考前必看。
#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 计算机院/软件院
#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年北邮计算机院复试上机题目_反身而诚、的博客-优快云博客
(1)要求第任意个发财数,可预先求出所有的发财数保存在数组中,题目需要第几个发财数直接取即可,所以发财数的数量应该不超过10^5;
(2)首先本地上不设发财数的数量上限,计算尽可能多的发财数,然后取第10000个发财数,发现其值为330912,所以本题可直接取上限为400000;
(3)打印素数表最快的方法是欧拉线性筛法,直接写上线性筛模板;
(4)判断x是否是发财数时,用x除最小的素数,每除一次计数自增,直到除到自身是素数并且计数值小于8。