Official

A - 484558 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 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.


This problem can be solved as follows.

  1. Find which of \(0,1,2,\ldots,15\) is each of the digit in the two-digit hexadecimal notation of \(N\).
  2. Convert the found digit into a hexadecimal ones 0123456789ABCDEF and print it.

Finding it each

“Finding each digit in the two-digit hexadecimal notation of \(N\)” is equivalent to:

  • finding integers \(a\) and \(b\) between \(0\) (inclusive) and \(16\) (exclusive) such that \(N=16a+b\).

This is found by \(a=\lfloor \frac{N}{16} \rfloor, b=N \bmod 16\). In C++, you can express them as follows:

a = N/16, b = N%16;

Printing digits as hexadecimal ones

If \(a\) and \(b\) is \(9\) or less, you may directly print it. If it is \(10\) or more, you need to print A for \(10\), B for \(11\), \(\ldots\), but in C++ you can find the uppercase English letter corresponding to \(a\) as follows:

(char)('A' + (a-10))

Therefore, you may find the desired output while checking each of \(a\) and \(b\).

Sample code (C++)

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

int main() {
    
	int N;
	cin>>N;
	
	int a,b;
	a = N/16, b = N%16;
	
	if(a<=9)cout<<a;
	else cout<<(char)('A'+a-10);
	
	if(b<=9)cout<<b;
	else cout<<(char)('A'+b-10);
	
	return 0;
}

posted:
last update: