Answer the question
In order to leave comments, you need to log in
C# how to make an array with changing values?
In general, it is necessary that the elements in the array change places, standing in odd places, with even ones. If possible, with comments. Thank you.
Answer the question
In order to leave comments, you need to log in
Something like this:
var array = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(string.Join(" ", array));
var last = array.Last(); //берем последний элемент
array.Remove(last); //удаляем его
array.Insert(0, last); //и добавляем в начало, все элементы сдвигаются и, соответственно, меняют четность позиции
Console.WriteLine(string.Join(" ", array));
var array = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(string.Join(" ", array));
for (int i = 0; i < array.Count - 1; i += 2)
{
//Меняем соседние элементы местами
array[i] ^= array[i + 1];
array[i + 1] ^= array[i];
array[i] ^= array[i + 1];
}
Console.WriteLine(string.Join(" ", array));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question