代码拉取完成,页面将自动刷新
package test_01;
import java.util.Stack;
public class test_03_25_02
{
//棒球赛
public static void main(String[] args) {
String[] ops={"5","2","C","D","+"};
System.out.println(calPoints(ops));
}
public static int calPoints(String[] ops)
{
Stack<Integer> s=new Stack<>();
for(String op:ops)
{
switch (op)
{
case "+" ->
{
int a=s.pop();
int b=s.peek();
s.push(a);
s.push(a+b);
}
case "D" ->s.push(2*s.peek());
case "C" ->s.pop();
default ->s.push(Integer.parseInt(op))
}
}
int sum=0;
for(Integer integer:s)
{
sum+=integer;
}
return sum;
}
//if语句
/*public int calPoints(String[] ops)
{
Stack<Integer>s=new Stack<>();
for(String op:ops)
{
if(op.equals("+"))
{
int a=s.pop();
int b=s.peek();
s.push(a);
s.push(a+b);
}
else if(ops.equals("D"))
{
s.push(2*s.peek());
}
else if(op.equals("C"))
{
s.pop();
}
else
{
s.push(Integer.parseInt(op));
}
}
int sum=0;
for(int i=0;i<s.size();i++)
{
sum+=s.get(i);
}
return sum;
}*/
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。