Code chuyển thành xâu nhị phân.
// bai3.cpp : Defines the entry point for the console application.
// code Dung NV
#include"stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<string.h>
char*nhiphan(int n)
{
inti=0,j,k,x;
chars[33],d[3]="01",tg;
x=n;
while(x!=0)
{
k=x%2;
s[i]=d[k];
i++;
x=x/2;
}
j=i;
for(i=0;i<(j/2);i++)
{
tg=s[i];
s[i]=s[j-i-1];
s[j-i-1]=tg;
}
s[j]=NULL;
return&s[0];
}
int_tmain(int argc, _TCHAR* argv[])
{
intn;
charnp[33];
printf("\nNhap N= ");
scanf("%d",&n);
strcpy(np,nhiphan(n));
printf("\nchuen %d thanh xau nhi phan : %s",n,np);
getch();
return0;
}
No comments:
Post a Comment