1 /*
2 * 第二题,输入字符串长度,字符串,计数m。从前往后计数,当数到m个元素时,m个元素出列,同时将该元素赋值给m,
3 * 然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字。输出出队队列。
4 * 例如: 输入:len=4 str="3,1,2,4" m=7
5 * 输出:2,3,1,4
6 */
7 public static String getOutString(int len, String str, int m) {
8 String[] ss = str.split(",");
9 ArrayList<Integer> arrayInt = new ArrayList<Integer>();
10 Stack<Integer> stackInt = new Stack<Integer>();
11 String s = "";
12
13 for(int i = 0; i < ss.length; i++){
14 arrayInt.add(Integer.parseInt(ss[i]));
15 }
16
17 while(arrayInt.size() != 0){
18
19
20 int n = m % arrayInt.size();//第n个数,在arrayInt 中是第 n - 1 个数
21 if(n == 0) {
22 n = arrayInt.size();
23 }
24 m = arrayInt.get(n - 1);
25 //arrayInt.remove(n - 1);
26 //arrayInt 中 第n个数开始
27 for(int i = n - 2; i >= 0; i--){
28 stackInt.push(arrayInt.get(i));
29 }
30 for(int i = arrayInt.size() - 1; i > n -1; i--){
31 stackInt.push(arrayInt.get(i));
32 }
33 System.out.println(arrayInt.remove(n - 1) +"");
34 for(int i = 0; i < arrayInt.size(); i++){
35 arrayInt.set(i, stackInt.pop());
36 }
37 }
38 return s;
39 }
 1 import java.util.ArrayList;
2 import java.util.List;
3
4
5 public class HuaWeiTest {
6
7 public static void main(String[] args) {
8 int len=4;
9 String str="3,1,2,4";
10 int m=7;
11 HuaWeiTest hwt = new HuaWeiTest();
12 System.out.println(hwt.getOutString(len, str, m));
13 }
14 public String getOutString(int len, String str, int m) {
15 String ret ="";
16 String[] arr = str.split(",");
17 List<String> ls = new ArrayList<String>();
18 for(int i=0;i<len;i++) {
19 ls.add(arr[i]);
20 }
21 for(int i=0;i<len;i++) {
22 int temp = (m-1)%ls.size();
23 ret += ls.get(temp);
24 m = Integer.parseInt(ls.get(temp))+temp;
25 ls.remove(temp);
26 }
27 return ret;
28 }
29 }
 1 package com.huawei.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Stack;
6 /**
7 * 第三题,输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。
8 * 例如: 输入:3+8×2/9-2
9 * 输出:2
10 *
11 * @author Administrator
12 *
13 */
14
15 public class HuaWeiTest {
16
17 public static void main(String[] args) {
18
19 System.out.println(getMyRet("3+8/2*2"));
20 }
21
22 public static int getMyRet(String str) {
23 String expression = str.trim();
24 Stack<Integer> operandStack = new Stack<Integer>();
25 Stack<Character> operatorStack = new Stack<Character>();
26
27 for(int i = 0; i < expression.length(); i++){
28 char ch = expression.charAt(i);
29 if(ch == '+' || ch == '-' ){//
30 while(!operatorStack.isEmpty() && (operatorStack.peek() == '+'
31 || operatorStack.peek() == '-'
32 || operatorStack.peek() == '*'
33 || operatorStack.peek() == '/')){
34 processAnOperator(operandStack, operatorStack);
35 }
36 operatorStack.push(ch);
37 }
38 else if (ch == '*' || ch == '/'){
39 while(!operatorStack.isEmpty() && (
40 operatorStack.peek() == '*' || operatorStack.peek() == '/')){
41 processAnOperator(operandStack, operatorStack);
42 }
43 operatorStack.push(ch);
44 }
45 else{
46 operandStack.push(ch - '0');
47 }
48 }
49 while(!operatorStack.isEmpty()){
50 processAnOperator(operandStack, operatorStack);
51 }
52 return operandStack.pop();
53 }
54 public static void processAnOperator(Stack<Integer> operandStack, Stack<Character> operatorStack){
55 char op = operatorStack.pop();
56 int op1 = operandStack.pop();
57 int op2 = operandStack.pop();
58 switch (op) {
59 case '+':
60 operandStack.push(op1 + op2);
61 break;
62 case '-':
63 operandStack.push(op2 - op1);
64 break;
65 case '*':
66 operandStack.push(op1 * op2);
67 break;
68 case '/':
69 operandStack.push(op2 / op1);
70 break;
71 default:
72 break;
73 }
74 }
75 }

HuaWeiJava 上机的更多相关文章

  1. flhs笔试题-回家上机实践

    这是最近参加的一个公司的笔试题,回家上机写了下代码,希望对有需要的小伙伴有用,简单实现字符串和数组在指定位置的插入: package org.flhs; import com.google.commo ...

  2. Java连接SQLServer2008终极解决办法(亲身上机演练版)

    今天我一学妹问我,Java连接SQLServer2008数据库的问题,一直无法连接成功.想起自己刚开始学习的时候,在网上找各种文章,然后实际上机验证操作,花了一两天时间才搞定,一把辛酸泪呀!记得当时是 ...

  3. 《Java语言程序设计》上机实验

    实验一   Java环境演练   [目的] ①安装并配置Java运行开发环境: ②掌握开发Java应用程序的3个步骤:编写源文件.编译源文件和运行应用程序: ③学习同时编译多个Java源文件. [内容 ...

  4. ztong上机3

    二.实验名称:数字图像处理matlab上机 三.实验学时:2学时 四.实验目的:(详细填写) 掌握几何变换 掌握插值 理解配准的概念 五.实验内容 (1)首先自己写一个对图像进行旋转和缩放的复合变换程 ...

  5. C++ 第一次上机作业

    今天完成了C++第一次上机作业,感觉比较简单. 题目: 求2个数或3个正整数中的最大数,用带有默认参数的函数实现. 对3个变量按由小到大顺序排序,要求使用变量的引用. 编写一个程序,用同一个函数名对几 ...

  6. Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)

    Twin Prime Conjecture                                            Time Limit: 2000/1000 MS (Java/Othe ...

  7. 代C语言上机实践

    这已经是开学第十二周了,个人感觉严老师教的这批学生效果不是很好,有的竟然毫不知道main函数前边的 int是做什么的.只知按照书本上给的样例程序一个字一个字的敲到编译器中,然后点击运行.有错误也不知道 ...

  8. lingo运筹学上机实验指导

    <运筹学上机实验指导>分为两个部分,第一部分12学时,是与运筹学理论课上机同步配套的4个实验(线性规划.灵敏度分析.运输问题与指派问题.最短路问题和背包问题)的Excel.LONGO和LI ...

  9. JSP脚本元素上机手册

    L3 <JSP基础>上机手册 内容回顾 脚本元素<%! %> <%= %> <% %> 注释元素 JSP指令元素 JSP动作元素 上机目标 掌握脚本元素 ...

随机推荐

  1. 【Tips】有道云笔记中Markdown插入图片

    在有道云笔记中用MarkDown插入图片 新建一个文档专门用来放图片 把所有要用的图片专门放在一个笔记里,用普通模式先同步笔记,然后用分享笔记 会有一个链接,用浏览器打开这个分享的笔记就能找到所有的图 ...

  2. Leetcode12. 整数转罗马数字Leetcode18. 四数之和

    > 简洁易懂讲清原理,讲不清你来打我~ 输入整数,输出对应的罗马字符串![在这里插入图片描述](https://img-blog.csdnimg.cn/54b001c62a0d4d348c962 ...

  3. 未开通js之前的纯css网页主题

    本主题修改自其他大佬:Rocket1184/MaterialCnblogs: Material Theme for cnblogs.com (github.com) 只需在博客后台选择"禁用 ...

  4. SpringBoot | 3.1 配置数据源

    目录 前言 1. 数据源的自动配置 2. *数据源自动配置源码分析 2.1 DataSourceAutoConfiguration:数据源自动配置类 2.2 JdbcTemplateAutoConfi ...

  5. odoo前后端交互详解

    为了简单叙述,暂时不考虑多个db的情况(主要是懒得说没有db或者多个db实例的情况)当odoo指定数据库开启服务时(也就是odoo-bin -d <some_db_name> ),我们使用 ...

  6. 第四篇--git 上传可能出现的问题

    1. Q:fatal: TaskCanceledException encountered. A task was canceled. A:$ git config --system --unset ...

  7. 01_安装电脑软件的步骤批处理脚本.bat

    REM 01_安装电脑软件的步骤批处理脚本.bat MD 01_安装电脑软件的步骤 REM ZIP解压密码空格MD 02_制作杏雨梨云USB维护系统2019中秋版之国庆更新固态U盘MD 03_复制安装 ...

  8. QT从入门到入土(四)——多线程(QtConcurrent::run())

    引言 在前面对Qt多线程(QThread)做了详细的分析:QT从入门到入土(四)--多线程(QThread) - 唯有自己强大 - 博客园 (cnblogs.com) 但是最近在做项目时候,要将一个函 ...

  9. 数据结构与算法 java描述 第一章 算法及其复杂度

    目录 数据结构与算法 java描述 笔记 第一章 算法及其复杂度 算法的定义 算法性能的分析与评价 问题规模.运行时间及时间复杂度 渐进复杂度 大 O 记号 大Ω记号 Θ记号 空间复杂度 算法复杂度及 ...

  10. Bypass D盾 Webshell

    测试环境 OS: Windows Server 2008 PHP: PHP 7.2.10 D盾: d_safe_2.1.5.4 绕过分析 我是以 eval($_POST['x']); 这个代码以根据, ...