DASBD72

11127_Binary_representation_and_sum

Oct 1st, 2020
908
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. int main(){
  3.     int N, carries=0, binary[20]={}, i = 0;
  4.     int isNotEnd = 1;
  5.     scanf("%d", &N); //input
  6.     N++;
  7.     //turn into binary and count
  8.     while(N){
  9.         binary[i++] = N%2;
  10.         //計算進位數量
  11.         if(isNotEnd){
  12.             if(N%2 == 0) carries++;
  13.             else isNotEnd = 0;
  14.         }
  15.         N /= 2;
  16.     }
  17.     while(i){
  18.         i--;
  19.         printf("%d", binary[i]);
  20.     }
  21.     printf(" %d", carries);
  22.     return 0;
  23. }
RAW Paste Data