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;
    }
  • 相关阅读:
    CF1066D Boxes Packing
    luogu P2508 [HAOI2008]圆上的整点
    luogu P2502 [HAOI2006]旅行
    luogu P2511 [HAOI2008]木棍分割
    luogu P4161 [SCOI2009]游戏
    luogu P4160 [SCOI2009]生日快乐
    windows2012系统IE浏览器无法打开加载flashplayer内容
    kvm虚拟机相关
    esxI开启虚拟化
    Termux 详细安装
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285884.html
Copyright © 2011-2022 走看看