본문 바로가기

알고스팟 풀이/구현

[알고스팟/ALGOSPOT] 24. ENDIANS

안녕하세요. 알고라파덕입니다.

24번 문제 ENDIANS


ENDIANS라는 문제는 그리디 문제입니다.


영어 문제로. 문제는 Lilliput, Blefuscu 이 두 나라가 존재하는데 서로 달걀을 깨는 방법이 다르다 하면서

수를 표기하는 방법 또한 다르다라고 합니다. 문제의 예로 305,419,896이라는 수가 있으면

Blefuscu 나라에서는 : 00010010 00110100 01010110 01111000

Lilliput 나라에서는 : 01111000 01010110 00110100 00010010 

이렇게 표기를 합니다. 

Blefuscu라는 나라의 표기가 일반적인 2진수 표현이죠.

Lilliput이라는 라나에서는 그 2진수 표현을 4개의 뭉떵이로 역순으로 출력을 한 것이구요

여기서 두 나라의 표기를 변환할 수 있는 프로그램을 짜는 것이 문제입니다.


ENDIANS

출처 : https://algospot.com/judge/problem/read/ENDIANS#


문제 정보

문제

The two island nations Lilliput and Blefuscu are severe rivals. They dislike each other a lot, and the most important reason was that they break open boiled eggs in different ways.

People from Lilliput are called little-endians, since they open eggs at the small end. People fromBlefuscu are called big-endians, since they open eggs at the big end.

This argument was not only about their breakfasts but about their computers, too. Lilliput and Blefuscu's computers differed in the way they store integers; they stored the bytes in different orders. Computers from Lilliput(*little-endians*) ordered it from the LSB(least significant byte) to the MSB(most significant byte), and computers from Blefuscu was exactly the opposite.

For example, a 32-bit unsigned integer 305,419,896 (0x12345678) would be saved in two different ways:

  • 00010010 00110100 01010110 01111000 (in an Blefuscu computer)
  • 01111000 01010110 00110100 00010010 (in an Lilliput computer)

Therefore, if there was any need to exchange information between Blefuscu and Lilliput computers, some kind of conversion process was inevitable. Since nobody liked converting the data by himself before sending, recipients always had to do the conversion process.

Given some 32-bit unsigned integers retrieved in a wrong endian, write a program to do a conversion to find the correct value.

입력

The first line of the input file will contain the number of test cases, C (1 ≤ C ≤ 10, 000). Each test case contains a single 32-bit unsigned integer, which is the data without endian conversion.

출력

For each test case, print out the converted number.

예제 입력

4
2018915346
1
100000
4294967295

예제 출력

305419896
16777216
2693136640
4294967295


생각해야 할 점


2진수 표현을 어떻게 뒤집어서 계산을 해야 할까.


2진수 표현을 뒤집기 위해서는 먼저 2진수 표현을 해주셔야 합니다. 

주어진 수를 2진수로 바꾸고, 여기서 이제 2진수의 단위를 바꾸어서 계산을 해야하죠.

일차원 배열으로 만들어서 2중 FOR문을 이용하여 계산하는 방법을 사용할 수 있습니다.

또는 2차원 배열로 만들어서 똑같이 2진수의 단위를 바꾸어 계산을 할 수 있습니다.

또는 2진수 표현을 해주면서 배열에 저장하지 않고, 각 위치의 값어치를 새로 더해주는 방법도 있습니다.

해결할 수 있는 방법은 많습니다.

저는 2차원 배열로 기억을 하는 방법을 선택하였습니다.




감사합니다.