暴力STL

 

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

 

#include<list>
#include<stdio.h>
using namespace std;
const int N=10005;
list<int>all[N];
int T,a,b,id,num;char s[N];
int main(){
    for(scanf("%d",&T);T--;){
        scanf("%s",s);
        switch(s[0]){
            case 'n':scanf("%*d");break;
            case 'a':scanf("%d%d",&id,&num);
                    all[id].push_back(num);
                    all[id].sort();
                    break;
            case 'o':
                    scanf("%d",&id);
                    for(int &x:all[id]) printf("%d ",x);puts("");
                    break;
            case 'm':scanf("%d%d",&a,&b);
                    all[a].merge(all[b]);
                    all[id].sort();
                    break;
            case 'u':
                    scanf("%d",&id);
                    all[id].unique();
                    break;
        }
    }
    return 0;
}

 

3340: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位。

#include<set>
#include<stack>
#include<iostream>
#include<math.h>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define getab a=s.top();s.pop();b=s.top();s.pop();
using namespace std;
typedef double real;
const int N=5005;

stack<real>s;
multiset<real>ans;
int n,cnt;real res,x,a,b;
void calc(char c){
    a=s.top();s.pop();b=s.top();s.pop();
    switch(c){
        case '+':res=b+a;break;
        case '-':res=b-a;break;
        case '*':res=b*a;break;
        case '/':res=b/a;break;
        case '^':res=pow(b,a);break;
    };
    s.push(res);    
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf",&x),ans.insert(x);
    for(string str;cin>>str;){
        switch(str[0]){
            case '=':res=s.top();
                     for(;!s.empty();s.pop());
                     printf("%.6e\n",res);
                     ans.erase(ans.begin());ans.insert(res);
                     break;
            case '+':case '-':case '*':case '/':case '^':calc(str[0]);
                                     break;
            default:
                    res=atof(str.c_str());
                    s.push(res);
                    break;
            
        }
    }
    puts("");
    for(auto &y:ans){
        printf("%.6e ",y);
        if(++cnt%10==0) puts("");
    }
    return 0;
}
RE代码 原因未知

 

 

#include<set>
#include<stack>
#include<iostream>
#include<math.h>
typedef double real;
using namespace std;
stack<real>stk;
multiset<real>ans;
int n,cnt;real x,num,num1,num2,re;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf",&x),ans.insert(x);
    for(string sign;cin>>sign;) {
        if(sign=="=") {
            num=stk.top();stk.pop();
            printf("%e\n", num);
            ans.erase(ans.begin());
            ans.insert(num);
        }
        else if(sign=="+"||sign=="-"||sign=="*"||sign=="/"||sign=="^"){
            num1=stk.top();stk.pop();
            num2=stk.top();stk.pop();
            if(sign=="+") re=num2+num1;else 
            if(sign=="-") re=num2-num1;else 
            if(sign=="*") re=num2*num1;else 
            if(sign=="/") re=num2/num1;else 
            if(sign=="^") re=pow(num2, num1);
            stk.push(re);
        }
        else{
            num=atof(sign.c_str());
            stk.push(num);
        }
    }
    puts("");
    for(auto &y:ans){
        printf("%e ", y);
        if(++cnt%10==0) puts("");
    }
    return 0;
}

 

-------

 

posted @ 2020-04-22 22:15  神犇(shenben)  阅读(142)  评论(0编辑  收藏  举报