卡塔尔世界杯
题目背景
卡塔尔世界杯将在今年冬天举行,这也是仅有的在冬天举办的世界杯。
题目描述
你是世界杯组委会的工作人员。现在你需要制作一份世界杯32强的食宿和行程,以及训练场馆等等的安排表。
给你n个国家,你要把它们按英文字母顺序排列好。
输入格式
你的程序将输入n+1行。
第一行一个整数n,表示你需要安排的国家数量。
接下来n行,每行一个国家的英文名字。
n≤32
输出格式
输出n行,为按英文字母顺序排列好的国家名称,注意每个国家单列一行。
解析:首先需要读取输入的国家数量n,然后读取n个国家的名称。接着将国家名称按英文字母顺序排序,最后输出排序后的国家名称。
代码如下:
c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> countries(n);
for (int i = 0; i < n; i++) {
cin >> countries[i];
}
sort(countries.begin(), countries.end());
for (int i = 0; i < n; i++) {
cout << countries[i] << endl;
}
return 0;
}
python
def main():
n = int(input())
countries = []
for _ in range(n):
countries.append(input())
countries.sort()
for country in countries:
print(country)
if __name__ == "__main__":
main()
java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] countries = new String[n];
for (int i = 0; i < n; i++) {
countries[i] = sc.next();
}
Arrays.sort(countries);
for (String country : countries) {
System.out.println(country);
}
}
}
GO
package main
import (
"fmt"
"sort"
)
func main() {
// 读取输入的国家数量n
var n int
fmt.Scan(&n)
// 读取国家名称并存储在切片中
countries := make([]string, n)
for i := 0; i < n; i++ {
fmt.Scan(&countries[i])
}
// 对国家名称进行排序
sort.Strings(countries)
// 输出排序后的国家名称
for _, country := range countries {
fmt.Println(country)
}
}
PHP
<?php
// 读取输入的国家数量
$n = intval(fgets(STDIN));
// 读取n个国家的英文名字
$countries = [];
for ($i = 0; $i < $n; $i++) {
$countries[] = trim(fgets(STDIN));
}
// 对国家名称进行排序
sort($countries);
// 输出排序后的国家名称
foreach ($countries as $country) {
echo $country . PHP_EOL;
}
?>
perl
#!/usr/bin/perl
use strict;
use warnings;
# 读取输入的国家数量
my $n = <STDIN>;
chomp($n);
# 读取n个国家的英文名字
my @countries;
for (1..$n) {
my $country = <STDIN>;
chomp($country);
push @countries, $country;
}
# 按字母顺序排序国家名称
@countries = sort @countries;
# 输出排序后的国家名称
foreach my $country (@countries) {
print "$country
";
}