牛客练习赛14 D 比较月亮大小 【水】

链接:https://www.nowcoder.com/acm/contest/82/D
来源:牛客网

比较月亮大小
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述

 点点是一名出色的狼人。众所周知,狼人只有在满月之夜才会变成狼。

同时,月亮的大小随着时间变化,它的大小变化30天为一循环。它的变化情况(从第一天开始)为0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 然后又再一次循环。

今年夏天点点很无聊,于是开始看月亮。由于点点很忙,所以他只选择一段连续的时间看月亮,并把月亮的大小记录了下来。

现在,他告诉你他记录下东西,让你告诉他下一天(即点点记录下的最后一天的第二天)的月亮是比前一天(即点点记录下的最后一天)大还是小。

输入描述:

给你一个正整数n表示点点记录下的时间个数。
下一行n个自然数表示点点记录下的月亮大小。

输出描述:

一个字符串。
如果下一天的比前一天的大则输出”UP”
如果下一天的比前一天的小则输出”DOWN”
如果无法判断则输出”-1”

示例1
输入

5
3 4 5 6 7

输出

UP

示例2
输入

8
12 13 14 15 14 13 12 11

输出

DOWN

示例3
输入

1
8

输出

-1

备注:

n≤100 0 ≤ ai ≤ 15
保证输入的数据满足月亮变化的循环规律

思路

考虑完整 分析出集中情况即可

0.n = 1 的情况
输入的一个数 只有是 0 或 15 的时候 分别对应 up 和 down 其他情况都是 -1

  1. n != 1 的时候 我们只需要最后两个数字 就足以判断

我们令 最后两数字 分别为 a b

a > b 的时候

如果 a = 1 那么 b 就为 0 就是 up
其他情况 都是 down

a < b 的时候
如果 a = 14 那么 b 就是 15 然后 就是 down
其他情况都是 up

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;

int main()
{
    int n, num;
    cin >> n;
    if (n == 1)
    {
        cin >> num;
        if (num == 0)
            printf("UP
");
        else if (num == 15)
            printf("DOWN
");
        else
            printf("-1
");
    }
    else
    {
        int m = n - 2;
        for (int i = 0; i < m; i++)
            scanf("%d", &num);
        int a, b;
        scanf("%d%d", &a, &b);
        if (a > b)
        {
            if (a == 1)
                printf("UP
");
            else
                printf("DOWN
");
        }
        else if (a < b)
        {
            if (a == 14)
                printf("DOWN
");
            else
                printf("UP
");
        }
    }
}






原文地址:https://www.cnblogs.com/Dup4/p/9433177.html