2017 程序设计实习之C++部分作业题汇总 - H:STL 容器与算法

题目来源:2017 程序设计实习之C++部分作业题汇总

1、H01:List

总时间限制: 4000ms 内存限制: 65536kB
描述
写一个程序完成以下命令:
new id ——新建一个指定编号为id的序列(id<10000)
add id num——向编号为id的序列加入整数num
merge id1 id2——合并序列id1和id2中的数,并将id2清空
unique id——去掉序列id中重复的元素
out id ——从小到大输出编号为id的序列中的元素,以空格隔开

输入
第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。
输出
按题目要求输出。
样例输入

16
new 1
new 2
add 1 1
add 1 2
add 1 3
add 2 1
add 2 2
add 2 3
add 2 4
out 1
out 2
merge 1 2
out 1
out 2
unique 1
out 1

样例输出

1 2 3
1 2 3 4
1 1 2 2 3 3 4

1 2 3 4

code for List

#include <iostream>
#include <algorithm>
#include <string>
#include <list>
using namespace std;
list<int> lst[10005];
template<class T>
void Print(T b,T e)
{
    while (b != e)
        cout << *b++ << " ";
    cout << endl;
}
int main()
{
    int n, id, id_1, id_2, num;
    cin >> n;
    string s;

    while (n--)
    {
        cin >> s;
        if (s == "new") {   cin >> id; }
        else if (s == "add") {  
            cin >> id >> num;
            lst[id].push_back(num);
        }
        else if (s == "out") {
            cin >> id;
            lst[id].sort();
            Print(lst[id].begin(), lst[id].end());
        }
        else if (s == "merge") {
            cin >> id_1 >> id_2;
            lst[id_1].merge(lst[id_2]);
        }
        else if (s == "unique") {
            cin >> id;
            lst[id].sort();
            lst[id].unique();
        }
    }
    return 0;
}

2、H02:RPN Calculator

总时间限制: 1000ms 内存限制: 10000kB
描述
Reverse Polish notation (or just RPN) by analogy with the related Polish notation, a prefix notation introduced in 1920 by the Polish mathematician Jan Łukasiewicz, is a mathematical notation wherein every operator follows all of its operands. It is also known as Postfix notation.

In Reverse Polish notation the operators follow their operands; for instance, to add three and four one would write “3 4 +” rather than “3 + 4”. If there are multiple operations, the operator is given immediately after its second operand; so the expression written “3 − 4 + 5” in conventional infix notation would be written “3 4 − 5 +” in RPN: first subtract 4 from 3, then add 5 to that. An advantage of RPN is that it obviates the need for parentheses that are required by infix. While “3 − 4 * 5” can also be written “3 − (4 * 5)”, that means something quite different from “(3 − 4) * 5”, and only the parentheses disambiguate the two meanings. In postfix, the former would be written “3 4 5 * −”, which unambiguously means “3 (4 5 *) −”.

You were asked to design a simple RPN calculator, which will support the “+”, “-“, “*”, “/”(the absolute value of the divisor will not less then 10^-9) and “^”(power operator, if the base number b<=0, the exponential e must be a positive integer not greater than 10^9) operators. You can assume all the numbers during the calculation can fit into a double-precision floating point number.

In addition, our calculator has some memory. Each time we calculate an expression, the smallest number in the memory will be erased, and replace it with the value of the expression.

输入
The first line contains an integer n, which is the memory size of our calculator.

From the second line, we will give n numbers, which is the initial value of the memory. each line except last will have 10 numbers.

And then each line has a valid RPN expression we previously described, end with “=”, which is the command for calculation. Each term will no longer than 20 characters.

输出
For each expression, output the value of it in a line.
And then output an empty line to separate the two parts.
At last, output the all the numbers in memory, in increasing order, 10 numbers per line.

Each number should be formatted in scientific notation with 6 digits after decimal point and 2 digits of exponential, such like “%e” format string of printf() function in C. The numbers in a line should be separated by a space.
样例输入

4
1e6 1e-6 0.001 1000
1 2 + 3 4 + * =
1 0.1 / 8 ^ =

样例输出
2.100000e+01
1.000000e+08

2.100000e+01 1.000000e+03 1.000000e+06 1.000000e+08
提示
Huge input, scanf() is recommended

%e格式输出在windows环境下指数部分为3位,在系统的测试环境下为2位。

code for RPN Calculator

#include <iostream>
#include <cstdlib>
#include <string>
#include <stack>
#include <cmath>
#include <set>
using namespace std;
int main()
{
    int n;
    cin >> n;
    double num;
    string s;
    multiset<double> mst;
    multiset<double> ::iterator aim;
    stack<double> st;
    for (int i = 1; i <= n; ++i)
    {
        cin >> num;
        mst.insert(num);
    }
    double x, y, re;
    // calculate the value of each expression
    while (cin >> s)
    {
        if (s == "+" || s == "-" || s == "*" || s == "/" || s == "^")
        {
            x = st.top();   st.pop();
            y = st.top();   st.pop();
            if (s == "+")   re = y + x;
            else if (s == "-")  re = y - x;
            else if (s == "*")  re = y * x;
            else if (s == "/")  re = y / x;
            else if (s == "^")  re = pow(y,x);
            st.push(re);
        }
        else if (s == "=")
        {
            re = st.top();  st.pop();
            printf("%e\n", re);
            aim = mst.begin();
            //Ref the description:
            //delete the least element in the memory first
            //then add the cur value of the exp into the memory
            mst.erase(aim);
            mst.insert(re);
        }
        else
            st.push(atof(s.c_str()));
    }
    //output the memory in the requsted order and format 
    printf("\n");
    int k = 1;
    aim = mst.begin();
    while (aim != mst.end())
    {
        if (k % 10 == 0)
            printf("%e\n", *aim++);
        else
            printf("%e ", *aim++);
        ++k;
    }
    printf("\n");
    return 0;
}

3、H03:Set

总时间限制: 5000ms 内存限制: 100000kB
描述
现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。
输入
第一行是一个整数n,表示命令数。0<=n<=100000。
后面n行命令,如Description中所述。
输出
共n行,每行按要求输出。
样例输入

7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1

样例输出

1
2
1 2
0 0
0
2
1 0

提示
Please use STL’s set and multiset to finish the task

code for Set


                
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值