Contest.samsu.ru :: соревнования по программированию
Русская версия || English version
Login:
Password:
Забыли пароль?
 пример поиска: Вася Пупкин
 

lab4

Автор задачи: nnn

Задачу добавил: nnn

Успешно сдано решений: 28



import java.util.Random;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import static java.lang.Math.abs;/*

/**
*
* @author нася
*/
public class SampleTask {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
Task39 t = new Task39();
t.solution();
t.solution2();
t.createRandomInputFile();
}

}

class Task39 {
int[] elements;
int Max;
int iLeftMax;
int iRightMax;
int Min;
int iLeftMin;
int iRightMin;

static final int maxLength = 10000;

Task39 () throws FileNotFoundException {
Scanner fin = new Scanner (new FileReader("input.txt"));
elements = new int [maxLength];
elements[0] = fin.nextInt();
Max = elements [0];
iLeftMax = 0;
iRightMax = 0;
Min = elements[0];
iLeftMin = 0;
iRightMin = 0;

int i = 1;
int el;
while ((el = fin.nextInt()) !=0){
elements[i] = el;
if (el < Min){
Min = el;
iLeftMin = i;
iRightMin = i;
} else if (el == Min){
iRightMin = i;
} else if (el > Max) {
Max =el;
iLeftMax =i;
iRightMax = i;
} else if (el == Max){
iRightMax = i;
}
i++;
}//while
}// construktor Task39

void solution() throws FileNotFoundException{
int outputDirection;
int startIndex;
int stopIndex;
if ((iLeftMin == iRightMin) && (iLeftMax == iRightMax)) {
if(iLeftMin < iLeftMax){
startIndex = iLeftMin + 1;
stopIndex = iRightMax - 1;
outputDirection = 1;
}
else {
startIndex = iLeftMin - 1;
stopIndex = iRightMax + 1;
outputDirection = -1;
}//if(iLeftMin < iLeftMax)

} else if (abs(iRightMax - iLeftMin) >= abs(iLeftMax - iRightMin)){
startIndex = iLeftMin + 1;
stopIndex = iRightMax - 1;
outputDirection = 1;
}
else {
startIndex = iRightMin - 1;
stopIndex = iLeftMax + 1;
outputDirection = -1;
}

int len;
PrintWriter fout = new PrintWriter("output.txt");
if (((stopIndex - startIndex)* outputDirection) < 0){
len = 0;
fout.println(len);
} else {
len = abs(stopIndex - startIndex) + 1;
fout.println(len);
for (int i = startIndex; i!=stopIndex; i = i + outputDirection){
fout.print(elements[i] + " ");
}//for i
fout.print(elements[stopIndex]);
}//else знаки неодинаковые

fout.flush();
fout.close();

}//solution

void createRandomInputFile() throws FileNotFoundException {
Random random = new Random();
PrintWriter pw = new PrintWriter ("input2.txt");
Scanner con = new Scanner (System.in);

System.out.print("Введите наименьший возможный элемент массива (>0): ");
int smallest = con.nextInt();
System.out.print("\n Введите наибольший возможный элемент массива (> "+ smallest+"): ");
int largest = con.nextInt();

int r;
for (int i = 0; i < maxLength; i++) {
r = smallest + random.nextInt(largest - smallest + 1);
pw.print(r+ " ");
} // for
pw.print('0');

pw.flush();
pw.close();
} //createRandomInputFile

void solution2() throws FileNotFoundException{
int outputDirection;
int startIndex;
int stopIndex;
int len;
if ((iRightMax - iLeftMin) >= ( iRightMin - iLeftMax)){
len = iRightMax - iLeftMin-1;
startIndex = iLeftMin + 1;
stopIndex = iRightMax - 1;
outputDirection = 1;
}
else {
len = iRightMin - iLeftMax -1;
startIndex = iRightMin - 1;
stopIndex = iLeftMax + 1;
outputDirection = -1;
}
PrintWriter pw = new PrintWriter("output.txt");
pw.println(len);

if ( len !=0) {
for (int i =startIndex; i !=stopIndex; i = i+ outputDirection){
pw.print(elements[i] + " ");
}
pw.print(elements[stopIndex]);
}
pw.flush();
pw.close();
}//solution2

} // class Task39






Сдать задачу

Задать вопрос жюри по этой задаче