Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 238 additions & 13 deletions Lab4/Green.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,46 @@ public void Task1(double[] array)
{

// code here

// end

}
double pos = 0,srz = 0, cnt = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > 0)
{
pos += array[i];
cnt++;
}

}
srz = pos/ cnt;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > 0)
{
array[i] = srz;
}
}

// end

}
public int Task2(int[] array)
{
int sum = 0;

// code here

int neg = 0;
for (int i = 0; i < array.Length; i ++)
{
if (array[i] < 0)
{
neg = array[i];

}break;
}
for( int i = 0; i < neg; i ++)
{
sum += array[i] * array[i];
}
// end

return sum;
Expand All @@ -25,6 +55,43 @@ public int[] Task3(int[] array)
int[] negatives = null;

// code here
int max = 0, imax = 0,imin = 0, min = int.MaxValue;
for( int i = 0; i < array.Length; i ++)
{
if (array[i] > max)
{
max = array[i];
imax = i;
}
}
for (int i = 0; i < array.Length; i++)
{
if (array[i] < min)
{
min = array[i];
imin = i;
}
}
int neg = 0;
if (imax > imin)
{
for (int i = imin; i < imax; i++)
{
if (array[i] < 0)
neg++;
}
}
else
{
for (int i = imax; i < imin; i++)
{
if (array[i] < 0)
neg++;
}
}
negatives = new int[neg];



// end

Expand All @@ -34,7 +101,28 @@ public void Task4(int[] array)
{

// code here

int max = -1000;
int idx1 = 0;
int idx2 = 0;
bool flag = false;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > max)
{
max = array[i];
idx1 = i;
}

if (array[i] < 0 && !flag)
{
idx2 = i;
flag = true;
}
}
if (flag)
{
(array[idx1], array[idx2]) = (array[idx2], array[idx1]);
}
// end

}
Expand All @@ -43,7 +131,34 @@ public int[] Task5(int[] array)
int[] answer = null;

// code here

int m = -1000;
int cnt = 0;

for (int i = 0; i < array.Length; i++)
{
if (array[i] > m)
{
m = array[i];
}
}

for (int i = 0; i < array.Length; i++)
{
if (array[i] == m)
{
cnt++;
}
}
answer = new int[cnt];
int j = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == m)
{
answer[j] = i;
j++;
}
}
// end

return answer;
Expand All @@ -52,15 +167,49 @@ public void Task6(int[] array)
{

// code here

int m = -1000;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > m)
{
m = array[i];
}
}

int j = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == m)
{
array[i] += ++j;

}
}
// end

}
public void Task7(int[] array)
{

// code here

int m = -1000;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > m)
{
m = array[i];
}
}

int s = 0;
for (int i = 0; i < array.Length; i++)
{
s += array[i];
if (array[i] == m)
{
array[i] = s - array[i];
}
}
// end

}
Expand All @@ -69,7 +218,14 @@ public int Task8(int[] array)
int length = 0;

// code here

int cur = 1, best = 1;
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] > array[i]) cur++;
else cur = 1;
if (cur > best) best = cur;
}
length = best;
// end

return length;
Expand All @@ -78,7 +234,17 @@ public void Task9(int[] array)
{

// code here

int n = (array.Length + 1) / 2;
int[] chet = new int[n];
for (int i = 0; i < n; i++)
{
chet[i] = array[2 * i];
}
Array.Sort(chet);
for (int i = 0; i < n; i++)
{
array[2 * i] = chet[i];
}
// end

}
Expand All @@ -87,7 +253,7 @@ public int[] Task10(int[] array)
int[] cleared = null;

// code here

cleared = array.Distinct().ToArray();
// end

return cleared;
Expand All @@ -97,7 +263,47 @@ public double[] Task11(double a, double b, int n)
double[] A = null, B = null;

// code here

if (n <= 1 || a == b)
{
return null;
}

A = new double[n];
double step = (b - a) / (n - 1);

for (int i = 0; i < n; i++)
{
A[i] = a + step * i;
}

double sum = 0; int cnt = 0;
for (int i = 0; i < n; i++)
{
if (A[i] > 0)
{
sum += A[i]; cnt++;
}
}
if (cnt == 0)
{
return System.Array.Empty<double>();
}

double avg = sum / cnt;
int k = 0;
for (int i = 0; i < n; i++)
if (A[i] > 0 && A[i] > avg)
{
k++;
}

B = new double[k];
int pos = 0;
for (int i = 0; i < n; i++)
if (A[i] > 0 && A[i] > avg)
{
B[pos++] = A[i];
}
// end

return B;
Expand All @@ -107,7 +313,26 @@ public int Task12(int[] dices)
int wins = 0;

// code here
int n = dices.Length;

int s = 0;
int[] mine = new int[n];
for (int i = 0; i < n; i++)
{
int a = dices[i] - s;
if (a < 1) a = 1;
mine[i] = a;

if (dices[i] == 6) s++;
}
for (int i = 0; i < n; i++)
{
int e = 6 - i;
if (e < 1) e = 1;
if (mine[i] > e) wins++;
}

return wins;
// end

return wins;
Expand Down
Loading