2015年7月13日 星期一

[C][瘋狂程設][CPE考題20150625] 15C2.UVA10931:Parity


Problem E - Parity

Time Limit: 1 second We define the parity of an integer n as the sum of the bits in binary representation computed modulo two. As an example, the number 21 = 101012 has three 1s in its binary representation so it has parity 3 (mod 2), or 1. In this problem you have to calculate the parity of an integer 1 ≤ I ≤ 2147483647.

Input

Each line of the input has an integer I and the end of the input is indicated by a line where I = 0 that should not be processed.

Output

For each integer I in the inputt you should print a line The parity of B is P (mod 2)., where B is the binary representation of I.

Sample Input

1 2 10 21 0

Sample Output

The parity of 1 is 1 (mod 2).
The parity of 10 is 1 (mod 2).
The parity of 1010 is 2 (mod 2).
The parity of 10101 is 3 (mod 2).

注意事項


1.讀取資料到字元0:


 int input;
 scanf("%d",&input);
 while(input!=0){....}

2.利用 link list 的結構,宣告指標,儲存2進位數字:

 char *data;
 count=0;
 *(data+count)=num%BASE;


3.數字10進位轉2進位,並且將結果儲存到data:


while(num>0){
 int i=num%BASE;
 *(data+count)=num%BASE;
 count++;
 num = num/BASE;
}

並且輸出結果:
for(int j=count-1;j>=0;j--){
 printf("%d",*(data+j));
}

4.計算parity位數:

 (i==1) ? (parity+=1) : (parity+=0);

Code:

#include <stdio.h>

#define BASE 2

int main(void){

 int input,num,parity,count;
 char *data;

 scanf("%d",&input);

 while(input!=0){
 
  count=0;
  parity=0;
  num=input;
  
  while(num>0){
   int i=num%BASE;
   *(data+count)=num%BASE;
   count++;
   (i==1) ? (parity+=1) : (parity+=0);
   num = num/BASE;
  }

  printf("The parity of ");  
  for(int j=count-1;j>=0;j--){
   printf("%d",*(data+j));
  }
  
  printf(" is %d (mod %d).\n",parity,BASE);
  scanf("%d",&input);
 }

 return 0;
}



[2015/07/20][update]利用Arithmetic shift算術位移的方式改寫]


1.取出最右邊的bit:利用位元運算子'&'

  bit = 01 & num; //get the rightest bit.

2.將原數字除以2:利用位元運算子 right shift右移'>>'

  num >>= 1; // num divided by 2

3.將原數字轉成二進位的數字

從link list的1號位置開始,逐一存放每個bit的值,若有8個bits,則存放在第1號到第8號位置。(第0號位置空下來)
實作方法:用count來計算位置,注意count的起始值、以及count++的順序擺放、列印二進位數值時for loop的起點與終點寫法。
 int input,num,bit,parity,count;
 int *binary;
 
  num=input;
  bit=parity=count=0;
  
  printf("The parity of ");
  while(num>0){
   bit = 01 & num;
   parity += bit;
   count++;
   *(binary+count)=bit;
   num >>= 1;
  }
  
  for(int i=count;i>0;i--)
   printf("%d",*(binary+i));
   
  printf(" is %d (mod 2).\n",parity);

4.讀取資料的中止條件:

讀取資料直到尾端:while(scanf("%d",&input)!=EOF){...}
若讀取到的數值為0,則使用"break;"跳出 while loop,接著來到 return 0; 程式終止。
 while(scanf("%d",&input)!=EOF){
 
  if(input==0)
   break;
               ......
 }
        return 0;

Code:

#include <stdio.h>

int main(void){
 
 int input,num,bit,parity,count;
 int *binary;
 
 while(scanf("%d",&input)!=EOF){
 
  if(input==0)
   break;
   
  num=input;
  bit=parity=count=0;
  
  printf("The parity of ");
  while(num>0){
   bit = 01 & num; //get the rightest bit.
   parity += bit; // calculate the parity.
   count++;
   *(binary+count)=bit;
   num >>= 1; // num divided by 2
  }
  
  for(int i=count;i>0;i--)
   printf("%d",*(binary+i));
   
  printf(" is %d (mod 2).\n",parity);
 }
 
 return 0;
}

3 則留言:

  1. 這一題應該是要練習「算術移位」吧…

    參考資料:https://en.wikipedia.org/wiki/Arithmetic_shift

    回覆刪除
    回覆
    1. 改用算術位移的方式計算pariry,以及轉換成二進位的數字:

      int input,num,bit,parity,count;
      int *binary;

      num=input;
      bit=parity=count=0;


      while(num>0){
      bit = 01&num; //get the rightest bit.
      parity += bit; // calculate the parity.
      count++;
      *(binary+count)=bit;
      num >>= 1; // num divided by 2
      }

      刪除