输入的小知识

1.10 编写程序选用顺序存储结构和链式存储结构实现抽象数据类型栈和队列,再利用栈和队列,输入若干个整数,将输入后的正整数和负整数分别保存起来,输入完成后,首先将以输入相反的次序输出所有保存的正整数,再以输入相同次序输出所有保存的负整数,。

输入格式:
若干非0整数。

输出格式:
正整数和负整数输出各占一行​,每个数占5位。

输入样例:
100 2 3 -2 -8 -6 -9 -10 50 2 -1
输出样例:
2 50 3 2 100
-2 -8 -6 -9 -10 -1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB

解决代码:

include<bits/stdc++.h>

using namespace std;

int main()
{
long temp;
stack st;
queue qu;
while(cin>>temp){
if(temp>0){
st.push(temp);
}
if(temp<0){
qu.push(temp);
}
}
bool flag=0;
if(!st.empty()){
flag=1;
}
while(!st.empty()){
cout<<setw(5)<<st.top();
st.pop();
}
if(flag){
cout<<endl;
}
while(!qu.empty())
{
cout<<setw(5)<<qu.front();
qu.pop();
}
}

在c++中,使用ctrl+z可以停止输入

posted @ 2026-03-14 23:23  暗神酱  阅读(12)  评论(0)    收藏  举报