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;
    }
  • 相关阅读:
    报错处理
    MySQL8.0跟5.7分组查询表所有字段
    模拟开始时间、结束时间生成历史时间生成曲线模拟数据
    查询电脑登录过的WiFI账号密码
    Samba服务器架设
    CentOS安装GitLab
    申请域名并使用DDNS
    极路由4增强版(极企版)-刷潘多拉固件
    Git命令
    elasticsearch7.6.2 -canal1.1.4集成
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285884.html
Copyright © 2011-2022 走看看