{ "question_id": 7439132, "question_title": "以特殊格式处理连续增加的数字", "question_content": "给出一串数字, 程序要把数字按照这样的格式输出,把连续增加的数字用 [x-y] 的形式表示,只显示这一组顺序数字的首位两个数字,不连续增加的数字单独列出。 例如:\n输入:1, 2, 3, 4, 5, 8, 10, 11, 12, 13, 20, 21, 22; \n输出:[1-5] [8] [10-13] [20-22]。", "difficulty": "简单", "answer_id": 53412990, "answer_content": "
\n# -*- coding: utf-8 -*-\n# Python3\n# 输入格式:1, 2, 3, 4, 5, 8, 10, 11, 12, 13, 20, 21, 22\nseq = list(map(int, input().split(',')))\ntmp = [seq[0]]\nall_list = []\nfor n in range(len(seq)):\n if n == len(seq) - 1:\n all_list.append(tmp)\n break\n if seq[n + 1] - seq[n] == 1:\n tmp.append(seq[n + 1])\n else:\n all_list.append(tmp)\n tmp = [seq[n + 1]]\nfor a in all_list:\n if len(a) > 1:\n print('[%s-%s]' % (a[0], a[-1]))\n else:\n print('[%s]' % a[0])\n
\n\n\n", "tag_name": "python", "python": "seq = list(map(int, input().split(',')))\ntmp = [seq[0]]\nall_list = []\nfor n in range(len(seq)):\n\tif n == len(seq) - 1:\n\t\tall_list.append(tmp)\n\t\tbreak\n\tif seq[n + 1] - seq[n] == 1:\n\t\ttmp.append(seq[n + 1])\n\telse:\n\t\tall_list.append(tmp)\n\t\ttmp = [seq[n + 1]]\nfor a in all_list:\n\tif len(a) > 1:\n\t\tprint('[%s-%s]' % (a[0], a[-1]))\n\telse:\n\t\tprint('[%s]' % a[0])", "topic_link": "https://bbs.csdn.net/topics/600469994", "status": 1, "keywords": "排序", "license": "csdn.net", "notebook": { "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/48.ipynb?type=file" }, "notebook_enable": 1 }