2011年4月24日 星期日

分享

高中生程式解題系統 a034: 二進位制轉換


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void) 
{

    int input;
    int length;
    int i;

    while(scanf("%d",&input)!=EOF)
    {
        for(i=1;i<=30;i++)
        {
            if(pow(2,i)>input)
            {
                length = i;
                break;
            }            
        }

        int temp[100] = {0};
        
        for(i=0;i<length;i++)
        {
            temp[i] = input % 2;
            input /= 2;
        }

        for(i=length-1;i>=0;i--)
            printf("%d",temp[i]);

        printf("\n");

    }
    
    return 0;
}

沒有留言:

張貼留言