2023-24(1)数据结构上机考试(B卷)
发布时间: 2023-12-27 09:00:00截止时间: 2023-12-27 12:00:00当前时间:2023-12-27 11:46:53
介绍:
请在规定时间范围内提交代码
附件:
练习题:
463339. B(2)最大乘积 目前得分:100
454800. B(3)最短路径 目前得分:100
466157. B(1) 回文数 目前得分:100
B(1) 回文数
总提交数: 391次通过数: 37次通过率: 9.46%
内存限制: 10485760(BYTE)时间限制: 1000(MS)输入限制: 1000(行)输出限制: 1000(行)
题目描述
给定一个10进制正整数 x (int 表达范围内) ,如果 x 是一个回文整数,输出 true ;否则,输出 false 。
回文数是指正序(从左向右)和倒序(从右向左)值相等的整数,例如121,111,1等均为回文数。
输入的为错误数字则输出error
输入格式:
判断的文本
输出格式:
判断结果
样例输入输出
样例1
输入:
121
输出:
true
样例2
输入:
18
输出:
false
样例3
输入:
1
输出:
true
样例4
输入:
010
输出:
error
样例5
输入:
10
输出:
false
样例6
输入:
7a
输出:
error
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
//cout<<s.length();
for (int i = 0; i < s.length(); i++) {
if (s[i] > '9' || s[i] < '0') {
cout << "error";
return 0;
}
}
if (s.length() > 1 && s[0] == '0') {
cout << "error";
return 0;
}
for (int i = 0, j = s.length() - 1; i < s.length(), j >= 0; i++, j--) {
if (s[i] != s[j]) {
cout << "false";
return 0;
}
}
cout << "true";
}
B(2)最大乘积
总提交数: 266次通过数: 36次通过率: 13.53%
内存限制: 10485760(BYTE)时间限制: 1000(MS)输入限制: 1000(行)输出限制: 1000(行)
题目描述
对于一个整型数组nums,在数组中找出可以组成最大乘积的四个数字,并按值递增顺序输出。
最大的乘积范围在long表达以内。
输入样式:
先输入整数个数 n 4<=n<20
然后输入n个整数
输出:
4个大小递增的数字,中间使用一个空格分隔,末尾无空格。
结果可以保证唯一
样例输入输出
样例3
输入:
6
-5 -6 1 2 3 4
输出:
-6 -5 3 4
样例1
输入:
4
1 2 3 4
输出:
1 2 3 4
样例2
输入:
4
-1 -2 -3 4
输出:
-3 -2 -1 4
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<string>
using namespace std;
void sort(int*& num, int n) {
for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (num[j] > num[j + 1]) {
swap(num[j], num[j + 1]);
}
}
}
}
int main() {
int n;
cin >> n;
int* num = new int[n];
for (int i = 0; i < n; i++) {
cin >> num[i];
}
//sort
sort(num, n);
int num0 = 0;
int num1 = 0;
for (int i = 0; i < n; i++) {
if (num[i] == 0)num0++;
else if (num[i] > 0)num1++;
}
int numk = n - num0 - num1;
int* ans = new int[4];
int k = 0;
for (int count = 0, front = 0, back = n - 1; count < 2; count++) {
int tmp1 = num[front] * num[front + 1];
int tmp2 = num[back] * num[back - 1];
if (tmp1 >= tmp2) {
ans[k++] = num[front++];
ans[k++] = num[front++];
}
else {
ans[k++] = num[back--];
ans[k++] = num[back--];
}
}
sort(ans, 4);
for (int i = 0; i < 4; i++) {
cout << ans[i] << ' ';
}
}
B(3)最短路径
总提交数: 194次通过数: 37次通过率: 19.07%
内存限制: 10485760(BYTE)时间限制: 1000(MS)输入限制: 1000(行)输出限制: 1000(行)
题目描述
给定一个无向图,n 个顶点编号为 0 到 n-1 , e条边(包含两个顶点和一个权重),请求从一个顶点到另外一个顶点最短路径
输入格式:
先输入顶点和边的个数,1<n<20,1<e<40
再输入 e条带权重的边 ,输入样式为 v1 v2 cost ,即2个顶点编号和一个路径长度,其中cost为正整数。
最后输入两个顶点编号,start,end, start!=end
输出:
顶点 start到顶点end的路径,测试案例保证路径一定存在
样式如下:无空格
start->v?...->end
输入:
5 6
0 1 2
0 3 3
1 2 5
1 3 4
2 3 10
3 4 7
0 2
输出:
0->1->2
样例输入输出
样例1
输入:
5 6
0 1 2
0 3 3
1 2 5
1 3 4
2 3 10
3 4 7
0 2
输出:
0->1->2
#include<iostream>
#include<stack>
using namespace std;
int main() {
int n, e;
cin >> n >> e;
int** p = new int* [n];
for (int i = 0; i < n; i++) {
p[i] = new int[n];
}
int** edge = new int* [e];
for (int i = 0; i < e; i++) {
edge[i] = new int[3];
cin >> edge[i][0] >> edge[i][1] >> edge[i][2];
}
int* get = new int[n];
int* length = new int[n];
for (int i = 0; i < n; i++) {
get[i] = 0;
length[i] = 9999;
}
int* l = new int[n];
int front, end;
cin >> front >> end;
l[front] = front;
get[front] = 1;
length[front] = 0;
for (int u = 0; u < n - 1; u++) {
int f, v, mincost = 100000;
for (int i = 0; i < e; i++) {
if (get[edge[i][0]] == 1) {
if (get[edge[i][1]] == 0 && edge[i][2] + length[edge[i][0]] < mincost) {
v = edge[i][1];
f = edge[i][0];
mincost = edge[i][2] + length[edge[i][0]];
continue;
}
}
if (get[edge[i][1]] == 1) {
if (get[edge[i][0]] == 0 && edge[i][2] + length[edge[i][1]] < mincost) {
v = edge[i][0];
f = edge[i][1];
mincost = edge[i][2] + length[edge[i][1]];
}
}
}
get[v] = 1;
l[v] = f;
length[v] = mincost;
}
stack<int>s;
s.push(end);
while (l[end] != front) {
s.push(l[end]);
end = l[end];
}
cout << front;
while (!s.empty()) {
cout << "->";
cout << s.top();
s.pop();
}
}