A - Count Down 解説 by en_translator
If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) (https://atcoder.jp/contests/typical90) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
「C++入門 AtCoder Programming Guide for beginners (APG4b)」(https://atcoder.jp/contests/APG4b) is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
In C++, if you want to perform a process \(N\) times, you can write as follows:
for(int i=0;i<N;i++){
// The process you want to repeat N times
}
Inside {}, the variable \(i\) is set to \(0,1,\ldots,N-1\), which you can use in the process.
One can enhance the code as follows:
for(int i=0;i<N+1;i++){
cout<<N-i<<endl;
}
Now the value \((N-i)\) is printed for each \(i=0,1,\ldots,N\), achieving the objective of this problem. (Since there are \((N+1)\) integers less than or equal to \(N\), we modified the control statement so that it is repeated \((N+1)\) times.)
Alternatively, you can modify the for statement as follows:
for(int i=N;i>=0;i--){
cout<<i<<endl;
}
This way, \(i\) is set \(N,N-1,\ldots,0\), so you just need to print \(i\).
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
for(int i=N;i>=0;i--){
cout<<i<<endl;
}
return 0;
}
投稿日時:
最終更新: