#include "stdafx.h" #include<iostream> #include<cstdlib> #include<ctime> #include<stack> using namespace std; void PrintStack(stack<int> mm); int _tmain(int argc, _TCHAR* argv[]) { stack<int> StackOdd,StackEven; int n; int m; srand(time(NULL)); cout<<"Please input the total numbers:"<<endl; cin>>n; for(int i=0;i<n;i++) { m=rand()%100; if(m%2==0) StackEven.push(m); else StackOdd.push(m); } cout<<"The odd numbers are:"<<endl; PrintStack(StackOdd); cout<<endl; cout<<"The even numbers are:"<<endl; PrintStack(StackEven); cout<<endl; system("pause"); return 0; } void PrintStack(stack<int> mm) { while(!mm.empty ()) { cout<<mm.top()<<"/t"; mm.pop(); } } 程序运行结果: