PKU Campus 2014 B:An Easy Task(模拟)

PKU Campus 2014 B:An Easy Task(模拟)
总时间限制: 1000ms 内存限制: 65536kB

描述

You are given an easy task by your supervisor – to find the best value of X, one of the parameters in an evaluation function, in order to improve the accuracy of the whole program.

However, after a few days’ analysis, you realize that it is far harder than you imagine. There are so many values X can be, and the only way to find the best one among them is to try all these possible values one after another!

Fortunately, you know that X is an integer and thanks to the previous works by your senior fellow apprentices, you have got n constraints on X. Each constraint must be in one of the following forms:
1. < k: means that X is less than integer k;
2. > k: means that X is greater than integer k;
3. <= k: means that X is less than or equal to integer k;
4. >= k: means that X is greater than or equal to integer k;
5. = k: means that X is equal to integer k.

Now, you are going to figure out how many possible values X can be, so that you can estimate whether it is possible to finish your task before deadline.

输入
The first line contains an integer T (1 ≤ T ≤ 10) – the number of test cases.

For each test case:
The first line contains an integer n. 0 ≤ n ≤ 10 000.
Then follows n lines, each line contains a comparison operator o and an integer k, separated by a single space. o can be one of “>”, “<”, “>=”, “<=”, and “=”. 0 ≤ | k | ≤ 1 000 000 000.
There is no contradictory between these constraints, in other word, at least one integer value meets all of them.

输出
For each test case, output one integer in a single line – the number of possible values of X, or “-1” if the answer is infinite.

样例输入

1
2
> 2
<= 5

样例输出

3

一个打卡题,但是如何优雅地处理字符串问题也是一个问题。

本题中好在符号和数字之间有空格,就很方便了。(我的代码里的做法复杂了,一开始我没考虑存在空格。)
如果没有空格。我的建议是一下子把字符串都读入,再判断清楚符号以后使用sscanf把数字从字符串内读取。
或者像我的代码一样,采用cin.peek()先看看第二个字符是什么,即有没有’=’再确定读入的格式,直接用scanf读取。cin.peek的好处是万一读出来是一个数字,那么这个数字还在输入流内,不会遇到那种很尴尬的情况:只想看一看是不是等号,结果是数字,那么剩下来的数字只能手动读取,再手动拼接起来。
当然,优雅地字符串处理这种问题靠的是经验,没必要讨论这么详细,可是本题也没有其他好讨论的了。


Accepted    512kB   8ms     1019 B  G++     
#include<iostream>
#include<stdio.h>

using namespace std;

typedef long long LL;

const LL INF = 1000000000 + 10;

inline int Max(int x, int y)
{
    return x > y? x: y;
}

inline int Min(int x, int y)
{
    return x < y? x: y;
}

int main()
{
    int t, n;
    LL k, l ,r;
    char ch;
    scanf("%d\n", &t);  
    while (t--)
    {
        scanf("%d\n", &n);
        l = -INF;
        r = INF;
        while (n--)
        {
            ch = cin.peek();
            if (ch == '=')
            {
                scanf("%c%lld\n", &ch, &k);
                l = r = k;
            }
            else if (ch == '<')
            {
                scanf("%c", &ch);
                if (cin.peek() == '=')
                {
                    scanf("%c%lld\n", &ch, &k);
                    r = Min(r, k);
                }
                else
                {
                    scanf("%lld\n", &k);
                    r = Min(r, k - 1);
                }
            }
            else if (ch == '>')
            {

                scanf("%c", &ch);
                if (cin.peek() == '=')
                {
                    scanf("%c%lld\n", &ch, &k);
                    l = Max(l, k);
                }
                else
                {
                    scanf("%lld\n", &k);
                    l = Max(l, k + 1);
                }               
            }
        }
        if ((l == - INF) || (r == INF))
            printf("-1\n");
        else
            printf("%lld\n", r - l + 1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值