zoukankan      html  css  js  c++  java
  • Kattis -Bus Numbers

    Bus Numbers

    Your favourite public transport company LS (we cannot use their real name here, so we permuted the letters) wants to change signs on all bus stops. Some bus stops have quite a few buses that stop there and listing all the buses takes space. However, if for example buses 141, 142, 143 stop there, we can write 141–143 instead and this saves space. Note that just for two buses this does not save space.

    You are given a list of buses that stop at a bus stop. What is the shortest representation of this list?

    Input

    The first line of the input contains one integer N,1N1000N,1≤N≤1000, the number of buses that stop at a bus stop. Then next line contains a list of NN space separated integers between 1 and 10001000, which denote the bus numbers. All numbers are distinct.

    Output

    Print the shortest representation of the list of bus numbers. Use the format as in the example, separate numbers with single spaces and output them in sorted order.

    Sample Input 1Sample Output 1
    6
    180 141 174 143 142 175
    
    141-143 174 175 180

    题意

    将给出的数字按照升序排好,然后是连续的就连起来直接输出

    代码

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    #pragma warning(disable:4996);
    int a[1005],n;
    int main() {
        while (~scanf("%d", &n))
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
            }
            sort(a, a + n);
            int key = 0;
            printf("%d", a[0]);
            if (a[0] + 1 == a[1])key++;
            for (int i = 1; i < n; i++)
            {    
                if (key == 0 && a[i] + 1 == a[i + 1])
                {
                    printf(" %d", a[i]);
                    key++;
                }
                else if (key &&a[i] + 1 == a[i + 1])
                {
                    key++;
                    continue;
                }
                else if (a[i + 1]!= a[i] + 1)
                {
                    if (key>1)
                    {
                        key = 0;
                        printf("-%d", a[i]);
                    }
                    else
                    {
                        key = 0;
                        printf(" %d", a[i]);
                    }
                }
            }puts("");
        }
        return 0;
    }
  • 相关阅读:
    [PoC]某B2B网站的一个反射型XSS漏洞
    Python中的基本语句
    视频: 千重浪Linux系统调试技术培训 03-01_Basic-CPU-Register
    POJ 2955 Brackets (区间dp 括号匹配)
    LeetCode 146 LRU Cache
    Poj1734题解
    Python
    小胖说事29-----iOS中Navigation中左滑pop页面的三种方法
    深入理解javascript之原型
    android 弹幕评论效果
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285884.html
Copyright © 2011-2022 走看看