Last active
September 8, 2018 13:52
-
-
Save nickaroot/005c3555f5a6fc9285c288bd08ada021 to your computer and use it in GitHub Desktop.
NDRW JAVA FINAL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class First { | |
// Дан одномерный массив Хк. | |
// Найти последний среди элементов с четными значениями. | |
// Все отрицательные элементы массива удалить. | |
// В конец массива дописать среднее арифметическое положительных элементов. | |
/* | |
* Тестовые входные данные: | |
* | |
* 17 | |
* 35 | |
* -12 | |
* -24 | |
* 352 | |
* -5 | |
* -5 | |
* 61 | |
* -17 | |
* 52 | |
* -16 | |
* -34 | |
* -43 | |
* -36 | |
* 834 | |
* 74 | |
* 234 | |
* 215 | |
* | |
*/ | |
public First() { | |
int yn = 0; | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Введите размер массива: "); | |
yn = scan.nextInt(); | |
int[] y = new int[yn]; | |
System.out.println("Введите массив: "); | |
for(int i = 0; i < yn; i++) | |
{ | |
System.out.print( "x[" + i +"] = " ); | |
y[i] = scan.nextInt(); | |
} | |
System.out.println(); | |
System.out.print( "{ " ); | |
for ( int x : y ) | |
System.out.print( ( x ) + " " ); | |
System.out.print( "}" ); | |
System.out.println(); | |
System.out.println(); | |
int last = y[ 0 ], sum = 0, counter = 0, offset = 0; | |
boolean wasRemoved = false; | |
for (int i = 0; i < y.length; i++) { | |
if ( wasRemoved ) { | |
System.out.println( "Удален элемент x["+ (i - 1) + "] = " + y[ i - 1 ] ); | |
System.out.println(); | |
wasRemoved = false; | |
} | |
last = ( y[ i ] % 2 == 0 ) ? y[ i ] : last; | |
if ( y[ i ] > 0 ) { | |
sum += y[ i ]; | |
counter += 1; | |
} | |
y[ i - offset ] = y[ i ]; | |
offset += ( y[ i ] < 0 ) ? 1 : 0; | |
wasRemoved = ( y[ i ] < 0 ); | |
} | |
int[] z = new int[ y.length - offset + 1 ]; | |
System.arraycopy( y, 0, z, 0, z.length); | |
z[ z.length - 1 ] = sum / counter; | |
System.out.println( ( last % 2 == 0 ) ? "Последний среди элементов с четными значениями: " + ( last ) : "Элементов с четными значениями не найдено" ); | |
System.out.print("{ "); | |
for (int x: z) | |
System.out.print(x + " "); | |
System.out.print("}"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Second() { | |
Scanner scan = new Scanner(System.in); | |
int xn, xm = 0; | |
boolean find = false; | |
System.out.print("Введите количество строк: "); | |
xn = scan.nextInt(); | |
System.out.print("Введите количество столбцов: "); | |
xm = scan.nextInt(); | |
int[][] x = new int[xn][xm]; | |
for(int i = 0; i < xn; i++) | |
{ | |
// System.out.println("Введите " + ( xm ) + " элементов массива строки " + ( i ) + ": "); | |
for(int j = 0; j < xm; j++) | |
{ | |
System.out.print("Введите x[" + i +"][" + j + "] = "); | |
x[i][j] = scan.nextInt(); | |
} | |
} | |
System.out.println(); | |
for ( int[] n : x ) { | |
for ( int m : n ) | |
System.out.print( ( String.format( "%5d" , m ) ) + "\t" ); | |
System.out.println(); | |
} | |
System.out.println(); | |
int[] y = new int[ x.length / 2 ]; | |
int si = 0; | |
for (int j = 0; j < x.length; j += 2) { | |
int s = 0; | |
for ( double m : x[ j + 1 ] ) { | |
s += ( m % 2 == 0 ) ? m : 0; | |
find = ( m % 2 == 0 ) || find; | |
} | |
if ( s != 0 ) { | |
y[ si ] = s; | |
si += 1; | |
} | |
} | |
if ( find ) { | |
System.out.print("{ "); | |
for (int z = 0; z < si; z++) { | |
System.out.print(y[z] + " "); | |
} | |
System.out.print("}"); | |
} else { | |
System.out.print("Четных элементов в нечетных строках не найдено"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Third { | |
public Third() { | |
// Все элементы матрицы X (n x n), | |
// лежащие выше второстепенной диагонали, | |
// заменить количеством элементов с дробной частью, | |
// лежащих на главной диагонали. | |
/* | |
* Тестовые входные данные: | |
* | |
* 4 | |
* 1.34 | |
* -1 | |
* -42 | |
* 32 | |
* 23 | |
* -29 | |
* 65 | |
* -5.8 | |
* 12 | |
* -58 | |
* -5 | |
* -22 | |
* 23 | |
* 52 | |
* 8 | |
* 9.342 | |
* | |
*/ | |
int xn = 0; | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Введите размер матрицы: "); | |
xn = scan.nextInt(); | |
double[][] x = new double[xn][xn]; | |
for(int i = 0; i < xn; i++) | |
{ | |
//System.out.println("Введите " + ( xn ) + " элементов массива строки " + ( i ) + ": "); | |
for(int j = 0; j < xn; j++) { | |
System.out.print( "x[" + i + "][" + j + "] = " ); | |
x[i][j] = scan.nextDouble(); | |
} | |
} | |
System.out.println(); | |
for ( double[] n : x ) { | |
for (double m : n) | |
System.out.print((String.format("%8.2f", m)) + "\t"); | |
System.out.println(); | |
} | |
System.out.println(); | |
int c = 0; // количество элементов с дробной частью, лежащих на главной диагонали | |
for (int i = 0; i < x.length; i++) { | |
c += ( x[ i ][ i ] != Math.ceil( x[ i ][ i ] ) ) ? 1 : 0; | |
} | |
for (int n = 0; n < x.length; n++) { | |
for (int m = 0; m < (x.length - (n + 1)); m++) { | |
x[n][m] = c; | |
} | |
} | |
for ( double[] n : x ) { | |
for (double m : n) | |
System.out.print((String.format("%8.2f", m)) + "\t"); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment