题解 | #二进制数#
二进制数
https://www.nowcoder.com/practice/103dd589fed14457a673c613d8de3841
#include <stdio.h>
#include<stdbool.h>
#define MAXSIZE 30
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;
} Stack_int;
// 初始化一个数字栈:
void InitStack(Stack_int* S) {
S->top = -1;
}
// 进栈:
bool Push(Stack_int* S, ElemType x) {
if (S->top == MAXSIZE - 1)
return false;
else {
S->data[++S->top] = x;
return true;
}
}
// 出栈:
bool Pop(Stack_int* S, ElemType* x) {
if (S->top != -1) {
*x = S->data[S->top--];
return true;
} else
return false;
}
int main() {
int n;
while ((scanf("%d", &n)) != EOF)
{
Stack_int S;
InitStack(&S);
while (n != 0) {
int x = n % 2;
Push(&S, x);
n /= 2;
}
while (S.top != -1) {
int y = 0;
Pop(&S, &y);
printf("%d", y);
}
printf("\n");
}
return 0;
}