题目:http://codeup.cn/problem.php?cid=100000579&pid=2
正确代码(已测验)看不懂memcpy0
#include <cstdio>
int main() {
char s[32]; //将十进制字符串转换为倒排的二进制字符串, 需模拟多次数字取余和除法
while (gets(s)) {
char nums[100] = {};
int numsSize = 0, sum = 1; //全十进制字符串
while (sum) { //当十进制字符还未除完时继续循环
sum = 0; //每一次十进制字符串除以2都恢复0
for (int i = 0; s[i]; i++) {
int digit = s[i] - '0';
int x = digit / 2;
sum += x;
if (s[i + 1]) {
s[i + 1] += (digit % 2 * 10);
} else {
nums[numsSize++] = digit % 2 + '0'; //从低位向高位存入取余的字符
}
s[i] = x + '0'; //一位字符的变化
}
}
for (int k = numsSize - 1; k >= 0; k--) {
printf("%c", nums[k]);
}
printf("\n");
}
return 0;
}
我的代码比较简单,但不能通过
#include<stdio.h>
int main(){
long long int a;
while(scanf("%d",&a)!=EOF){
int y[100]={0},num=0;
do{
y[num++]=a%2;
a/=2;
}while(a!=0);
int i=0;
for(i=num-1;i>=0;i--){
printf("%d",y[i]);
}
printf("\n");
}
return 0;
}