公式

A - 3,2,1,GO 解説 by en_translator


Original proposer: admin

Just as instructed in the problem statement, print the numbers in descending order while interleaving commas. Be careful not to append an excessive comma at the end. A natural way is to write a conditional branch depending on whether the printed number is 1.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin >> n;
  for(int i=n; i>=1; i--){
    cout << i;
    if(i!=1){
      cout << ',';
    }
  }
}

Sample code (Python)

N = int(input())
for i in range(N, 0, -1):
  print(i, end="")
  if i!=1:
    print(",", end="")

In Python, one may use the join method to concisely implement it.

Sample code (Python)

N = int(input())
num_strings = [str(i) for i in range(N, 0, -1)]
print(",".join(num_strings))

投稿日時:
最終更新: