PTA:10進数のバイナリ(15分)(再帰と非再帰的な方法)

正の整数nは、バイナリ出力に変換された後に、この問題は、機能を達成するために必要。

関数インタフェースの定義:
無効dectobin(N-INT);

Dectobinは、行のバイナリnの関数を印刷しなければなりません。勧告は、再帰的に実装されています。

審判試験サンプルプログラム:
書式#include <stdio.hに>

無効dectobin(int型n)を。

メイン()がINT
{
INT nを、

scanf("%d", &n);
dectobin(n);

return 0;

}

/ *あなたのコードはここに埋め込まれます* /

サンプル入力:
10

出力サンプル:
1010

再帰

void dectobin( int n )
{
	if (n == 0)
		printf("0");
	else if (n == 1)
		printf("1");
	else
	{
		dectobin(n / 2);
		printf("%d", n % 2);
	}
		
} 

非再帰的な方法

void dectobin( int n )
{
    int a[10];
    int temp, i;

    do
    {
        a[i++] = n % 2;
    }while ((n /= 2) != 0);
    for (--i; i >= 0; i--)
        printf("%d", a[i]);
}
公開された58元の記事 ウォン称賛21 ビュー598

おすすめ

転載: blog.csdn.net/qq_45624989/article/details/105418320