Official

A - AtCoder Quiz 3 Editorial 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 to problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).

First, by reading the problem statement carefully, we can observe that we should output

  • \(N\) if \(N \lt 42\);
  • \(N + 1\) if \(42 \leq N\).

This can be implemented with a if statement, or by incrementing \(N\) if \(N \geq 42\).

All that left is to append zero-padding to make it three digits. In order to achieve this, in C++, we may use std::setfill or std::setw in iomanip library as follows:

cout << setfill('0') << setw(3) << N;

then zero-padding can be instructed simply. As another solution, we may use a conditional branch like

if(N < 100) cout << "0";
if(N < 10) cout << "0";
cout << N;

to implement it.
By implementing it just as the description above, this problem can be solved.

A sample code in C++ follows.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N;
  cin >> N;
  if (N >= 42) N++;
  cout << "AGC";
  cout << setfill('0') << setw(3) << N;
  cout << endl;
}

posted:
last update: