using System;
namespace Application
{
class MainClass
{
// К-во элементов массива
const int COUNT = 20;
// Можно регулировать максимальное значение элемента, тем самым изменяя вероятность появления
// одинаковых элементов
const int MAX = 400;
// Генератор случайных чисел
static Random random = new Random ();
public static void Main (string[] args)
{
// Массив
int[] array = new int[COUNT];
Console.WriteLine ("Исходный массив:");
// Заполним случ. числами
for (int i = 0; i < COUNT; ++i) {
array[i] = random.Next () % MAX;
Console.Write ("{0,10} ", array[i]);
}
// Выводим результат
Console.WriteLine ("\nЭлементы с одинаковыми значениями {0}", onlyDifferent (array) ? "отсутствуют" : "есть");
Console.ReadKey ();
}
// Проверяет есть ли одинаковые
private static bool onlyDifferent (int[] array)
{
// Перебор элементов
for (int i = 0, end_i = array.Length - 1; i < end_i; ++i) {
for (int j = i + 1, end_j = array.Length; j < end_j; ++j) {
if (array[i] == array[j]) {
return false;
}
}
}
return true;
}
}
}
Исходный массив:
32 346 227 233 5 390 247 45 323 244 70 385 254 327 114 318 366 215 284 296
Элементы с одинаковыми значениями отсутствуют
Исходный массив:
395 73 129 212 69 292 201 127 66 277 143 346 11 129 292 76 121 327 157 97
Элементы с одинаковыми значениями есть
using System;
namespace ca181296
/*
2. Дан двумерный массив целых чисел. Нечётные строки этого массива заменить на Х.
*/
{
class Program
{
static int x;
static int[,] ar;
static Random rnd;
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("Replace odd row to x-value.");
rnd = new Random();
Console.WriteLine("Source array:");
ar = new int[5, 5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
ar[i,j] = rnd.Next (100);
Console.Write("{0,3} ", ar[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write("Set x-value: X = ");
x = Convert.ToInt16(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Result array:");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i % 2 == 1)
{
ar[i, j] = x;
}
Console.Write("{0,3} ", ar[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write("Done. Press any key...");
Console.ReadKey();
}
}
}
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int arraysize=0;
int arrayindex;
bool IsNumeric;
while (arraysize == 0)
{
Console.Write("Введите размер массива: ");
IsNumeric = Int32.TryParse(Console.ReadLine(), out arraysize);
if (arraysize == 0)
Console.Write("Введено неверное или нулевое значение, попробуйте еще раз! \r\n");
}
string[] myArray = new string[arraysize];
for (arrayindex = 0; arrayindex != myArray.Length; arrayindex++)
{
Console.Write("Введите элемент № {0} массива: ", arrayindex+1);
myArray[arrayindex] = Console.ReadLine();
}
var uniqueElements = myArray.Distinct();
if (uniqueElements.Count() != arraysize)
{
foreach (var ue in uniqueElements)
{
int amount = (from a in myArray
where a == ue
select a).Count<string>();
if (amount > 1)
Console.WriteLine("\nЗначение {0} повторяется {1} раз(а)", ue, amount);
}
}
else
{
Console.WriteLine("\nМассив не содержит совпадений!");
}
Console.ReadKey();
}
}
}
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.