'[Public] 컴퓨터공학/UVA'에 해당되는 글 4건

1. 2010.04.18 [UVA] 10019 Funny Encryption Method
2010.04.18 04:17

[UVA] 10019 Funny Encryption Method

오늘 부터 UVA 알고리즘을 활성화 하기로 했다.

많은 문제를 풀어서

ACM 대회 나가보장~ 화이팅

 Funny Encryption Method

The Problem

History :
A student from ITESM Campus Monterrey plays with a new encryption method for numbers. These method consist of the following steps:

Steps :  Example
1) Read the number N to encrypt M = 265
2) Interpret N as a decimal number X1= 265 (decimal)
3) Convert the decimal interpretation of N to its binary representation X1= 100001001 (binary)
4) Let b1 be equal to the number of 1’s in this binary representation B1= 3
5) Interpret N as a Hexadecimal number X2 = 265 (hexadecimal)
6) Convert the hexadecimal interpretation of N to its binary representation X2 = 1001100101
7) Let b2 be equal to the number of 1’s in the last binary representation B2 = 5
8) The encryption is the result of  M xor (b1*b2) M xor (3*5) = 262

This student failed Computational Organization, that’s why this student asked the judges of ITESM Campus Monterrey internal ACM programming Contest to ask for the numbers of 1’s bits of this two representations so that he can continue playing.

You have to write a program that read a Number and give as output the number b1 and b2

The Input

The first line will contain a number N which is the number of cases that you have to process. Each of the following N Lines ( 0<N<=1000) will contain the number M (0<M<=9999, in decimal representation)  which is the number the student wants to encrypt.

The Output

You will have to output N lines, each containing the number b1 and b2 in that order, separated by one space  corresponding to that lines number to crypt

3
265
111
1234

3 5
6 3
5 5

소스

#include <stdio.h>
#include <math.h>
int main()
{
int Input, nCnt;
scanf("%d", &nCnt);
if(nCnt <0 && nCnt > 1000)
return 0;
int nP1, nQ1;
int nB1, nB2;
int nHexCnt, nHexData;
while(nCnt > 0)
{
scanf("%d", &Input);
if(Input <0 && Input > 9999)
break;
nP1 = Input;
nHexCnt = 0;
nHexData = 0;
while(nP1 > 9)
{
nQ1 = nP1 % 10;
nQ1 = nQ1 * pow(16.0, nHexCnt);
nHexData += nQ1;
nP1 = nP1/10;
nHexCnt++;
}
nHexData = nHexData + nP1* pow(16.0, nHexCnt);
nP1 = Input;
nB1 = 0;
while(nP1 > 0)
{
nQ1 = nP1%2;
nP1 = (nP1 / 2);
nB1 += nQ1;
}
nP1 = nHexData;
nB2 = 0;
while(nP1 > 0)
{
nQ1 = nP1%2;
nP1 = (nP1 / 2);
nB2 += nQ1;
}
printf("%d %d\n",nB1, nB2);
nCnt--;
}
return 0;
}

결과 기분 좋게 한방에  Accepted  가 나왔다. ㅋ

순위에 들려면  0.000  이던데

지금은 시간 줄이는 것보다 보다 많은 문제를 풀어보는게 좋을듯 싶다.

'[Public] 컴퓨터공학 > UVA' 카테고리의 다른 글

 [UVA] 11362 Age Sort  (0) 2011.07.28 2011.07.25 2010.04.26 2010.04.18