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;
    }
  • 相关阅读:
    const,var,let区别(转载)
    在windows上搭建redis集群
    Linux学习笔记—vim程序编辑器
    Linux学习笔记—文件与文件系统的压缩与打包(转载)
    Linux学习笔记—Linux磁盘与文件系统管理(转载)
    五,mysql优化——sql语句优化小技巧
    八,mysql优化——读写分离
    六,mysql优化——小知识点
    七,mysql优化——表的垂直划分和水平划分
    三,mysql优化--sql语句优化之索引一
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285884.html
Copyright © 2011-2022 走看看