Repository navigation
Expand file tree
/
Copy path4.10.cpp
More file actions
103 lines (92 loc) · 1.47 KB
/
Copy path4.10.cpp
File metadata and controls
103 lines (92 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
//手写栈
#define N 100009
int ans = 0;
int cnt = 0;
typedef struct
{
int data[N];
int topit;
} sqstk;
int qp(int a, int b)
{
if (b == 0) return 1;
if (b % 2 == 1) return qp(a, b / 2) * qp(a, b / 2) * a;
if (b % 2 == 0) return qp(a, b / 2) * qp(a, b / 2);
return 1;
}
void push(sqstk &l, int e) //压栈
{
if (l.topit == N - 1)
{
printf("Stack is full! \n");
return;
}
l.data[l.topit++] = e;
return;
}
void pop(sqstk &l) //弹出
{
if (l.topit == 0) return;
--l.topit;
return ;
}
int emporful(sqstk &l)
{
if (l.topit == 0)
{
return 1;
}
if (l.topit == N - 1)
{
return 2;
}
return 0;
}
void show(sqstk &l)
{
int tmp = l.topit - 1;
while (tmp != -1)
{
printf("%d", l.data[tmp--]);
}
printf("\n");
}
void ttb(int x) //十 to 二
{
printf("原数字:%d ", x);
sqstk res;
res.topit = 0;
int cnt = 0;
while (x > 0)
{
push(res, x & 1);
x = x >> 1;
}
printf("二进制: ");
show(res);
}
void btt(int x) //二 to 十
{
printf("原数字:%d ", x);
sqstk res;
res.topit = 0;
while (x > 0)
{
push(res, x % 10);
x = x / 10;
cnt++;
}
while (cnt--)
{
ans += qp(2, cnt) * res.data[cnt];
}
printf("十进制: %d ", ans);
}
int main()
{
ttb(100);
btt(1111);
return 0;
}