This is my first question here, so sorry for any mistake.
How to do second loop to generate unique rows?
Every time when i add second loop to find unique solution program hangup...
static void Main(string[] args)
{
int[,] tab = new int[9, 9];
Random r = new Random();
List<int> temp = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
while (true)
{
bool uniq = true;
int randomNumber = r.Next(0, 9);
for (int z = 0; z < i; z++)
{
if (randomNumber==tab[z,j])
{
uniq = false;
}
}
if (uniq == false)
{
continue;
}
tab[i, j] = randomNumber;
temp.Remove(randomNumber);
break;
}
Try this:
private Random r = new Random();
void Main()
{
int[,] tab = new int[9, 9];
List<int> digits = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < 9; i++)
{
List<int> temp = digits.OrderBy(x => r.Next()).ToList();
for (int j = 0; j < 9; j++)
{
tab[i, j] = temp[j];
}
}
}
Related
The task is I need to check if the array in array (2d array) is palindromic
The problem is I get an "Out of Range" exception at LINE 40 and can't figure out how to tell computer to do proper calculating there: if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])
I need to interpret if (arr[i] != arr[n - i - 1]) for 2d array.
This is my code so far: ```
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int n;
n = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[n, n];
for (int i = 0; i < arr.GetLength(0); i++)
{
for(int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = rnd.Next(1, 15);
}
}
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.WriteLine(arr[i, j]);
}
}
int flag = 0;
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j <= arr.GetLength(1) / 2 && n != 0; j++)
{
if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])
{
Console.WriteLine(i);
flag = 1;
break;
}
}
}
if (flag == 1)
{
Console.WriteLine("pali");
}
else
{
Console.WriteLine("not pali");
}
}
}
Are looking for something like this ?
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[,]
{
{ 1, 2, 3, 4, 5, 6 },
{ 1, 2, 3, 4, 5, 6 },
{ 1, 2, 3, 3, 2, 1 }, // Palindrome
{ 1, 2, 3, 4, 5, 6 },
{ 1, 2, 3, 4, 5, 6 }
};
PrintArray(arr);
CheckForPalindrome(arr);
}
static void PrintArray(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write($"{arr[i, j]} ");
}
Console.WriteLine();
}
}
static void CheckForPalindrome(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
var equals = true;
for (int j = 0; j < arr.GetLength(1) / 2; j++)
{
equals = arr[i, j] == arr[i, arr.GetLength(1) - j - 1];
if (equals == false)
break;
}
if (equals == true)
Console.WriteLine($"Row {i + 1} is palindrome");
}
}
}
}
The following picture is retrieved from Wikipedia:
I haven't understood this part:
I have two questions here:
Where did they obtain [8,4,1,2] from and what did they want to tell us by that?
Take a look at cell [0, 0] whose value is 13. If I go clockwise along with its contouring values, I obtain the binary string 0010 which is 2. How does the 1st cell-value become 13?
.
enum What
{
lines, surface, both
}
class Program
{
public static void Print(int[,] data, int xn, int yn)
{
for (int j = 0; j < yn; j++)
{
for (int i = 0; i < xn; i++)
{
Console.Write(data[i,j] + ", ");
}
Console.WriteLine();
}
}
public static int[,] normalize(int[,] data, int xn, int yn)
{
for (int j = 0; j < yn; j++)
{
for (int i = 0; i < xn; i++)
{
if (data[i, j] > 1)
{
data[i, j] = 0;
}
else
{
data[i, j] = 1;
}
}
}
return data;
}
public static int[,] marching_square(int x, int y, int[,] data, int isovalue, What what)
{
int xn = x;
int yn = y;
data = normalize(data, xn, yn);
int[,] bitMask = new int[xn - 1, yn - 1];
for (int j = 0; j < yn - 1; j++)
{
for (int i = 0; i < xn - 1; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append(data[i, j]);
sb.Append(data[i, j + 1]);
sb.Append(data[i + 1, j]);
sb.Append(data[i + 1, j + 1]);
bitMask[i, j] = Convert.ToInt32(sb.ToString(), 2);
}
}
return bitMask;
}
static void Main(string[] args)
{
int[,] data = new int[,] {
{ 1,1,1,1,1 },
{ 1,2,3,2,1 },
{ 1,3,3,3,1 },
{ 1,2,3,2,1 },
{ 1,1,1,1,1 }
};
Print(data, 5,5);
int[,] bitMask = marching_square(5,5,data, 0, What.lines);
Print(bitMask, 4, 4);
}
}
Output:
1, 1, 1, 1, 1,
1, 2, 3, 2, 1,
1, 3, 3, 3, 1,
1, 2, 3, 2, 1,
1, 1, 1, 1, 1,
14, 10, 10, 11,
12, 0, 0, 3,
12, 0, 0, 3,
13, 5, 5, 7,
I inverted the bits. But, the output looks different.
I need to write out my array using a for-loop. This is my code. The reason I have to do it with a for-loop is because it is for a school project and the teacher does not accept anything else. What the code is doing right now is that first it creates an array with random numbers and make sure there are no duplicates then it makes sure it is sorted with bigger numbers first.
int[] myArray = new int[20];
Random random = new Random();
bool isUnique;
for (int i = 0; i < myArray.Length; i++) {
isUnique = false;
while (!isUnique) {
isUnique = true;
myArray[i] = random.Next(1, 100);
for (int j = 0; j < i; j++) {
if (myArray[i] == myArray[j]) {
isUnique = false;
}
}
}
}
bool isSorted;
int change;
for (int i = 0; i < myArray.Length; i++) {
isSorted = false;
while (!isSorted) {
isSorted = true;
for (int j = i + 1; j < myArray.Length; j++) {
if (myArray[i] < myArray[j]) {
change = myArray[j];
myArray[j] = myArray[i];
myArray[i] = change;
isSorted = false;
}
}
}
}
Works for me.
for (var i = 0; i < myArray.Length; i++)
{
Console.WriteLine(myArray[i]);
}
Prints your array, it's sorted fine and prints biggest to smallest.
What isn't working?
I rewrote your code to less verbose variant:
int[] myArray = new int[20];
Random random = new Random();
int randomValue = 0;
// Fill array
for (int i = 0; i < myArray.Length; i++)
{
do randomValue = random.Next(1, 100);
while (myArray.Contains(randomValue));
myArray[i] = randomValue;
}
// Sort array
myArray = myArray.OrderByDescending(x => x).ToArray();
For write array to console you should use cycle:
for (var i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i] + " ");
But for best and less verbose result you should use this:
Console.WriteLine(string.Join(", ", myArray));
It's return this:
97, 95, 93, 92, 91, 85, 84, 78, 77, 76, 75, 68, 57, 45, 24, 22, 14, 10, 8, 4
I have been given this task to solve:
Write a program that reads an array of integers and removes from it a minimal number of elements in such a way that the remaining array is sorted in increasing order. Print the minimal number of elements that need to be removed in order for the array to become sorted.
Time limit 0.1sec.
Sample test:
Input:
1,
4,
3,
3,
6,
3,
2,
3
Output:
3
Unfortunately, my program is slower. This is my code:
using System;
static bool CheckAscending(List<int> list)
{
bool ascending = true;
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
ascending = false;
}
}
return ascending;
}
static void Main()
{
int n;
n = int.Parse(Console.ReadLine());
List<int> arr = new List<int>();
List<int> sorted = new List<int>();
int maxSubsetLenght = 0;
for (int i = 0; i < n; i++)
{
arr.Add(int.Parse(Console.ReadLine()));
}
for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
{
int tempSubsetLenght = 0;
List<int> temp = new List<int>();
for (int j = 1; j <= n; j++)
{
if (((i >> (j - 1)) & 1) == 1)
{
temp.Add(arr[j - 1]);
tempSubsetLenght++;
}
}
if ((tempSubsetLenght > maxSubsetLenght) && (CheckAscending(temp)))
{
sorted = temp;
maxSubsetLenght = tempSubsetLenght;
}
}
Console.WriteLine(n - sorted.Count);
}
Can someone help me to make my program a bit faster. I will be glad if you could answer in the near future.
Ok I found how to soleve it and thanks #Gabor for your help :). Here is my solution:
using System;
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
List<int> numbers = new List<int>();
for (int i = 0; i < n; i++)
{
int currentNumber = int.Parse(Console.ReadLine());
numbers.Add(currentNumber);
}
int[] size = new int[numbers.Count];
// Define each number as subsequence.
for (int i = 0; i < numbers.Count; i++)
{
size[i] = 1;
}
int max = 1;
// Compare current number with the numbers before.
for (int i = 1; i < numbers.Count; i++)
{
for (int j = 0; j < i; j++)
{
if (numbers[i] >= numbers[j] && size[i] <= size[j] + 1)
{
size[i] = size[j] + 1;
// Update max increasing subsequence.
if (max < size[i])
{
max = size[i];
}
}
}
}
// Print numbers to remove as a result.
int numbersToRemove = n - max;
Console.WriteLine(numbersToRemove);
}
I think it would be helpful for other people, who have the same task to do like me.
UPDATE #2
When there is a decrease then we should delete not only the current number but all the numbers before which are more than the new minValue.
The Stopwatch is in the System.Diagnostic namespace.
// int?[] numbers = new int?[] { 8, 1, 4, 3, 3, 6, 3, 2, 3 };
int?[] numbers = new int?[] { -7, -7, -7, -100, -100, -99, 4, -90, -80, -70 };
Console.WriteLine("Array: [{0}]", String.Join(", ", numbers));
System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Start();
int minValue = Int32.MinValue;
for (int i = 0; i < numbers.Length - 1; i++)
{
var currentNumber = numbers[i];
var nextNumber = numbers[i + 1];
if (currentNumber > nextNumber)
{
if (nextNumber < minValue)
{
numbers[i + 1] = null;
}
else
{
minValue = nextNumber.Value;
for (int j = i; j >= 0; j--)
{
if (numbers[j] > minValue)
{
numbers[j] = null;
}
}
}
}
}
watch.Stop();
Console.WriteLine("Result: {0}; Time: {1} ms", numbers.Count(number => number == null), watch.ElapsedMilliseconds);
Console.WriteLine("Array: [{0}]", String.Join(", ", numbers));
I am trying to solve my task using a List and I know I am very close to solving it but I am stuck now. Something is not ok in the code and I can not figure out what it is. Could you please take a look and help:
/*
Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the
remaining array is sorted in increasing order. Print the remaining sorted array.
Example: {6, 1, 4, 3, 0, 3, 6, 4, 5} {1, 3, 3, 4, 5}
*/
using System;
using System.Collections.Generic;
class RemoveMinimalElements
{
static void Main()
{
int n;
n = int.Parse(Console.ReadLine());
List<int> arr = new List<int>();
List<int> sorted = new List<int>();
int maxSubsetLenght = 0;
for (int i = 0; i < n; i++)
{
arr.Add(int.Parse(Console.ReadLine()));
}
for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
{
int tempSubsetLenght = 0;
string tempString = "";
List<int> temp = new List<int>();
for (int j = 1; j <= n; j++)
{
int andMask = i & (1 << j);
int bit = andMask >> j;
if (bit == 1)
{
temp.Add(arr[n - 1 - j]);
tempSubsetLenght++;
}
if (tempSubsetLenght > maxSubsetLenght)
{
maxSubsetLenght = tempSubsetLenght;
for(int k =1; k < temp.Count; k ++)
{
if (temp[k] >= temp[k - 1])
{
sorted = temp;
}
}
}
}
}
for (int i = sorted.Count - 1; i > 0; i--)
{
Console.WriteLine(sorted[i]);
}
}
}
I didn't follow the code, I just tested your app.
This is my first input: 5.
Then I entered these 5 inputs 2,4,6,8,10 so
arr = {2,4,6,8,10};
And when it came to the last lines it gave me the ArguementOutOfRangeException (Index was out of range. Must be non-negative and less than the size of the collection.) because it was trying to fetch arr[item] and item is 6 so it's trying to fetch arr[6] which does not exist.
I don't know if an exhaustive search is suitable for your case, but will this work for you?
static void Main(string[] args)
{
int[] input = new[] { 6, 1, 4, 3, 0, 3, 6, 4, 5 };
int[] expectedOutput = new[] { 1, 3, 3, 4, 5 };
int[] solution = TryGetSolution(input);
Console.WriteLine("Input: " + FormatNumbers(input));
Console.WriteLine("Expected Output: " + FormatNumbers(expectedOutput));
Console.WriteLine("Output: " + FormatNumbers(solution));
Console.ReadLine();
}
private static string FormatNumbers(int[] numbers)
{
return string.Join(", ", numbers);
}
private static int[] TryGetSolution(int[] input)
{
return TryWithoutAnyItem(input);
}
private static int[] TryWithoutAnyItem(int[] items)
{
return Enumerable.Range(0, items.Length)
.Select(i => TryWithoutItem(items, i))
.Where(solution => solution != null)
.OrderByDescending(solution => solution.Length)
.FirstOrDefault();
}
private static int[] TryWithoutItem(int[] items, int withoutIndex)
{
if (IsSorted(items)) return items;
var removed = items.Take(withoutIndex).Concat(items.Skip(withoutIndex + 1));
return TryWithoutAnyItem(removed.ToArray());
}
private static bool IsSorted(IEnumerable<int> items)
{
return items.Zip(items.Skip(1), (a, b) => a.CompareTo(b)).All(c => c <= 0);
}
}
I solved it! Thank you very much for your support. I am a beginer and I am not able to use and understand more difficult stuff yet so here is what I did whit the things I already know:
/*
Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the
remaining array is sorted in increasing order. Print the remaining sorted array.
Example: {6, 1, 4, 3, 0, 3, 6, 4, 5} {1, 3, 3, 4, 5}
*/
using System;
using System.Collections.Generic;
class RemoveMinimalElements
{
static bool CheckAscending(List<int> list)
{
bool ascending = true;
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
ascending = false;
}
}
return ascending;
}
static void Main()
{
int n;
n = int.Parse(Console.ReadLine());
List<int> arr = new List<int>();
List<int> sorted = new List<int>();
int maxSubsetLenght = 0;
for (int i = 0; i < n; i++)
{
arr.Add(int.Parse(Console.ReadLine()));
}
for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
{
int tempSubsetLenght = 0;
List<int> temp = new List<int>();
for (int j = 1; j <= n; j++)
{
if (((i >> (j - 1)) & 1) == 1)
{
temp.Add(arr[j - 1]);
tempSubsetLenght++;
}
}
if ((tempSubsetLenght > maxSubsetLenght) && (CheckAscending(temp)))
{
sorted = temp;
maxSubsetLenght = tempSubsetLenght;
}
}
for (int i = 0; i < sorted.Count; i++)
{
Console.WriteLine(sorted[i]);
}
}
}
This works for me
private static void FindLongestRisingSequence(int[] inputArray)
{
int[] array = inputArray;
List<int> list = new List<int>();
List<int> longestList = new List<int>();
int highestCount = 1;
for (int i = 0; i < array.Length; i++)
{
list.Add(array[i]);
for (int j = i+1; j < array.Length; j++)
{
if (array[i] < array[j])
{
list.Add(array[j]);
i++;
}
else
{
break;
}
i = j;
}
// Compare with in previous lists
if (highestCount < list.Count)
{
highestCount = list.Count;
longestList = new List<int>(list);
}
list.Clear();
}
Console.WriteLine();
// Print list
Console.WriteLine("The longest subsequence");
foreach (int iterator in longestList)
{
Console.Write(iterator + " ");
}
Console.WriteLine();
}