题目
本题要求实现一个函数,将非负整数n转换为二进制后输出。
函数接口定义:
void dectobin( int n );
函数dectobin
应在一行中打印出二进制的n
。建议用递归实现。
裁判测试程序样例:
#include <stdio.h>
void dectobin( int n );
int main()
{
int n;
scanf("%d", &n);
dectobin(n);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10
输出样例:
1010
代码长度限制 16 KB
时间限制 400 ms
内存限制 64 MB
题解
void dectobin(int n)
{
int temp=n;
int str[16];
int count=0;
for(int i=0;temp!=0;i++)
{
str[i]=temp%2;
temp/=2;
count++;
}
for(int i=0;i<count;i++)
{
printf("%d",str[count-i-1]);
}
if(n==0) printf("0");
}