{ }codelper
字符不换行输出P0045

字母链

一行中给出两个英文字母 a 和 b,中间隔一个空格。
两个字母都是小写或都是大写。
按字母表顺序从 a 到 b 逐个输出字母,中间用 "-" 隔开,输出在一行。
如果 a 在 b 之后,就倒着走(朝 b 的方向)。
a 和 b 相同时只输出这一个字母。
开头和结尾都不写 "-"。

输入
a, b:各一个英文字母,大小写相同
输出
由字母和 "-" 组成的一行
样例 1 · stdin
c g
stdout
c-d-e-f-g
样例 2 · stdin
E B
stdout
E-D-C-B
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

把字母“当作数字”处理的方法因语言而异。
C 的 char 是小整数,c + 1 就是下一个字母,用 putchar(c) 输出。
C++、Java、C# 也有字符类型,但 char + int 的结果是 int,所以 Java System.out.print('c' + 1) 和 C++ std::cout << 'c' + 1 会输出 "100"——要用 (char)(c + 1) 转回来,或重新存进 char 变量。
把循环变量设成 int 直接输出,也会得到 "99-100-101"。
Python、JS、TS 没有字符类型,只有单字符字符串,所以要和编码互相转换:Python ord("a")、chr(97),JS/TS "a".charCodeAt(0)、String.fromCharCode(97),Craykon 字符.编码("a")、字符.从编码(97)。
忘了这一步,JS 的 "c" + 1 会悄悄变成 "c1",Python 则以 TypeError 停止。
不换行接着输出:Python print(x, end=""),JS/TS process.stdout.write(console.log 总会换行),Java System.out.print,C# Console.Write,C putchar、printf,C++ std::cout << x,Craykon 控制台.接着输出。
常见陷阱是每个字母后面都加 "-",结尾多出一个 "-"——只在不是第一个字母时加在前面,或者把字母收集起来用 "-".join、join("-") 连接。
别忘了末尾的一个换行。
还有 a > b 倒着走的情况。

Pythonmain.py · 8 行
a, b = input().split()# 글자 ↔ 코드: ord("a") == 97, chr(97) == "a"step = 1 if a <= b else -1for code in range(ord(a), ord(b) + step, step):    if code != ord(a):        print("-", end="")    print(chr(code), end="")print()
JavaScriptmain.js · 10 行
const [a, b] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);// 글자 ↔ 코드: "a".charCodeAt(0) === 97, String.fromCharCode(97) === "a"const from = a.charCodeAt(0);const to = b.charCodeAt(0);const step = from <= to ? 1 : -1;for (let code = from; code !== to + step; code += step) {  if (code !== from) process.stdout.write("-");  process.stdout.write(String.fromCharCode(code));}process.stdout.write("\n");
TypeScriptmain.ts · 11 行
import { readFileSync } from "node:fs";const [a, b]: string[] = readFileSync(0, "utf8").trim().split(/\s+/);// 글자 ↔ 코드: "a".charCodeAt(0) === 97, String.fromCharCode(97) === "a"const from: number = a.charCodeAt(0);const to: number = b.charCodeAt(0);const step: number = from <= to ? 1 : -1;for (let code = from; code !== to + step; code += step) {  if (code !== from) process.stdout.write("-");  process.stdout.write(String.fromCharCode(code));}process.stdout.write("\n");
JavaMain.java · 17 行
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        char a = sc.next().charAt(0);        char b = sc.next().charAt(0);        int step = a <= b ? 1 : -1;        StringBuilder out = new StringBuilder();        // char + int 는 int 가 되므로 (char)로 되돌린다        for (char c = a; c != b + step; c = (char) (c + step)) {            if (c != a) out.append('-');            out.append(c);        }        System.out.println(out);    }}
C#Main.cs · 19 行
using System; class Program{    static void Main()    {        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        char a = parts[0][0];        char b = parts[1][0];        int step = a <= b ? 1 : -1;        // char + int 는 int 가 되므로 (char)로 되돌린다        for (char c = a; c != b + step; c = (char)(c + step))        {            if (c != a) Console.Write('-');            Console.Write(c);        }        Console.WriteLine();    }}
C++main.cpp · 13 行
#include <iostream> int main() {    char a, b;    std::cin >> a >> b;    int step = a <= b ? 1 : -1;    // c + step 은 int 라서 char 변수에 다시 담아야 글자로 찍힌다    for (char c = a; c != b + step; c += step) {        if (c != a) std::cout << '-';        std::cout << c;    }    std::cout << '\n';}
Cmain.c · 14 行
#include <stdio.h> int main(void) {    char a, b;    scanf(" %c %c", &a, &b);    int step = a <= b ? 1 : -1;    /* C의 char는 작은 정수라 c + step 이 곧 다음 글자다 */    for (char c = a; c != b + step; c += step) {        if (c != a) putchar('-');        putchar(c);    }    putchar('\n');    return 0;}
Craykonmain.cray · 19 行
글자들 = 控制台.读行().分割(" ");처음 = 字符.编码(글자들[0]);끝 = 字符.编码(글자들[1]);걸음 = 1;如果 (처음 > 끝){    걸음 = -1;}코드 = 처음;当 (코드 != 끝 + 걸음){    如果 (코드 != 처음)    {        控制台.接着输出("-");    }    控制台.接着输出(字符.从编码(코드));    코드 += 걸음;}控制台.输出("");