您的位置:首页 > 其它

CQUPT 2018 寒假训练 DIV2 (1)STL基础题解

2018-02-15 17:40 375 查看

Problem  A:

ACboy was kidnapped!! 
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(. 
 As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy." 
The problems of the monster is shown on the wall: 
 Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out"). 
and the following N lines, each line is "IN M" or "OUT", (M represent a integer). 
 and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!InputThe input contains multiple test cases. 
The first line has one integer,represent the number oftest cases. 
And the input of each subproblem are described above.OutputFor each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.

题目大意:

FIFO和FILO决定取出时的规则,in则代表放入。用队列和栈模拟即可。

代码

 
#include <iostream>
#include <list>
#include<string>
#include<stdio.h>

using namespace std;

int main()
{
    //测试组数  命令字符串 命令 中间组数  操作数
    int n;
    string oper;
    string y;
    int midn;
    int x;
    int i,j;

    scanf("%d",&n);
    list<int> mlist;
    for(i = 0;i < n;i ++)
    {
        scanf("%d",&midn);
        cin >> oper;
        for(j = 0;j < midn;j ++)
        {
            cin >> y;
            if(y.compare("IN") == 0)
            {
                scanf("%d",&x);
                mlist.push_back(x);
            }
            else
            {
                if(oper.compare("FIFO"))
                {
                    if(mlist.empty())
                        cout << "None"<<endl;
                    else{
                        cout << mlist.back()<<endl;
                        mlist.pop_back();
                    }

                }
                 else{
                    if(mlist.empty())
                        cout << "none"<<endl;
                    else{
                        cout << mlist.front()<<endl;
                        mlist.pop_front();
                    }

                 }
            }
        }
        mlist.clear();
    }
    return 0;
}

Problem B:

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(a) if it is the empty string(b) if A and B are correct, AB is correct,(c) if A is correct, (A) and [A] is correct.Write a program that takes a sequence of strings of this type and check their correctness. Yourprogram can assume that the maximum string length is 128.InputThe file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one stringa line.OutputA sequence of ‘Yes’ or ‘No’ on the output file.Sample Input3([])(([()])))([()[]()])()Sample OutputYesNoYes

题目大意:

判断括号的河法性。先找第一个右括号,然后找它旁边的括号,判断是否合法,若和法,删去,并重复该过程。

代码

#include<iostream>
#include<string>
#include<stdio.h>
#include<iterator>

using namespace std;

int judge(string a); //判断是否正确
int main()
{
    //接受字符串   组数
    string x;
    int n;
    int i;

    scanf("%d",&n);
    getchar();
    for(i = 0;i < n;i ++)
    {
        getline(cin,x);
        if(judge(x))
            cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
int judge(string a)
{
    static string::iterator itr;
    static string::iterator itr1;
    for(itr = a.begin();itr != a.end();itr ++)
    {
        if(*itr == ')')
        {
            for(;;itr --)
            {
                if(*itr == '(')
                    judge()
            }
        }
    }
}

Promblem C:

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 

题目大意:

计算一个表达式的值。由于表达式比较规范,所以可以分为数值和字符输入。
先将'*',' /'  计算后数值放入list中,再计算加减。

代码:

  
#include<iostream>
#include<vector>
#include<list>
#include<stdio.h>

using namespace std;

list<double> a;
list<char> c;

int main()
{
    char b;
    double x;
    while(1)
    {
        cin >> x;
        if(x == 0&&getchar() == '\n')
            break;
        a.push_back(x);
        do
        {
            cin >> b;

            cin >> x;
            if(b == '+' ||b == '-'){
                c.push_back(b);
                a.push_back(x);
            }
            else if(b == '*')
            {
                double temp = a.back();
                temp *= x;
                a.pop_back();
                a.push_back(temp);
            }
            else if(b == '/')
            {
                double temp = a.back();
                temp /= x;
                a.pop_back();
                a.push_back(temp);
            }
            if(getchar() == '\n')
                break;
        }while(1);

        double sum = a.front();
        a.pop_front();
        while(!a.empty())
        {
            if(c.front() == '+')
            {
                sum += a.front();
                c.pop_front();
                a.pop_front();
            }
            else if(c.front() == '-')
            {
                sum -= a.front();
                c.pop_front();
                a.pop_front();
            }
        }
        printf("%.2lf\n",sum);

        c.clear();
    }
    return 0;
}
 
  

Problem D:

Raju and Meena love to play with Marbles. They have got a lot ofmarbles with numbers written on them. At the beginning, Raju wouldplace the marbles one after another in ascending order of the numberswritten on them. Then Meena would ask Raju to find the first marblewith a certain number. She would count 1...2...3. Raju gets one pointfor correct answer, and Meena gets the point if Raju fails. After somefixed number of trials the game ends and the player with maximumpoints wins. Today it’s your chance to play as Raju. Being the smartkid, you’d be taking the favor of a computer. But don’t underestimateMeena, she had written a program to keep track how much time you’retaking to give all the answers. So now you have to write a program,which will help you in your role as Raju.

题目大意:

给一组数据,再给一个数,找它在排序后序列中的位置。

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<iterator>

using namespace std;

int main()
{
    //组数
    int n,m;
    vector<int> itr;
    int round = 1;

    do
    {
        scanf("%d%d",&n,&m);
        if(n == 0&& m == 0)
            break;

        //记录一组数据
        for(int i = 0;i < n;i ++)
        {
            int x;
            scanf("%d",&x);
            itr.push_back(x);
        }
        //库函数快排
        sort(itr.begin(),itr.end());

        cout << "CASE# "<<round<<":"<<endl;
        for(int i = 0;i < m;i ++)
        {
            int x;
            int flag = 0;
            scanf("%d",&x);

            int j;
            for(j = 0;j < n;j ++)
            {
                if(itr[j] == x)
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 1)
            {
                cout << x << " found at "<<j + 1<<endl;
            }
            else cout << x << " not found" << endl;
        }
        round ++;
        itr.clear();
    }while(1);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: