Task Description
一位來自墨西哥蒙特瑞技術研究學院(ITESM Campus Monterrey)的學生想發表一種新的數值加密演算法。
演算法步驟如下:
- 讀入一個整數N,N為欲加密的數字:N = 265
- 將N當作十進位的數值:X1 = 265(decimal)
- 把X1由十進位轉為二進位:X1 = 100001001(binary)
- 計算二進位的X1有幾個1:b1 = 3
- 把N當作十六進位數值:X2 = 265(hexadecimal)
- 把X2由十六進位轉為二進位:X2 = 1001100101(binary)
- 計算二進位的X2有幾個1:b2 = 5
- 最後的編碼為N xor (b1 * b2):265 xor (3 * 5) = 262
這位學生並未通過這次的計算機組識考試,所以他請求校方在ACM的試題上出一題計算共有幾個位元1的題目,好讓他能順利發表他的數值加密演算法。
你必須寫一個程式能讀入一個整數,然後輸出該整數的b1, b2值。
Hint
bin.h
打上 function header 以及相關的設定。
bin.c
撰寫程式碼後對應上傳。
#include "bin.h"
int bin( int N) {
}
|
hex.h
打上 function header 以及相關的設定。
hex.c
撰寫程式碼後對應上傳。
#include "hex.h"
int hex( int N) {
}
|
main.c
這個檔案無法更改也無須上傳。
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h>
#include "bin.h"
#include "hex.h"
int main() {
int N, b1, b2;
scanf ( "%d" , &N);
b1 = bin(N);
b2 = hex(N);
printf ( "%d %d" , b1, b2);
return 0;
}
|
Sample Input
Sample Output
Sample Input
Sample Output