当前位置: 代码迷 >> J2SE >> 华为上机题如何输入
  详细解决方案

华为上机题如何输入

热度:101   发布时间:2016-04-23 19:38:27.0
华为上机题怎么输入?
比如,题目为:第一题,输入字符串长度len1,字符串s1,字符串长度len2,字符串s2。从后向前比较,以最短字符串为标准,输出不同的元素的个数。
    例如:  输入:s1="1,3,5"   len1=3        s2="2,4,1,7,5"   len2=5  
             输出:2  
华为自动判卷应该使用scanner吗?
这四个变量是从每一行输入的吗?
Scanner s = new Scanner(System.in);
int len1 = s.nextLine;
String s1 = s.nextLine;
int len2 = s.nextLine;
String s2 = s.nextLine;这样不对吧。。。。。
哪位大神能给出完整程序,多谢了
ps:我只会Java
------解决思路----------------------
看不懂你的题,不同元素啥意思 除了5 和1 其他都不一样 2是咋来的
------解决思路----------------------
这个题我都没看明白,为什么结果是2?
------解决思路----------------------
什么鬼……哪来的题……
------解决思路----------------------
答案应该是3. 

package net.csdn.question;

import java.util.Scanner;
import java.util.ArrayList;

public class GetDifferentLetterCount {
public static void main(String[] args) {
getDifferentLetterCount();
}

private static void getDifferentLetterCount() {
Scanner input = new Scanner(System.in);
System.out.println("Input the total of the String(Integer Only):");
int time = 0;
try {
time = new Integer(input.nextLine());
if (time == 1) {
throw new NumberFormatException("1 is not Valid");
}
} catch (NumberFormatException e) {
System.err.println("Unvalid Integer Format!");
System.exit(1);
}

String[][] strs = new String[time][];
String content = null;
int index = 0;
int minLength = 0;

for (int i = 0; i < time; i++) {
System.out.println((i + 1) + ": please input the length of the String seperate by semicolon.(eg: a,b,c)");
content = input.nextLine();
strs[i] = content.split(",");
}

minLength = strs[0].length;
for (int i = 0; i < time; i++) {
// 循环获取最小数组,并获取对应的index
if (strs[i].length < minLength) {
minLength = strs[i].length;
index = i;
}
}

ArrayList<ArrayList<String>> allArrays = new ArrayList<ArrayList<String>>();
ArrayList<String> minArray = null;// 用来存储最包含最少String对象的数组.
ArrayList<String> temp = new ArrayList<String>(); // 用于内部循环.
String minArrayObject = null; // 用来存储最短数组里面的内容

for (int i = 0; i < time; i++) {
for (int j = 0; j < strs[i].length; j++) {
temp.add(strs[i][j]);
}
allArrays.add(temp);
temp = new ArrayList<String>();
}

minArray = allArrays.remove(index);

while (allArrays.size() != 0) {
temp = allArrays.remove(0);
for (int i = 0; i < minArray.size(); i++) {
minArrayObject = minArray.get(i);
while (temp.remove(minArrayObject)) {
// 这里循环删除temp中包含minArrayObject.
}
}
System.out.print(temp.size() + ":");// 输出删除后temp内包含String的个数.
for (int j = 0; j < temp.size(); j++) {
// 输出temp中剩余的String的内容
System.out.print(temp.get(j) + ",");
}
System.out.println();
}
input.close();
}
}

  相关解决方案