(UVA) 10019 - Funny Encryption Method - Solução
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int leitor(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
void processa() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int qteCasos = leitor(br);
for (int i = 0; i < qteCasos; i++) {
int m = leitor(br);
// Valor binario de m
String binarioM = Integer.toBinaryString(m);
// Considerando m na base hexadecimal, converto para saber seu valor na base decimal
int hexadecimalM = Integer.parseInt(String.valueOf(m), 16);
// Valor binario do valor decimal de m sendo hexadecimal
String binarioHex = Integer.toBinaryString(hexadecimalM);
int res1 = 0;
for (int j = 0; j < binarioM.length(); j++) {
if (binarioM.charAt(j) == '1') {
res1 += 1;
}
}
int res2 = 0;
for (int j = 0; j < binarioHex.length(); j++) {
if (binarioHex.charAt(j) == '1') {
res2 += 1;
}
}
bw.write(res1 + " " + res2 + "\n");
}
bw.flush();
bw.close();
return;
}
}
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int leitor(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp;
}
void processa() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int qteCasos = leitor(br);
for (int i = 0; i < qteCasos; i++) {
int m = leitor(br);
// Valor binario de m
String binarioM = Integer.toBinaryString(m);
// Considerando m na base hexadecimal, converto para saber seu valor na base decimal
int hexadecimalM = Integer.parseInt(String.valueOf(m), 16);
// Valor binario do valor decimal de m sendo hexadecimal
String binarioHex = Integer.toBinaryString(hexadecimalM);
int res1 = 0;
for (int j = 0; j < binarioM.length(); j++) {
if (binarioM.charAt(j) == '1') {
res1 += 1;
}
}
int res2 = 0;
for (int j = 0; j < binarioHex.length(); j++) {
if (binarioHex.charAt(j) == '1') {
res2 += 1;
}
}
bw.write(res1 + " " + res2 + "\n");
}
bw.flush();
bw.close();
return;
}
}
Comments
Post a Comment