LeeBlog

导航

HDU 2051 Bitset

    表示从这题中学到了一个地方,定义字符串的时候最好将其初始化为'\0',并表示以后多用memset

代码
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
int main ( )
{
int n,d,i,len;
char s[100];
while ( scanf ( "%d",&n ) != EOF )
{
memset ( s,
0,sizeof ( s ) );
i
= 0;
if ( !n )//这里很容易漏
s[0] = '0';
while ( n )
{
s[ i
++ ] = n % 2 + '0';
n
= n / 2;
}
len
= strlen ( s );
while ( len-- )
printf (
"%c",s[len] );
puts (
"" );
}
return 0;
}

 

posted on 2011-01-27 14:56  LeeBlog  阅读(259)  评论(0编辑  收藏  举报