a034.二進位轉換

a034.二進位轉換



解題思路 : 先知道十進位轉二進位的方法,開一個陣列紀錄每一位處理好的數,再從陣列的最後一個數開始輸出

程式碼

/*
/*
a034二進位轉換
skyblue
AC (2ms, 324KB) 
*/

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    int a[50];

    while(scanf("%d", &n) != EOF){
        int i = 0;
        
        //轉換成二進位
        while(n > 0){
            a[i] = n%2;
            n /= 2;
            i++;
        }

        //從陣列的最後一個數開始輸出
        for(int c = i-1; c>=0; c--){
            printf("%d", a[c]);
        }
        printf("\n");
    }

    return 0;
}



留言

這個網誌中的熱門文章

a024.最大公因數(GCD)

d066.上學去吧!