算式转移

问题描述

给出一个仅包含加减乘除四种运算符的算式(不含括号),如1+2*3/4,在保持运算符顺序不变的情况下,现在可以进行若干次如下操作:如果交换相邻的两个数,表达式值不变,那么你就可以交换这两个数。
现在你可以进行人一次操作,使得算式的数字序列字典序最小,然后输出结果,数字之间的字典序定义为若 a < b 则 a 的字典序小于 b。

输入描述

第一行包含一个整数n,表示算式的长度,即包含n个数字和n-1个运算符。(1<=n<=100000)
第二行包含一个含有n个非0整数和n-1运算符的算式,整数与运算符用空格隔开,运算符包含“+,-,*,/”,整数的绝对值不超过1000。

输出描述

按要求输出字典序最小的表达式,数字与符号直接用空格隔开。

实例1

输入
6
3 + 2 + 1 + -4 * -5 + 1
输出
1 + 2 + 3 + -5 * -4 + 1

问题分析

考点:冒泡排序, 对运算符顺序的理解
第一点: 题目中的相邻两个元素的互换排序,第一想到的就是冒泡排序。
冒泡排序:
比较相邻元素,如果反序,则进行交换。

void BubbleSort(int arr[], int n)
{
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

第二点: 考虑算式的运算的,例如 ‘a’ 5 ‘b’ 4 ‘c’ 3。想对5和4进行交换,我们分别考虑5 ‘b’ 4 在’b’分别为’ + ', ’ - ', ’ * ', ’ \ '时,‘a’ 和 ‘c’ 满足什么条件下可以进行交换。

  • 当 ‘b’ = ’ + ’ 时,满足’a’ = ‘+’ 且 ‘c’ != ‘*’ or ‘/’;

  • 当 ‘b’ = ’ - ’ 时,满足’a’ = ‘-’ 且 ‘c’ != ‘*’ or ‘/’;

  • 当 ‘b’ = ’ * ’ 时,满足’a’ = ‘+’ or ‘-’ or ‘*’, ‘c’ = ‘any’;

  • 当 ‘b’ = ’ / ’ 时,满足’a’ = ‘/’ , ‘c’ = 'any;

  • [ ] Concern:有没有可能即使破坏了算式的运算法则,进行数据交换,但最后的算式的值不变,这样只能用暴力求解了。

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>

using namespace std;

bool checkOperator (char a, char b, char c)
{
        // E.g. 'a' 5 'b' 4 'c' 3
        // And we consider separately when 'b' is equal to '+' '-' '*' '/'

        // When 'b' = '+'; 'a' must = '+' also, and 'c' must != '*' or '/'
        // e.g. + 5 + 4 + 3;
        //      + 5 + 4 - 3;
        //      if c = '*' or '/', can't switch 5 and 4
        if (b == '+' && a == '+' && (c == '+' || c == '-'))
                return true;
        // When 'b' = '-'; 'a' must = '-' also, and 'c' must != '*' or '/' 
        // e.g. - 5 - 4 + 3;
        //      - 5 - 4 - 3;
        //              if c = '*' or '/', can't switch 5 and 4
        else if (b == '-' && a == '-' && (c == '+' || c == '-'))
                return true;
        // When 'b' = '*'; 'a' = '+' or '-' or '*'; 'c' can = 'any' 
        // e.g. * 5 * 4 any 3;
        //      + 5 * 4 any 3;
        //      - 5 * 4 any 3;
        //      if a = '/', can't switch 5 and 4 
        else if (b == '*' && a != '/')
                return true;
        // When 'b' = '/'; 'a' must = '/'; and 'c' can = 'any'
        // e.g. '/' 5 '/'4 any 3
        //              if a != '/', can't switch 5 and 4
        else if (b == '/' && a == '/')
                return true;
        else
                return false;
}

int main()
{
        int n = 0;

        cin >> n;

        vector <int> S(n, 0);
        vector <char> C(n+1, '+');

        for (int i = 1; i < n + n; i++)
        {
                if (i % 2 == 0)
                        cin >> C[i/2];
                else
                        cin >> S[(i -1)/2];
        }
/*
        for ( int i = 0; i < n; i ++)
        {
                cout<<S[i] << " " ;
                cout<<C[i] << " " ;
        }

*/
#if 1
        for (int i = 0; i < n - 1; i ++)
        {
                for (int j = 0; j < n - i -1; j++)
                {
                        if (S[j] > S[j+1] && checkOperator(C[j], C[j+1], C[j+2])) {
                                int temp = S[j];
                                S[j] = S[j+1];
                                S[j+1] = temp;
                        }
                }
        }
#endif
        for ( int i = 0; i < n; i ++)
        {
                cout<<S[i] << " " ;
                if (i < n-1)
                        cout<<C[i+1] << " " ;
        }
        cout << endl;

        return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include<stdlib.h> struct four { double a; struct four *next; //定义结构体,作为链表的节点. }; void main() { double sum(void); //函数声明. 该函数返回等式的计算结果. 有优先级的运算符号在函数内部先进行计算。 double sum1; printf("请输入等式,以 '=' 结束, 例如“ 2*2*3-2/2= ” 结果将自动保留六位有效数字\n"); sum1=sum(); printf("该等式的结果为:\t%f\n\n",sum1); } double sum(void) { struct four *head,*pnew,*ptail,*p,*q; //结构体成员. char ah; double s=0,last; //last作为 pnew->a 的前一个数值. int j=1; q=(struct four *)malloc(sizeof(struct four)); scanf("%lf%c",&q->a,&ah); last=q->a; while(j==1 && ah!='=') //头节点的建立. { switch(ah) //对运算符号的优先级进行选择,优先级高的先进行计算. { case '+': j=0; continue; break; case '-': j=0; continue; break; case '*': q=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&q->a); q->a=last*q->a; break; case '/': q=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&q->a); q->a=last/q->a; break; default: printf("Error!\n"); //当运算符号出错时的处理. exit(0); } last=q->a; scanf("%c",&ah); } pnew=(struct four *)malloc(sizeof(struct four)); pnew->a=q->a; //将头节点的信息传递给 head 和 ptail. head=ptail=pnew; while(ah!='=') //接下来节点的建立. { pnew=(struct four *)malloc(sizeof(struct four)); scanf("%lf",&pnew->a); switch(ah) { case '*': pnew->a=last*pnew->a; break; case '/': pnew->a=last/pnew->a; break; case '+': break; case '-': pnew->a=-pnew->a;break; default: printf("Error!\n"); //当运算符号出错时的处理. exit(0); } scanf("%c",&ah); if(ah=='-' || ah=='+'|| ah=='=') //将值进行传递 ptail->next=pnew. { ptail->next=pnew; ptail=pnew; } last=pnew->a; } ptail->next=NULL; p=head; while(p!=NULL) //各个节点数值相加的结果,有优先级符号的已经先计算了. { s=s+(p->a); p=p->next; } return s; //返回运算结果. }
PyTorch 是一个基于Python的机器学习库,它提供了许多用于开发深度学习模型的工具和算法。在算式识别中,PyTorch 可以用于训练一个神经网络模型,对输入的算式进行识别和解析。 算式识别是指将输入的数学算式转换为计算机可识别的形式,通常包括识别算式的各个元素(如数字、运算符)和确定它们之间的关系。PyTorch 可以通过使用多层的神经网络模型来实现算式识别。 首先,需要准备一个合适的训练数据集,包括正例(正确的算式)和负例(错误的算式)。然后,利用PyTorch提供的工具和函数,构建一个神经网络模型。可以选择使用卷积神经网络(CNN)或循环神经网络(RNN)等结构。 训练过程中,需要先将数据集划分为训练集和测试集。通过将训练集输入到神经网络中,进行反向传播和优化,使网络能够逐渐学习到正确的算式识别方式。在每次迭代中,可以使用损失函数来评估模型的性能,并根据损失函数的结果调整模型的参数。 完成网络训练后,可以使用测试集来验证模型在未知数据上的表现。将测试集输入到训练好的模型中,通过模型的输出来进行算式识别。 通过PyTorch提供的函数和工具,我们可以实现对算式的自动识别和解析。这样就可以将输入的算式转换为计算机能够理解和处理的形式,从而实现更便捷和高效的数学计算和问题求解。 总而言之,PyTorch 可以用于算式识别,通过训练神经网络模型来实现对输入算式的自动识别和解析。这样可以方便地将算式转换为计算机可理解的形式,为数学计算和问题求解提供支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值