#include <stack>
#include <cstdio>
using namespace std;
int main(){
stack<int> myStack;
//栈的大小.size()
printf("size of myStack = %d\n",myStack.size());
//压入元素.push()
for(int i=0; i<5; i++){
myStack.push(i);
}
printf("size of myStack = %d\n",myStack.size());
while(!myStack.empty()){//栈不为空时
//若栈不为空,打印栈顶元素.top()
printf("top of myQueue is %d\n",myStack.top());
//栈顶元素出栈.pop()
myStack.pop();
}
printf("myStack is empty!");
}
KY109 Zero-complexity Transposition
描述
You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.
输入描述:
For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).(用long long型)
输出描述:
For each case, on the first line of the output file print the sequence in the reverse order.
示例1
输入:
5
-3 4 6 -8 9
输出:
9 -8 6 4 -3
#include <stack>
#include <cstdio>
using namespace std;
int main(){
stack<long long> myStack;
int n;
long long num;
scanf("%d", &n);
for(int i = 0;i<n; i++){
scanf("%lld", &num);//读取long long类型的十进制数
myStack.push(num);
}
while(!myStack.empty()){
printf("%lld ",myStack.top());
myStack.pop();
}
printf("\n");
扩号匹配问题
描述
在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.
输入
输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100
注意:cin.getline(str,100)最多只能输入99个字符!
输出
对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。
样例输入
((ABCD(x)
)(rttyy())sss)(
样例输出
((ABCD(x)
$$
)(rttyy())sss)(
? ?$
#include <stack>
#include <cstdio>
#include <string>
using namespace std;
int main(){
char buf[200];
while(fgets(buf,200,stdin) != NULL){//fgets不确定数量的多行读取
string str = buf;
str.pop_back();//去掉str最后一个换行字符
stack<unsigned> indexStack;//存储左圆括号的下标
string res;//保存输出的结果
for(unsigned i=0; i<str.size();i++){
if(str[i] == '('){
indexStack.push(i);//把左圆括号的下标压栈
//暂时认为该左圆括号非法
res.push_back('$');
}
else if(str[i] == ')'){
if(indexStack.empty()){
res.push_back('?');//若存储左圆括号的栈为空,则右括号不匹配
}
else{
res.push_back(' ');//若存储左圆括号的栈为不空,则右括号匹配
res[indexStack.top()]= ' ';//上面暂时认为非法的左圆括号通过indexStack下标改为合法
indexStack.pop();//弹出匹配的左圆括号
}
}
else{
res.push_back(' ');//既不是左又不是右圆括号,输出结果压入空格
}
}
printf("%s\n%s\n",str.c_str(),res.c_str());
}
}