2026天梯赛模拟赛9-10
2026/4/14 13:41:56 网站建设 项目流程

L2-1 简单计算器

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); Stack<Integer> num=new Stack<>(); Stack<String> op=new Stack<>(); String[] s = br.readLine().split(" "); String[] s1 = br.readLine().split(" "); for (int i=0;i<n;i++){ num.push(Integer.parseInt(s[i])); } for (int i=0;i<n-1;i++){ op.push(s1[i]); } while (num.size()!=1&&!op.isEmpty()){ int n1=num.pop(); int n2=num.pop(); String pop = op.pop(); if (pop.equals("+")){ num.push(n2+n1); } if (pop.equals("-")){ num.push(n2-n1); } if (pop.equals("*")){ num.push(n2*n1); } if (pop.equals("/")){ if (n1==0){ System.out.println("ERROR: "+n2+"/"+n1); return; } num.push(n2/n1); } } System.out.println(num.pop()); } }

L2-2 为 i 做 e

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); Map<String, String> map = new HashMap<>(); for (int i = 0; i < n; i++) { String[] s = br.readLine().split(" "); map.put(s[0], s[1]); } int m=Integer.parseInt(br.readLine()); List<Integer> list=new ArrayList<>(); for (int i=0;i<m;i++){ String[] s = br.readLine().split(" "); boolean t=true; for (int j=1;j<s.length;j++){ if (map.get(s[j]).equals("e")){ t=false; break; } } if (t){ list.add(i+1); } } if (list.size()==0){ System.out.println("None"); return; } System.out.print(list.get(0)); for (int i=1;i<list.size();i++){ System.out.print(" "+list.get(i)); } } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询