Answer the question
In order to leave comments, you need to log in
What is the correct way to work with BufferedInputStream(System.in)?
There is a task: https://contest.yandex.ru/contest/8458/problems/C
To complete it, I wrote the following code:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Mass {
private static int count_mass = 11; // Длина считываемого числа
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
char[] el;
char[] o_el = new char[] {'-','9','9','9','8',10};; // предыдущее считанное значение
for (int i = 1; i <= count; i++){
el = getBuf();
if(!eq_mass(el, o_el)){
print_buf(el);
}
o_el = el;
}
br.close();
}
// Блок функций ------------------------------
static void print_buf(char[] b){
int l = 0;
do {
System.out.print(b[l]);
l++;
}while(b[l] != 10 && b[l] != 0);
if (b[l] != 0) {
System.out.print(b[l]);
}
}
static char[] getBuf() throws IOException { // Считать массив из консоли
char[] buf;
buf = new char[count_mass];
BufferedInputStream bf = new BufferedInputStream(System.in);
try{
int i, l = 0;
char c = 1;
while(c != 0 && c != 10 && ((i = bf.read()) != -1) ) {
c = (char) i;
buf[l] = c;
l++;
}
}
finally{
//bf.close();
return buf;
}
}
static boolean eq_mass(char[] m1, char[] m2){ // Сравнить массив
boolean rez = true; // по умолчанию массивы равны
for (int i = 0; i < count_mass; i++) {
if(m1[i] != m2[i]){
rez = false;
}
if (m1[i] == 10 || m2[i] == 10){
break;
}
}
return rez;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question