用Java解决牛客网华为机试题目HJ74

103 篇文章 1 订阅
89 篇文章 0 订阅

题目描述

在命令行输入如下命令:

xcopy /s c:\ d:\,

各个参数如下: 

参数1:命令字xcopy 

参数2:字符串/s

参数3:字符串c:\

参数4: 字符串d:\

请编写一个参数解析程序,实现将命令行各个参数解析出来。

 

解析规则: 

1.参数分隔符为空格 
2.对于用“”包含起来的参数,如果中间有空格,不能解析为多个参数。比如在命令行输入xcopy /s “C:\program files” “d:\”时,参数仍然是4个,第3个参数应该是字符串C:\program files,而不是C:\program,注意输出参数时,需要将“”去掉,引号不存在嵌套情况。
3.参数不定长 
4.输入由用例保证,不会出现不符合要求的输入 
 

 

输入描述:

 

输入一行字符串,可以有空格

输出描述:

 

输出参数个数,分解后的参数,每个参数都独占一行

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String s;

		while ((s = br.readLine()) != null) {

			String str[] = s.split(" ");

			ArrayList<String> ss = new ArrayList<>();

			// int sum = 0;

			String s1 = "";

			String s2 = "";
			// System.out.println(sum);
			int temp = 0;
			for (int i = 0; i < str.length; i++) {

				if (temp == 0) {

					if (str[i].charAt(0) != '“' && str[i].charAt(str[i].length() - 1) != '”') {

						ss.add(str[i]);
						// System.out.println(str[i]);
					}

					else if (str[i].charAt(0) == '“' && str[i].charAt(str[i].length() - 1) != '”') {

						temp = 1;

						str[i] = str[i].substring(1);
						s1 = s1 + str[i] + s2;

					} else if (str[i].charAt(0) == '“' && str[i].charAt(str[i].length() - 1) == '”') {

						ss.add(str[i].substring(1, str[i].length() - 1));
						// System.out.println(str[i].substring(1, str[i].length() - 1));
					}

				} else if (temp == 1) {

					if (str[i].charAt(0) != '“' && str[i].charAt(str[i].length() - 1) != '”') {

						s1 = s1 + " " + str[i] + s2;
					} else if (str[i].charAt(0) != '“' && str[i].charAt(str[i].length() - 1) == '”') {

						temp = 0;

						ss.add(s1 + " " + str[i].substring(0, str[i].length() - 1));
						// System.out.println(s1 + " " + str[i].substring(0, str[i].length() - 1));
					}

				}

			}

			System.out.println(ss.size());

			for (int i = 0; i < ss.size(); i++) {

				System.out.println(ss.get(i));
			}

		}

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,牛客网华为试题解的JavaScript版本提供了第11到20题的解答。其中包括了数字颠倒、字符串反转、句子逆序、字符串排序、求int型数据在内存中存储时1的个数、购物单、坐标移动、识别有效的IP地址和掩码并进行分类统计、简单错误记录和密码验证合格程序。 根据引用,题描述了如何将输入的整数以字符串的形式逆序输出。程序不考虑负数的情况,如果数字中包含0,逆序形式也会包含0。 根据引用,题描述了如何计算输入的正整数在内存中存储时1的个数。题要求输入一个整数(int类型),并将该数转换成二进制形式后输出其中1的个数。 需要注意的是,输入和输出的具体实现可能因题而异,以上引用提供了一些示例代码,但并不代表所有题的通用解法。正确的输入输出取决于具体题的要求和所给代码的实现方式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JavsScript牛客网华为试(11-20)题解](https://blog.csdn.net/weixin_43465339/article/details/110709521)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值