UVA10019 Funny Encryption Method【进制】

A student from ITESM Campus Monterrey plays with a new encryption method for numbers. Thesemethod 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) : 265 xor (3*5) = 262

  This student failed Computational Organization, thats why this student asked the judges of ITESMCampus Monterrey internal ACM programming Contest to ask for the numbers of 1’s bits of this tworepresentations 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. Eachof the following N Lines (0 < N ≤ 1000) will contain the number M (0 < M ≤ 9999, in decimalrepresentation) 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 onespace corresponding to that lines number to crypt

Sample Input

3

265

111

1234

Sample Output

3 5

6 3

5 5


问题链接UVA10019 Funny Encryption Method

问题简述

  给定整数M,满足0<M<9999,求其16进制和10进制各位数字转换为2进制后,1的个数之和。

问题分析

  C/C++语言程序中,整数是补码表示。

  16进制时,各位数字中1的个数直接数一下就可以了。

  10进制时,则需要取出各个10进制位再数一下。

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA10019 Funny Encryption Method */

#include <bits/stdc++.h>

using namespace std;

int getBits(int a)
{
    int count = 0;

    while(a) {
        count += a & 1;
        a >>= 1;
    }

    return count;
}

int main()
{
    int n, m, b16, b10;

    scanf("%d", &n);
    while(n--) {
        scanf("%d", &m);

        b16 = getBits(m);

        b10 = 0;
        while(m) {
            b10 += getBits(m % 10);
            m /= 10;
        }

        printf("%d %d\n", b16, b10);
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值