题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String aI, aR;
try {
aI = r.readLine();
aR = r.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
char[] chsI = aI.toCharArray();
char[] chsR = aR.toCharArray();
int i = 0, j = 0, nI = 0, nR = 0, tmp = 0, times = 0, num = 0;
while (i < chsI.length) {//获得I的个数
if (chsI[i] == ' ') {
i++;
break;
}
nI = 10 * nI;
nI += chsI[i] - '0';
i++;
}
while (j < chsR.length) {//获得R的个数
if (chsR[j] == ' ') {
j++;
break;
}
nR = 10 * nR;
nR += chsR[j] - '0';
j++;
}
//---------------------------------------------------------------------------------------
int[] inI = new int[nI];
int[] inR = new int[nR];
assignArr(chsI, inI, i);
assignArr(chsR, inR, j);
//---------------------------------------------------------------------------------------
i = 0;
while (i < inR.length - 1) {
if (inR[i] == -1) {//-1跳过
i++;
continue;
}
j = i + 1;
while (j < inR.length) {
if (inR[j] == -1) {//-1跳过
j++;
continue;
}
if (inR[j] == inR[i]) {
inR[j] = -1;//去重,将重复值置为-1
}
j++;
}
i++;
}
//---------------------------------------------------------------------------------------
i = 0;//冒泡排序
while (i < inR.length - 1) {
j = 0;
while (j < inR.length - 1 - i) {
if (inR[j] > inR[j + 1]) {
tmp = inR[j];
inR[j] = inR[j + 1];
inR[j + 1] = tmp;
}
j++;
}
i++;
}
//---------------------------------------------------------------------------------------
i = 0;
StringBuffer s1 = new StringBuffer();
StringBuffer s2 = new StringBuffer();
while (i < inR.length) {
if (inR[i] == -1) {
i++;
continue;
}
j = 0;
s1.setLength(0);
times = 0;
while (j < inI.length) {
if (contain(inR[i], inI[j])) {
times++;
s1.append(" " + j + " " + inI[j]);
num += 2;
}
j++;
}
if (times > 0) {
s2.append(" " + inR[i] + " " + times);
s2.append(s1);
num += 2;
}
i++;
}
s2.insert(0, num);
System.out.println(s2);
}
//给数组赋值
public static void assignArr(char[] chs, int[] in, int i) {
boolean b = false;//初始表示没有赋过值
int k = 0, n = 0;
while (i < chs.length) {
if (chs[i] == ' ') {
if (!b) {//表示n没被赋过值,继续递增
i++;
continue;
}
in[k++] = n;
n = 0;
i++;
b = false;
continue;
}
b = true;//表示n已被赋过值
n = 10 * n;
n += chs[i] - '0';
if (i == chs.length - 1) in[k] = n;
i++;
}
}
public static boolean contain(int x, int y) {
int a = 0, b = 0, match = 0;
boolean bool = false;
char[] c1 = (x + "").toCharArray();
char[] c2 = (y + "").toCharArray();
while (b < c2.length) {
match = 0;
a = 0;
while (a < c1.length && b + a < c2.length) {
if (c1[a] == c2[b + a]) {
match++;
a++;
continue;
}
break;
}
if (match == c1.length) {
bool = true;
break;
}
b++;
}
return bool;
}
}
查看4道真题和解析
