-
Recursive (using stacks automatically assigned by the system):
#include
using namespace std;
#define maxn 1000
bool isok(char *n)
int len = strlen(n);
for(int i=0; i < len; i++)if(n[i] != '0') return false;
return true;
void dec2bin(char* n)if(!isok(n))
int ys = 0;
int len = strlen(n);
int ans = (n[len - 1]-'0') %2;
for(int i=0; i
n;int len = strlen(n);
dec2bin(n);
cout non-recursive** (although it is not written in a normal way, ans is a stack):
#include
using namespace std;
#define maxn 10000
bool isend(char *n)
for(int i=0; i
0) return false;
return true;
int dec2bin()
char n[maxn];
char ans[maxn];
memset(n, 0, sizeof(n));
memset(ans, 0, sizeof(ans));
cin >>n;
int len = strlen(n);
int ans_i = 0;
for(int i=0; i
0; i--)
cout int main()
while(1)
dec2bin();
return 0;
-
This has to do with the algorithm of the base conversion. For example:
56d to 3 is converted to base system, and the specific algorithm is as follows.
The integer working variable a is stored for 56d
When a is not equal to 0, start the loop: calculate a%3, get the remainder 2, this is the lowest bit of the result (the 0th digit), save, calculate a=a 3=18
When a is not equal to 0, the cycle continues: calculate a%3, get the remainder 0, this is the first digit of the result, save, calculate a=a 3=6
When a is not equal to 0, the cycle continues: calculate a%3 to get the remainder 0, this is the second digit of the result, save, calculate a=a 3=2
When a is not equal to 0, the cycle continues: calculate a%3, get the remainder 2, this is the 3rd digit of the result, save, calculate a=a 3=0
If the cycle condition of a is not equal to 0 is not satisfied, the cycle is stopped.
At this point, we can see that the remainder we save is in reverse order, and to compose the final number, we need to output it in reverse order.
The stack happens to have a first-in, first-out feature, so we press the above remainders into the stack in order, and when the loop is over, all the remainders out of the stack will get the output in the correct order. (You can save it as a stack in the above steps).
-
Data structures, stack applications, can be converted to base through subroutines or functions, can be programmed into a word program, or a custom function can be used to convert binary, decimal, hexadecimal, etc.
-
There are a total of 24 kinds of full arrangement of the 4 elements, and the stack requirements meet the last-in-first-out
14 possibilities, 10 impossible.
The original meaning of the question is to implement the function of one queue with two stacks. >>>More
Concerning.
data[top]==data[i] to determine whether it is in the same column. >>>More
There are many kinds of plastic pallets, with a wide range of applications and pivotal jointness, according to the summary of Xuzhou Ducheng plastic products, plastic pallets are in the center of the efficiency of various logistics links such as loading and unloading, storage, transportation and packaging, and have a very important connection function, therefore, although the pallet is only a small appliance, its specifications and sizes, is the core of the packaging size, the size of the carriage and the size of the container unit. How to identify the quality of the pallet: 1 >>>More
The characteristics of the stack are:
1. First in and last out (like a tall steamer layer by layer, the buns placed in the bottom steamer can be taken out at the end), such as into 123, out of 321 >>>More
01-09 Conversion of four decimal numbers.