🌕🌑🌑🌑🌑
# 題目連結
- 題目連結
- Online Judge
- uDebug
# 題目說明
Time limit: 3.000 seconds
# 題目
A student from ITESM Campus Monterrey plays with a new encryption method for numbers. These method consist of the following steps:
Steps : Example
- Read the number N to encrypt : M = 265
- Interpret N as a decimal number : X1 = 265 (decimal)
- Convert the decimal interpretation of N to its binary representation : X1 = 100001001 (binary)
- Let b1 be equal to the number of 1’s in this binary representation : b1 = 3
- Interpret N as a Hexadecimal number : X2 = 265 (hexadecimal)
- Convert the hexadecimal interpretation of N to its binary representation : X2 = 1001100101
- Let b2 be equal to the number of 1’s in the last binary representation : b2 = 5
- The encryption is the result of M xor (b1 ∗ b2) : 265 xor (3*5) = 262
This student failed Computational Organization, thats 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
# 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.
# 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
# Sample Input
3
265
111
1234
# Sample Output
3 5
6 3
5 5
# 解題技巧
BigInteger n1 = new BigInteger("1234567890");
- 加減乘除運算
n1.add(n2);
n1.subtract(n2);
n1.multiply(n2);
n1.divide(n2);
- 比較
n1.compareTo(n2);
- 變回字串
n1.toString(radix: );
# Solution
import java.math.BigInteger; | |
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
for(int i = 0; i < N; i++){ | |
String M = sc.next(); | |
String X1 = (new BigInteger(M, 10)).toString(2); | |
String X2 = (new BigInteger(M, 16)).toString(2); | |
int b1 = 0; | |
int b2 = 0; | |
for(int j = 0; j < X1.length(); j++){ | |
if(X1.charAt(j) == '1') b1++; | |
} | |
for(int j = 0; j < X2.length(); j++){ | |
if(X2.charAt(j) == '1') b2++; | |
} | |
System.out.println(b1 + " " + b2); | |
} | |
sc.close(); | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!