滴滴笔试--算术转移(20190827)

探讨了一种算法,用于在保持运算符顺序不变的前提下,通过交换相邻数字以获得字典序最小的算式表达。该算法适用于仅含加减乘除的算式,通过对数字和运算符的分段处理及特定规则应用,实现了表达式的优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

题目描述:
给出一个仅包含加减乘除四种运算符的算式(不含括号),如1+2*3/4,在保持运算符顺序不变的情况下,现在你可以进行若干次如下操作:如果交换相邻的两个数,表达式值不变,那么你就可以交换这两个数。

 

现在你可以进行任意次操作,使得算式的数字序列字典序最小,然后输出结果,数字之间的字典序定义为若a<b则a的字典序小于b。

 

输入
第一行包含一个整数n,表示算式的长度,即包含n个数字和n-1个运算符。(1≤n≤100000)。

 

第二行包含一个含有n个非0整数和n-1个运算符的算式,整数与运算符用空格隔开,运算符包括“+,-,*,/”,整数的绝对值不超过1000。

 

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

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

思路:参考自
先用两个数组来存储数字(opNum)和这个数字之前的操作符(opChar)。其中ops第一个元素固定是'+',因为第一个数没有符号。例如样例就会得到如下的两个数组:

索引 0 1 2 3 4 5
nums 3 2 1 -4 -5 1
ops + + + + * +

接着用两个指针 l 和 r,一段一段地找ops中连续的操作符,例如样例中就会得出“l=0,r=3; l=4,r=4; l=5,r=5”三段。
接着根据一套规则来确定 l 和 r 区间内哪些元素是需要进行排序,以实现字典序最小:
  • 如果ops[l]是加号或减号
  1. 这段区间下一个操作符是'*'或者‘/’,那么要排序的区间是[l, r-1]; 想想类似5+4+3+2+1*5的情况
  2. 这段区间下一个操作符是'-',那么要排序的区间是[l, r];想想类似5+4+3+2+1-5的情况
  3. 这里不需要考虑区间前一个操作符是*、/,-,要排序的区间肯定是[l, r],可参考5*4+1+2+3的情况  (故2,3归为else语句即可)
  • ops[l]是乘号
  1. 区间前一个操作符是'+'或者‘-’,那么要排序的区间是[l-1, r];想想类似1+5*4*3*2*1的情况
  2. 区间前一个操作符是‘/’,那么要排序的区间是[l, r];想想类似1/5*4*3*2*1的情况
  • ops[l]是除号
  1. 不论什么情况,要排序的区间都是[l, r];
以上遍历一套,就可以得到答案了
代码:
#include<iostream>
#include<vector>
#include<string> 
#include<algorithm>
using namespace std;

int Partition(vector<int> &nums, int l, int r){
    int pivot = nums[l];
    while(l<r){
        while(l<r && pivot<=nums[r]) 
            --r;
        nums[l] = nums[r];
        while(l<r && pivot>=nums[l]) 
            ++l;
        nums[r] = nums[l];
    }
    nums[l] = pivot;
    return l;
}

void QuickSort(vector<int> &nums, int l, int r){
    if(l<r){
        int mid = Partition(nums, l, r);
        QuickSort(nums, l, mid-1);
        QuickSort(nums, mid+1, r);
    }
}

int main()
{
    int n;
    cin>>n;
    vector<int> opNum(n, 0);
    vector<char> opChar(n, '+');
    for(int i=0;i<n-1;i++){
        cin>>opNum[i];
        cin>>opChar[i+1];
    }
    cin>>opNum[n-1];
    

    int l=0, r=0;
    while(r<n){
        while( r<n && opChar[l]==opChar[r]){
            r++;
        }
        r--;
        //cout<<l<<" "<<r<<endl;
        if(opChar[l]=='*' ){
            if(l-1>=0 && (opChar[l-1]=='+' || opChar[l-1]=='-'))
                QuickSort(opNum, l-1, r);
            else
                QuickSort(opNum, l, r);
        }else if(opChar[l]=='+' || opChar[l]=='-'){
             if(r+1<n-1 && (opChar[r+1]=='*' || opChar[r+1]=='|'))
                 QuickSort(opNum, l, r-1);
             else
                 QuickSort(opNum, l, r);
                 
        }else if(opChar[l]=='/'){
            QuickSort(opNum, l, r);
        }
        r++;
        l = r;
    }
    for(int i=0;i<n-1;i++){
        cout<<opNum[i]<<" ";    
        cout<<opChar[i+1]<<" ";
    }
    cout<<opNum[n-1]<<endl;
    return 0;
    //3 + 2 + 1 + -4 * -5 + 1  ---> 1 + 2 + 3 + -5 * -4 + 1
}

 

#include #include 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; //返回运算结果. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值