"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "048-parameter Resolution" "Project Download" topic description
在命令行输入如下命令: 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.输入由用例保证,不会出现不符合要求的输入
Enter a description
输入一行字符串,可以有空格
Output description
输出参数个数,分解后的参数,每个参数都独占一行
Input example
xcopy /s c:\\\ d:\\
Output example
4xcopy/sc:\\d:\\
Algorithm implementation
ImportJava.util.ArrayList;ImportJava.util.List;ImportJava.util.Scanner;/** * Author: Wang Junshu * date:2015-12-25 08:31 * declaration:all rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {String input = Scanner.nextline (); SYSTEM.OUT.PRINTLN (arganalysis (input)); } scanner.close (); }/** * Analysis parameters, "there is another way, is directly using the array to deal with, can reduce the complexity of time and space complexity, processing will be troublesome" * @param S * @return */
Private StaticStringarganalysis(String s) {List<string> List =NewArraylist<> ();//Indicates the last "occurrence of the next position, if not" is 0 intPrev =0; for(inti =0; I < s.length (); ) {first double quote starting with//I if(S.charat (i) = =' "') {//Add a string that does not wrap double quotesList.add (s.substring (prev, i)); for(intj = i +1; J < S.length (); J + +) {//Start with a second double quote if(S.charat (j) = =' "') {List.add (S.substring (i, J +1)); i = j +1; prev = i; } } }Else{i++; } }//"1"//// indicates no "//if (prev = = 0) {//List.add (s);// }//// and last one no/Else if (prev < s.length ()) {//List.add (s.substring (prev, s.length ()));// } //"2", "1" "2" to achieve the same effect if(Prev < s.length ()) {List.add (s.substring (prev, s.length ())); }//Handle spaces below, double quotation marks cause string not to be processed intresult =0; StringBuilder Builder =NewStringBuilder (); for(String t:list) {//If you do not start with the if(!t.startswith ("\"") {string[] sts = T.split ("\\s+"); result + = Sts.length; for(String st:sts) {Builder.append (ST). Append (' \ n '); } }Else{result++; Builder.append (t). Append (' \ n '); } }returnResult +"\ n"+ builder.substring (0, Builder.length ()-1); }}
"Huawei OJ" "048-parameter Resolution"