5.1练习

四则运算

输入一个表达式(用字符串表示),求这个表达式的值。

保证字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。且表达式一定合法。

数据范围:表达式计算结果和过程中满足∣val∣≤1000  ,字符串长度满足1≤n≤1000 

输入描述:

输入一个算术表达式

输出描述:

得到计算结果

示例1

输入:

3+2*{1+2*[-4/(8-6)+7]}

输出:

25

双栈方法:一个存储数字,一个存储符号

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

void compute(stack<int>& st1, stack<char>& st2) {
    //st1是数字栈,st2是运算符栈
    int a = st1.top();
    st1.pop();
    int b = st1.top();
    st1.pop();
    char op = st2.top();
    st2.pop();
    switch (op) {
        case '+':
            b = a + b;
            break;
        case '-':
            b = b - a;
            break;
        case '*':
            b = b * a;
            break;
        case '/':
            b = b / a;
            break;
    }
    st1.push(b);
}

//比较优先级,带括号的优先级更高
bool priority(char m, char n) {
    if (m == '(')
        return false;
    else if ((m == '+' || m == '-') && (n == '*' || n == '/'))
        return false;
    return true;
}

int main() {
    string s;
    while (cin >> s) {
        stack<int> st1;
        stack<char> st2;
        st2.push('(');
        s+=')';
        bool flag=false;
        for(int i=0;i<s.length();i++)
        {
            //如果遇见左括号
            if(s[i]=='('||s[i]=='['||s[i]=='{')
            {
                st2.push('(');
            }
            //如果遇见右括号
            else if(s[i]==')'||s[i]==']'||s[i]=='}')
            {
                while(st2.top()!='(')
                    compute(st1,st2);
                st2.pop();
            }
            else if(flag)
            {
                //如果是其他运算符:先比较运算符,再进行运算
                while(priority(st2.top(), s[i]))
                    compute(st1,st2);
                st2.push(s[i]);
                flag=false;
            }
            else {
                //如果是数字
                int j=i;
                if(s[j]=='-'|| s[j]=='+')
                    i++;
                while(isdigit(s[i]))
                    i++;
                string temp=s.substr(j,i-j);
                st1.push(stoi(temp));;
                i--;
                flag=true;
            }
            
        }
        cout<<st1.top()<<endl;

    }
    return 0;
}

 计算字符串的编辑距离

Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。编辑距离的算法是首先由俄国科学家 Levenshtein 提出的,故又叫 Levenshtein Distance 。

例如:

字符串A: abcdefg

字符串B: abcdef

通过增加或是删掉字符 ”g” 的方式达到目的。这两种方案都需要一次操作。把这个操作所需要的次数定义为两个字符串的距离。

要求:

给定任意两个字符串,写出一个算法计算它们的编辑距离。

数据范围:给定的字符串长度满足 1≤len(str)≤1000 

输入描述:

每组用例一共2行,为输入的两个字符串

输出描述:

每组用例输出一行,代表字符串的距离

示例1

输入:

abcdefg
abcdef

输出:

1
#include <iostream>
#include <vector>
using namespace std;

int minOperation(string &s1,string &s2)
{
    int m=s1.size(),n=s2.size();
    if(m==0 || n==0)
        return n+m;
    vector<vector<int>> dp(m+1,vector<int>(n+1,0));
    //初始化
    for(int i=0;i<=m;i++)
    {
        dp[i][0]=i;
    }
    for(int j=0;j<=n;j++)
    {
        dp[0][j]=j;
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(s1[i-1]==s2[j-1])
                dp[i][j]=dp[i-1][j-1];
            else
                dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
        }
    }
    return dp[m][n];
}
int main() 
{
    string s1,s2;
    while(cin>>s1>>s2)
    {
        int res=minOperation(s1, s2);
        cout<<res<<endl;
    }
    return 0;

}

挑7

输出 1到n之间 的与 7 有关数字的个数。

一个数与7有关是指这个数是 7 的倍数,或者是包含 7 的数字(如 17 ,27 ,37 ... 70 ,71 ,72 ,73...)

数据范围:1≤n≤30000 

输入描述:

一个正整数 n 。( n 不大于 30000 )

输出描述:

一个整数,表示1到n之间的与7有关的数字个数。

示例1

输入:

20

输出:

3

说明:

输入20,1到20之间有关的数字包括7,14,17共3个。
#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        int cnt = 0;
        string str;
        for (int i = 1; i <= n; i++) {
            if (i % 7 == 0) {
                cnt++;
                continue;
            } else {
                str = to_string(i);
                if (str.find('7') != str.npos)
                    cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

 高精度整数加法

输入两个用字符串 str 表示的整数,求它们所表示的数之和。

数据范围:1≤len(str)≤10000 

输入描述:

输入两个字符串。保证字符串只含有'0'~'9'字符

输出描述:

输出求和后的结果

示例1

输入:

9876543210
1234567890

输出:

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

int main() 
{
    string s1,s2;
    string ans;
    while(cin>>s1>>s2)
    {
        stack<char> st1;
        stack<char> st2;
        for(char c:s1)
            st1.push(c);
        for(char c:s2)
            st2.push(c);
        int carry=0;//进位
        while(!st1.empty() || !st2.empty())
        {
            int temp=0;
            if(!st1.empty())
            {
                temp+=st1.top()-'0';
                st1.pop();
            }
            if(!st2.empty())
            {
                temp+=st2.top()-'0';
                st2.pop();
            }
            ans+=(temp+carry)%10+'0';//结果
            carry=(temp+carry)/10;//进位
            //如果有进位且已经计算完
            if(carry==1 && st1.empty() && st2.empty())
                ans+='1';

        }
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值