- 1
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t != 0) {
int n = in.nextInt();
int m = in.nextInt();
long[] w = new long[n];
for (int j = 0; j < w.length; j++) {
w[j] = in.nextLong();
}
long[] x = new long[m];
for (int k = 0; k < x.length; k++) {
x[k] = in.nextLong();
}
for (int l = 0; l < w.length; l++) {
for (int o = 0; o < x.length; o++) {
if (w[l] == x[o]) {
w[l] = 0;
}
}
}
Arrays.sort(w);
for (int r = 0; r < w.length; r++) {
if (w[r] != 0) {
System.out.print(w[r] + ",");
}
}
System.out.print("\b");
System.out.println();
t--;
}
}
}
don't use "\b"
use StringBuilder class, and if you have answer, delete last char
Arrays.sort(w);
int flag = 0;
StringBuilder sb = new StringBuilder();
for (int r = 0; r < w.length; r++) {
if (w[r] != 0) {
flag = 1;
sb.append(w[r]).append(",");
}
}
if (flag == 1) {
sb.delete(sb.length() - 1, sb.length());
}
System.out.println(sb.toString());
t--;
thank you so much! but why we can't use "\b" while that give us same result?
M.Khooryani said:
don't use "\b"
use StringBuilder class, and if you have answer, delete last char
Arrays.sort(w); int flag = 0; StringBuilder sb = new StringBuilder(); for (int r = 0; r < w.length; r++) { if (w[r] != 0) { flag = 1; sb.append(w[r]).append(","); } } if (flag == 1) { sb.delete(sb.length() - 1, sb.length()); } System.out.println(sb.toString()); t--;
No, they aren't same
'\b' is a char
your answer and correct answer differs in length
for example:
int length1 = "abc\b".length();
int length2 = "ab".length();
System.out.println(length1 + " " + length2);
output:
4 2
Nima Mrd said:
thank you so much! but why we can't use "\b" while that give us same result?
M.Khooryani said:
don't use "\b"
use StringBuilder class, and if you have answer, delete last char
Arrays.sort(w); int flag = 0; StringBuilder sb = new StringBuilder(); for (int r = 0; r < w.length; r++) { if (w[r] != 0) { flag = 1; sb.append(w[r]).append(","); } } if (flag == 1) { sb.delete(sb.length() - 1, sb.length()); } System.out.println(sb.toString()); t--;
oh!! you're right! thx my friend ♥
M.Khooryani said:
No, they aren't same
'\b' is a char
your answer and correct answer differs in length
for example:
int length1 = "abc\b".length(); int length2 = "ab".length(); System.out.println(length1 + " " + length2);
output:
4 2
Nima Mrd said:
thank you so much! but why we can't use "\b" while that give us same result?
M.Khooryani said:
don't use "\b"
use StringBuilder class, and if you have answer, delete last char
Arrays.sort(w); int flag = 0; StringBuilder sb = new StringBuilder(); for (int r = 0; r < w.length; r++) { if (w[r] != 0) { flag = 1; sb.append(w[r]).append(","); } } if (flag == 1) { sb.delete(sb.length() - 1, sb.length()); } System.out.println(sb.toString()); t--;
In order to post something you must login first. You can use the form in the top of this page.