十进制数转换为二进制数
Description
輸入一個(gè)十進(jìn)制整數(shù),將其轉(zhuǎn)換為二進(jìn)制數(shù),并將轉(zhuǎn)換結(jié)果輸出。
Input
多組測(cè)試數(shù)據(jù),每組輸入一個(gè)十進(jìn)制整數(shù)。
Output
將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制數(shù),輸出轉(zhuǎn)換結(jié)果。
Sample Input
10
15
Sample Output
1010
1111
#include<stdio.h>
int main()
{
int n,s[20],i=0,rem;
while(scanf("%d",&n)!=EOF)
{
while(n){ // while(n)等價(jià)于while(n!=0)
rem=n%2;
n=n/2;
s[i]=rem;
i++;//這里的i++最后還執(zhí)行了一遍導(dǎo)致無(wú)效化,所以下面有個(gè)–i,先-1再執(zhí)行
}
while(i>0)
printf("%d",s[–i]);
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n,s[20],i=0,rem;
while(scanf("%d",&n)!=EOF)
{
while(n){
rem=n%2;
n=n/2;
s[i]=rem;
i++;
}
while(i>0)
{
i=i-1;
printf("%d",s[i]);
}
printf("\n");
}
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的十进制数转换为二进制数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql native数据同步_记一次
- 下一篇: anki 插入表格_Anki之导出卡牌组