Use stack to solve C++ problems, example 1:

/*Title introduction: put a string of numbers you see (the length is not necessarily, it ends with 0, and the maximum number is not more than 100,

The number does not exceed 2^32-1)
memorize it and read it backwards (don't read the number 0 indicating the end).
Programming solves this problem. */

 

# include <bits/stdc++.h> //Universal header file, don't say anything, search the Internet by yourself

using namespace std;

int main ()

{

stack< int> s; //define a stack

while (1)

{

int a; scanf("%d",&a);

if (!a) break; //Determine whether to end

s.push(a); //Push a onto the stack

}

while (!s.empty())

{

cout<<s.top(); //Output the top element of the stack

printf( " "); s.pop(); //The first element on the top of the stack plays

}

cout<< "\n"; // newline ignore it =-= return 0;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250474&siteId=291194637