CPE 程式解答

10403: Funny Encryption Method

10403: Funny Encryption Method
Time Limit: 3 sec

A student from ITESM Campus Monterrey plays with a new encryption method for numbers. These method consist of the following steps:
Steps : (Example for 265)
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 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

  1. #include <iostream>
  2. #include <bitset>
  3. #include <sstream>
  5. using namespace std;
  7. int main()
  8. {
  9.     int n;
  10.     cin >> n;
  11.     for(int i=0;i<n;i++){
  12.         int a,b;
  13.         cin >> a;
  14.         bitset<20> x1(a);
  15.         stringstream temp;
  16.         temp << a;
  17.         temp >> hex >> b;
  18.         //cout << b;
  19.         bitset<20> x2(b);
  20.         cout << x1.count() << " " << x2.count() << endl;
  21.     }
  22.     return 0;
  23. }

程式都在cpe系統里鑒定通過了 當然可能coding style未必好 單純供大家參考
如果看不明白 欢迎留言或E-mail我 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.