设计一个算法,将计算机产生的n个随机数,分为奇数、偶数两组,并将它们分别压入两个栈中,然后输出在屏幕上

#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(); } }

 

程序运行结果:

你可能感兴趣的:(算法,null,ini,input,include,Numbers)