cpe練習筆記 UVa10019 Funny Encryption Method
原文題目
一個學生正在玩一個數字編碼。這個方法是這樣的:
1. 將數字N加密到M=265
2. 將N視作是一個十進位數字:X1=265(十進位)
3. 轉換到二進制X1=100001001(二進制)
4. 讓b1相等於這個二進制1的數量,以X1即b1=3
5. 將N以16禁制表達:X2=265(HEX)
6. 轉換X2到binary X2=1001100101
7. 讓b2的值為X2中1的數量=5
8. 加密結果為 M xor (b1*b2)=>265 xor (3*5)=262
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
程式碼
進制轉換參考
一個學生正在玩一個數字編碼。這個方法是這樣的:
1. 將數字N加密到M=265
2. 將N視作是一個十進位數字:X1=265(十進位)
3. 轉換到二進制X1=100001001(二進制)
4. 讓b1相等於這個二進制1的數量,以X1即b1=3
5. 將N以16禁制表達:X2=265(HEX)
6. 轉換X2到binary X2=1001100101
7. 讓b2的值為X2中1的數量=5
8. 加密結果為 M xor (b1*b2)=>265 xor (3*5)=262
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
程式碼
#include <stdlib.h> #include <stdio.h> int main(){ int i,j,tmp,counter; int datas,rows[1000]={0}; //讀入資料 scanf("%d",&datas); for(i=0;i<datas;i++){ scanf("%d",&rows[i]); //轉二進位(紀錄DEC幾個1) tmp=rows[i]; counter=0; while(tmp!=0){ if(tmp%2==1) counter++; tmp=tmp/2; } printf("%d ",counter); //16轉10 tmp=rows[i]; int a4,a3,a2,a1,aHEX; a4=0;a3=0;a2=0;a1=0; a4=tmp/1000; a3=(tmp-a4*1000)/100; a2=(tmp-a4*1000-a3*100)/10; a1=tmp%10; aHEX=a1 +a2*16 +a3*16*16 +a4*16*16*16; //轉二進位(紀錄HEX幾個1) tmp=aHEX; counter=0; while(tmp!=0){ if(tmp%2==1) counter++; tmp=tmp/2; } printf("%d\n",counter); } }
留言
張貼留言