#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DATA 40
#define MAX 200
void tenToBin(char *str);
char bin[MAX];
int main()
{
char str[DATA];
while(scanf("%s", str) != EOF)
{
tenToBin(str);
puts(bin);
}
return 0;
}
/**
* Description:字符串整数转换为二进制
*/
void tenToBin(char *str)
{
int i, j, k, len, sum, d;
char temp;
//初始化参数
sum = 1;
len = strlen(str);
k = 0;
memset(bin, 0, sizeof(bin));
while(sum)
{
sum = 0;
for(i = 0; i < len; i ++)
{
d = (str[i] - '0') / 2;
sum += d;
if(i == len - 1)
{
bin[k ++] = (str[i] - '0') % 2 + '0';
}else
{
str[i + 1] += (str[i] - '0') % 2 * 10;
}
str[i] = d + '0';
}
}
//逆序
for(i = 0, j = k - 1; i < j; i ++, j --)
{
temp = bin[j];
bin[j] = bin[i];
bin[i] = temp;
}
}
/**************************************************************
Problem: 1138
User: Leeeeo
Language: C++
Result: Accepted
Time:120 ms
Memory:1020 kb
****************************************************************/
题目1138:进制转换
最新推荐文章于 2021-11-29 16:14:57 发布