STL模拟训练


前言

今日acm使用STL进行模拟训练,菜鸟代码,不足之处请斧正

一、表达式求值

Description
ACM队想做一个计算器,但是,他们要做的不仅仅是一计算一个A+B的计算器,他们想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他们来实现这个计算器吧。
比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)

Input
第一行输入一个整数n,共有n组测试数据(n<100)。
每组测试数据只有一行,是一个字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含±*/与小括号这几种符号。其中小括号可以嵌套使用。输入保证合法,也不会出现负数。(很简单的表达式求值哦,测试数据很水的)

Output
每组都输出该组运算式的运算结果,输出结果保留两位小数。

Sample Input Copy
2
1.000+2/4=
((1+2)*5+1)/4=
Sample Output Copy
Case #1: 1.50
Case #2: 4.00

#include <iostream>
#include <stack>
using namespace std;
stack<double> num;
stack<char> op;
char s[1010];
int cmp(char a) {
	if (a == '=')				   return 0;
	else if (a == '+' || a == '-') return 1;
	else if (a == '*' || a == '/') return 2;
	else return 0;
}
void calu(stack<double> &num, stack<char> &op) {
	double b = num.top();	num.pop();
	double a = num.top();	num.pop();
	switch (op.top()) {
	case '+':num.push(a + b); break;
	case '-':num.push(a - b); break;
	case '*':num.push(a * b); break;
	case '/':num.push(a / b); break;
	}
	op.pop();
}
int main()
{
	int t; scanf("%d", &t);
	while (t--) {
		cin >> s;
		for (int i = 0; s[i] != '\0'; i++) {
			if (isdigit(s[i])) {
				double temp = atof(&s[i]);

				num.push(temp);
				while (isdigit(s[i]) || s[i] == '.')
					i++;
				i--;
			}

			else {
				if (s[i] == '(') {
					op.push(s[i]);
				}else if (s[i]==')') {
					while (op.top() != '(')
						calu(num, op);
					op.pop();
				}

				else if (op.empty() || cmp(s[i]) > op.top()) {
					op.push(s[i]);
				}

				else if (!op.empty() && cmp(s[i]) <= op.top()) {
					while (!op.empty() && cmp(s[i]) <= op.top()) {
						calu(num, op);
					}
					op.push(s[i]);
				}
			}
		}
		printf("%.2lf\n", num.top());
		num.pop();
		op.pop();
	}
	return 0;
}

二、Unique Number

Description
Junhan特别喜欢排序,据传拥有排序小王子的他发明了一个神奇的算法,它不仅可以快速的排序,还可以去掉里面重复的数字。为了证明它算法的优越性,它设下擂台,广征天下豪杰,比拼去重排序。-

Input
测试数据包含多组,请处理到EOF结束。

每组测试数据第一行包含一个整数N ( 1 < = n < = 100000 ),第二行包含N个数ai ( -10^9 < = ai < = 10^9 )

Output
对于每组测试数据要求首先输出去重后所剩下的数字个数m,接下来输出从大到小排好序的m个数,每两个数之间以空格分隔,注意最后一个数字后面没有多余的空格。

Sample Input Copy
10
1 2 3 4 2 4 5 6 9 1
5
3 3 4 2 4
Sample Output Copy
7 9 6 5 4 3 2 1
3 4 3 2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
int a[100010];
int main()
{
    int t;
    while (~scanf("%d", &t))
    {
        for (int i = 0; i < t; i++)
            scanf("%d", &a[i]); 
        sort(a, a + t);
        int n = unique(a, a + t) - a;
        printf("%d ",n);
        for (int i = n - 1; i >= 0; i--)
        {
            if (i == 0) printf("%d", a[i]);
            else printf("%d ", a[i]);
        }
        printf("\n");
    }
}

三、去掉双斜杠注释

Description
将C程序代码中的双斜杠注释去掉。

Input
输入数据中含有一些符合C++语法的代码行。需要说明的是,为了方便编程,规定双斜杠注释内容不含有双引号,源程序中没空行。保证每行不超过1000个字符

Output
输出不含有双斜杠注释的C++代码,除了注释代码之外,原语句行格式不变。

Sample Input Copy
//======================
// simplest program
//======================
#include
using namespace std;
//----------------------
int main(){
cout<<”hello world!\n”;
}//---------------------
Sample Output Copy
#include
using namespace std;
int main(){
cout<<”hello world!\n”;
}
HINT
本题每一行数据的输出末尾不含有多余的空格字符!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int main()
{
    int l;
    char a[1000];
    int flag = 0;
    vector<char> bb;
    while (gets(a)) {
        flag = 0;
        bb.clear();
        l = strlen(a);
        for (int i = 0; i < l; i++) {
            if (a[0] == '/' && a[1] == '/') {
                break;
            }else {
                for (i = 0; i < l; i++){
                    if (a[i] == '/' && a[i + 1] == '/')
                        break;
                    else {
                        if (a[i] != ' ')
                            flag = 1;
                        bb.push_back(a[i]);
                    }
                       
                }
                if (flag == 1) {
                    for (int i = 0; i < bb.size(); i++) {
                        cout << bb[i];
                    }
                    printf("\n");
                    break;
                }
                else {
                    break;
                } 
            }
        }
    }
    return 0;
}


四、 Babelfish

Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.

Sample Input Copy
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output Copy
cat
eh
loops
HINT
Huge input and output,scanf and printf are recommended.

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <map>
using namespace std;
char a[25], b[25], c[25];
int main()
{
    map<string, string>mp;
    while (gets(a) && a[0] != '\0')
    {
        sscanf(a, "%s%s", b, c);
        mp[c] = b;
    }
    while (gets(b))
    {
        if (mp.find(b) == mp.end())
            printf("eh\n");
        else{
            cout << mp[b];
            printf("\n");
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值