Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() (int, error) {
	n, err := strconv.Atoi(nextString())
	return n, err
}

func main() {
	scanner.Split(bufio.ScanWords)

	stack := []int{}
	for {
		n, err := nextInt()
		if err != nil {
			break
		}

		if n == 0 { // pop
			fmt.Println(stack[len(stack)-1])
			stack = stack[0:(len(stack) - 1)]
		} else { // push
			stack = append(stack, n)
		}
	}
}

C++:  /*
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.

We can simulate the movement (comings and goings) of the cars as follow:

An entry of a car is represented by its number.
An exit of a car is represented by 0
For example, a sequence

1 6 0 8 10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.

Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
 */
 
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	vector<int> cars, ans, done(10000000000000, -1);
	int temp, i=0, m;
	while (cin >> temp) {
		if (temp != 0) {
			cars.push_back(temp);
			i++;
		} else {
			sort(done.begin(), done.end());
			if (!binary_search(done.begin(), done.end(), cars.at(i-1))) {
				ans.push_back(cars.at(i-1));
				done.push_back(i-1);
			}
		}
	}
	for (int j=0; j<ans.size(); j++)
		cout << ans[j] << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
	stack<int> s;
	int num;
	while(cin >> num){
		if(num == 0){
			cout << s.top() << endl;\
			s.pop();
		}else{
			s.push(num);
		}
	}
}

C++: #include<iostream>
#include<stack>

using namespace std;

int main(){
	stack<int> st;
	int car;
	cin >> car;
	st.push(car);
	if(car == 0) return 0;
	while( !st.empty() ){
		int inout;
		cin >> inout;
		if(inout == 0){
			cout << st.top() << endl;
			st.pop();
		}else{
			st.push(inout);
		}
	}
	return 0;
}

C++: #include <stdio.h>
 
int main(void) {
    int A[10];
    int m=0, n;
    while(!feof(stdin)) {
        scanf("%d",&n);
        if(feof(stdin))break;
        if(n==0) {
            m--;
            printf("%d\n",A[m]);
        } else {
            A[m]=n;
            m++;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <stack>

std::stack<int> st;
int main() {
	int x = 0;
	while(std::cin >> x) {
		if(x == 0) {
			int y = st.top(); //トップの値を取得
			st.pop(); //トップの値を消去(pop)
			std::cout << y << std::endl;
		} else {
			st.push(x); //トップに値を積む(push)
		}
	}
}

C++:  /*
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.

We can simulate the movement (comings and goings) of the cars as follow:

An entry of a car is represented by its number.
An exit of a car is represented by 0
For example, a sequence

1 6 0 8 10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.

Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
 */
 
#include <iostream>
#include <vector>

using namespace std;

int main() {
	vector<int> cars, ans;
	int temp, i=0, s=0, m;
	while (cin >> temp) {
		if (temp != 0) {
			cars.push_back(temp);
			i++;
		} else {
			m = i-s;
			ans.push_back(cars.at(m-1));
			s++;
		}
	}
	for (int j=0; j<ans.size(); j++)
		cout << ans[j] << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>

int main(int argc,char* argv[]){
  std::vector<int> vec;
  
  while(!std::cin.eof()){
    int temp;
    std::cin >> temp;
    if(temp == 0){
      std::cout << vec.back() << std::endl;
      vec.pop_back();
      continue;
    }else{
      vec.push_back(temp);
      continue;
    }
  }

  return 0;
  
}

C++: #include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

int main()
{
	int n;
	stack<int> car;
	vector<int> out;

	while (cin >> n){
		if (n == 0){
			out.push_back(car.top());
			car.pop();
		}
		else {
			car.push(n);
		}
	}

	for (int i = 0; i < out.size(); i++){
		cout << out[i] << endl;
	}

	return (0);
}

C++: #include<iostream>
#include<queue>
#include<string>
#include<stack>
#include<cstdio>
#include<algorithm>

using namespace std;

int main(void)
{
  stack <int> n;
  int a;

  while(scanf("%d", &a)!=EOF)
    {
      if(a==0)
	{
          cout<<n.top()<<endl;
          n.pop();
	}

      else
        n.push(a);
    }
}

C++: #include<iostream>
using namespace std;
int main(){
  int i=0,num,a[10],b[10],x=0;
  
  while(cin>>num){
    int count=0;
    count++;
    if(num==0){
      b[x++]=a[i-1];
      i--;
    }
    else  a[i++]=num;
    if(!i)break;
  }

  for(i=0;i<x;i++)
    cout<<b[i]<<endl;
  return 0;
}

C++: #include<iostream>
#include<stack>
using namespace std;

int main(){
	int x;
	stack<int> S;
	while(1){
		cin>>x;
		if(x==0){
			cout<<S.top()<<endl;
			S.pop();
		}else{
			S.push(x);
		}
	}
	return 0;
}

C++: #include <fstream>
#include <iostream>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

int main() {
  stack<int> st;
  int n;
  while (cin >> n) {
    if (n == 0) {
      cout << st.top() << endl;
      st.pop();
    } else {
      st.push(n);
    }
  }
}

C++: #include <cstdio>
#include <stack>
using namespace std;
int main(){
	int n;
	stack<int> car;
	while(~scanf("%d",&n)) {
		if(n!=0) car.push(n);
		else{
			printf("%d\n",car.top());
			car.pop();
		}
	}	
	return 0;
}

C++: #include <iostream>
#include <stack>

int main(){
	
	std::stack<int> train;
	int num;
	
	while(std::cin >> num){
		if(num == 0){
			std::cout << train.top() << std::endl;
			train.pop();
		}else{
			train.push(num);
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
  stack<int> st;
  int d;
  do {
    cin >> d;
    if(d == 0){
      if(st.empty())break;
      cout << st.top() << endl;
      st.pop(  );
    }else {
      st.push( d );
    }
  }while(!st.empty());
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  stack<int> s;
  int n;
  while (cin >> n) {
    if (n == 0) {
      cout << s.top() << endl;
      s.pop();
    } else {
      s.push(n);
    }
  }
  return 0;
}

C++: #include <iostream>
#include <list>
using namespace std;

int main(){
  list<int> rails;
  int car;

  while(cin >> car){
    if(car == 0){
      cout << *(--rails.end()) << endl;
      rails.pop_back();
    }
    else
      rails.push_back(car);
  }
  
  return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;

int main(void) {
  int n;
  stack<int> s;
  
  cin >> n;
  s.push(n);
  
  while(!s.empty()) {
    cin >> n;
    if(n == 0) { n = s.top(); s.pop(); cout << n << endl; }
    else s.push(n);
  }
  
  return 0;
}