数据结构与算法之栈与队列/字符串KMP/矩阵及稀疏矩阵问题题解 A进制转换 B 回文判断 C表达式求值 D基因配对 E稀疏矩阵转置

 A

将一个十进制整数转换成2进制或8进制或16进制等进制表示的整数(用栈实现)

用例1:

4
2
100

用例2

16
8
20

用例3

255
16
FF

说明:用例的第一行表示需要转换的十进制整数,第二行表示要转换的进制,第三行为转换结果

代码:

#include <iostream>
#include <stack>
#include <string>

using namespace std;
string decimalToBase(int decimal, int base) {
    stack<char> stack;
    string digits = "0123456789ABCDEF";

    while (decimal > 0) {
        int remainder = decimal % base;
        stack.push(digits[remainder]);
        decimal /= base;
    }

    string converted = "";
    while (!stack.empty()) {
        converted += stack.top();
        stack.pop();
    }

    return converted;
}

int main() {
    int base0, decimal0;
    cin >> decimal0 >> base0;
    cout << decimalToBase(decimal0, base0);
    return 0;
}

B

判断一个字符序列是不是回文,要求用栈和队列或双端队列完成。

“回文”是正向扫描和反向扫描都相同的字符序列。例如:ABCDEDCBA,123321。

用例1:

ABCDEDCBA
1

用例2

1212
0

注:每个用例的第一行表示输入的字符序列。 第二行表示结果,1表示输入的字符序列是回文,0表示输入的字符序列不是回文。

代码:

#include <iostream>
#include <deque>

using namespace std;

bool isPalindrome(string str) {
    deque<char> deque;

   
    for (char c : str) {
        deque.push_back(c);
    }

   
    while (deque.size() > 1) {
        if (deque.front() != deque.back()) {
            return false;
        }
        deque.pop_front();
        deque.pop_back();
    }

    return true;
}

int main() {
    string t;
    cin >> t;
    cout << isPalindrome(t);
    return 0;
}

C

整数表达式求值。表达式由整数、+、-、*、/、(、) 构成,请输入正确的整数表达式并求值。建议用栈的知识完成

用例1

((21+4)/5-3)
2

用例2

((21+4)/5-3)+(60*10)*6
3602

用例3

((21+4)/5-3)-1
1

用例4

((21+4)/5-3)-(1+2)
-1

注:每个用例的第一行表示输入的整数数值表达式序列。 第二行表示计算结果。

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

int calculate(int num1, int num2, char op) {
    switch (op) {
    case '+':
        return num1 + num2;
    case '-':
        return num1 - num2;
    case '*':
        return num1 * num2;
    case '/':
        return num1 / num2;
    default:
        return 0;
    }
}

int evaluateExpression(string expression) {
    stack<int> operands;
    stack<char> operators;

    for (int i = 0; i < expression.length(); i++) {
        if (expression[i] == ' ') {
            continue;
        }
        else if (isdigit(expression[i])) {
            int num = 0;
            while (i < expression.length() && isdigit(expression[i])) {
                num = num * 10 + (expression[i] - '0');
                i++;
            }
            operands.push(num);
            i--;  // 回退一步,因为for循环还会自增
        }
        else if (expression[i] == '(') {
            operators.push(expression[i]);
        }
        else if (expression[i] == ')') {
            while (!operators.empty() && operators.top() != '(') {
                int num2 = operands.top();
                operands.pop();
                int num1 = operands.top();
                operands.pop();
                char op = operators.top();
                operators.pop();
                int result = calculate(num1, num2, op);
                operands.push(result);
            }
            // 弹出左括号
            if (!operators.empty()) {
                operators.pop();
            }
        }
        else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/') {
            while (!operators.empty() && (operators.top() == '*' || operators.top() == '/')) {
                int num2 = operands.top();
                operands.pop();
                int num1 = operands.top();
                operands.pop();
                char op = operators.top();
                operators.pop();
                int result = calculate(num1, num2, op);
                operands.push(result);
            }
            operators.push(expression[i]);
        }
    }

    while (!operators.empty()) {
        int num2 = operands.top();
        operands.pop();
        int num1 = operands.top();
        operands.pop();
        char op = operators.top();
        operators.pop();
        int result = calculate(num1, num2, op);
        operands.push(result);
    }

    return operands.top();
}

int main() {
    string expression;
    getline(cin, expression);
    int result = evaluateExpression(expression);
    cout << result << endl;
    return 0;
}

D

已知n个人的供体基因库和1个人的受体基因,请利用朴素的字符串匹配算法或KMP算法等实现基因匹配。

用例1

3
TGAAGGTGATTAG
TTAAGGTATATTTATATT
GATTGGTTAGATATTG
TATAGA
2 7 66.67%

注:用例的第一行代表供体基因库的基因串数量n,n为≥1的整数,之后的n行代表基因库中的每个基因串。最后一行代表匹配结果。匹配结果的第一个整数表示和基因库中第几个基因串实现了最佳匹配。第二个整数表示匹配的点位,第三个数表示匹配度。匹配点位是指受体基因和基因库中某个基因串最高相似的第一次最高相似的起始位置p;匹配度是指受体基因和基因库中某个基因串的最高相似度,公式为从p开始连续相似的基因数/受体基因串的长度 *100%。相似度要求保留小数点后2位。如果匹配失败,则输出结果为:0 0 0.00%

代码:

#include <stdio.h>
#include <string.h>

int main()
{
	int n, i, j, a1, a2, l1, l2, max, k, t, f;
	double h, a3;
	scanf("%d", &n);
	char g1[n][100];
	char g2[100];
	for (int i = 0; i < n; i++)
	{
		scanf("%s", g1[i]);
	}
	scanf("%s", g2);
	a1 = -1;
	a2 = 0;
	a3 = 0.0;
	max = 0;
	for (i = 0; i < n; i++)
	{
		l1 = strlen(g1[i]);
		l2 = strlen(g2);
		for (j = 0; j < l1; j++)
		{
			t = 0;
			for (k = 0; k < l2 && k + j < l1; k++)
			{
				if (g2[k] == g1[i][j + k])
					t++;
				else
					break;

			}
			if (t > max)
			{
				max = t;
				f = j;
			}
		}
		h = (double)max / l2 * 100.0;
		if (h > a3)
		{
			a1 = i + 1;
			a2 = f + 1;
			a3 = h;
		}
	}
	if (a1 == -1)
	{
		printf("0 0 0.00%%\n");
	}
	else
	{
		printf("%d %d %.2lf%%\n", a1, a2, a3);
	}
	return 0;
}

E

稀疏矩阵转置。用三元组(行号,列号 ,元素值)的方式输入一个稀疏矩阵,输出该矩阵的转置结果。

输入用例1:

79 7 4
63 1 16170
26 5 3080
55 2 27334
52 2 18563

输出用例1:

7 79 4
1 63 16170
2 55 27334
2 52 18563
5 26 3080

注:输入和输出的第一行的三个整数分别代表矩阵的行数、列数及元素个数,其后各行为各个元素的三元组形式。且输出用例中各个元素按行号升序输出。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
using namespace std;
struct SparseMatrixElement {
    int row;
    int col;
    int value;
};

int main() {
    int rows, cols, numElements;
    std::cin >> rows >> cols >> numElements;

    std::vector<SparseMatrixElement> originalMatrix(numElements);
    std::vector<std::vector<SparseMatrixElement>> transposedMatrix(cols);

    for (int i = 0; i < numElements; ++i) {
        std::cin >> originalMatrix[i].row >> originalMatrix[i].col >> originalMatrix[i].value;
    }

    for (const SparseMatrixElement& element : originalMatrix) {
        transposedMatrix[element.col - 1].push_back({ element.col, element.row, element.value });
    }

    std::cout << cols << " " << rows << " " << numElements << std::endl;
    for (int col = 0; col < cols; ++col) {
        for (const SparseMatrixElement& element : transposedMatrix[col]) {
            std::cout << element.row << " " << element.col << " " << element.value << std::endl;
        }
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值