So, I have this task where I have to find the competitors who won in a tie.
Basically the input is a txt file, which has the number of competitors and the number of competitions in it's first row. The second and third rows are the maximum and minimum points. The rows after the third represent competitors and they have the amount of points they won.
I have to find the number of competitors who if they won, they won in a tie and their indexes.
One input:
6 8
9 9 9 9 9 9 9 9
5 5 5 5 5 5 5 5
8 4 6 6 6 6 6 6
7 5 7 6 6 6 6 5
6 6 6 5 5 5 5 6
8 6 8 7 7 7 7 6
8 6 6 6 6 6 6 6
8 6 6 6 6 6 6 1
The first row is 6 and 8 which means there is 6 competitors and 8 competition, the maximum points are 9 the minimum points are 5.
In this case the output is:
5 1 3 4 5 6
In the first column the winning point is 8, number 1 4 5 and 6 got 8 points which means they tied and should be added to the output, in the second column the winning point is 6, number 3 4 5 6 got 6 points so 3 should be added to the output too, and so on...
I've got this so far:
static void Main(string[] args)
{
string[] fRow = Console.ReadLine().Split(' '); //splitting the first row to get N and M
int N = int.Parse(esor[0]);
int M = int.Parse(esor[1]);
int[] maxPoint = new int[M];
int[] minPoint = new int[M];
string[] maxP = new string[M];
string[] minP = new string[M];
maxP= Console.ReadLine().Split(' ');
minP = Console.ReadLine().Split(' ');
for (int i = 0; i < M; i++)
{
maxPoint[i] = Convert.ToInt32(maxP[i]);
minPoint[i] = Convert.ToInt32(minP[i]);
}
int[,] matrix = new int[N, M];
int[] s = new int[M];
int mCol = 0; int mRow = 0;
for (int i = 0; i < N; i++) //adding the points to the matrix
{
string[] row = Console.ReadLine().Split(' ');
for (int h = 0; h < N; h++)
{
s[h] = Convert.ToInt32(row[h]);
matrix[mCol, mRow] = s[h];
mCol++;
if (mCol==N)
{
mCol = 0;
mRow++;
}
}
}
int max = 0; //trying to find the greatest point in each column
int[] winPoint = new int[M];
for (int i = 0; i < M; i++)
{
for (int h = 0; h < N; h++)
{
if (matrix[i, h] > max)
{
max = matrix[i, h];
}
if (h == N-1)
{
h = 0;
}
}
}
}
And I got stuck here, I'm trying to find the greatest point in each column so they I could search for the same numbers in the colums and count the amount of ties, but I don't know how to do it. If someone could help me I'd really appreciate it, or if I should to it in a different way just tell me.
Related
-> Please help with the implementation of a function
Basically I want to manipulate an image
Imagine that the image can be brake apart in pixels
-> each pixel of the image will be (for an easy example) "a number"
int[,] originalImage = new int[,]
{
{ 1,2,3 },
{ 4,5,6 }
};
And I need to double the size of the image x2 (or x3, x4, etc)
the pixel "1" when double the size will be copied on te right position, down and on the corner
int[,] expectedResult = new int[,]
{
{ 1,1,2,2,3,3 },
{ 1,1,2,2,3,3 },
{ 4,4,5,5,6,6 },
{ 4,4,5,5,6,6 }
};
How does the function can be implemented?
Multiply(int[,] originalImage, int multiply)
Note there are oodles of libraries that would do this in math or image processing. However, purely for academic purposes
Given
private static int[,] DoStuff(int[,] array,int size)
{
var sizeX = array.GetLength(0);
var sizeY = array.GetLength(1);
var newArray = new int[sizeX * size, sizeY * size];
for (var i = 0; i < newArray.GetLength(0); i++)
for (var j = 0; j < newArray.GetLength(1); j++)
newArray[i, j] = array[i / size, j / size];
return newArray;
}
Usage
int[,] originalImage =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
var newArray = DoStuff(originalImage,3);
for (var i = 0; i < newArray.GetLength(0); i++)
{
for (var j = 0; j < newArray.GetLength(1); j++)
Console.Write(newArray[i, j] + " ");
Console.WriteLine();
}
Output
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 6 years ago.
i have some list with 5 numbers (exp. 1 2 3 4 5) i want to order them in random ordering each time (page refresh) examples: (2 4 3 1 5) (1 3 5 4 2) (5 1 2 3 4)... code in C#, Thanks
var loadcards = (from card in db.GameCards
select card).Take(5).ToList();
foreach (var item in loadcards)
{
Response.Write("<script>alert('" + item.cardId + "');</script>");
}
Something like this:
int[] RandomizeOrder(int[] input)
{
Random RNG = new Random();
bool[] cellMap = new bool[input.Length];
int[] output = new int[input.Length];
for(int i = 0; i < input.Length; i++)
{
int index = RNG.Next(input.Length)
while(cellMap[index)
index = RNG.Next(input.Length);
cellMap[index] = true;
output[index] = input[i];
}
return output;
}
PS: You can remove the cellMap if none of the values is 0
I am trying to make a multiplication table appear on a page based on input from the user. This is my code:
<asp:GridView runat="server" ID="TableData"></asp:GridView>
List<List<int>> nestedList = new List<List<int>>();
protected void LoadTable(int val)
{
for (int y = 0; y <= val; y++)
{
List<int> list = new List<int>();
for (int x = 0; x <= val; x++)
list.Add(x * y);
nestedList.Add(list);
}
TableData.DataSource = nestedList;
TableData.DataBind();
}
But this displays as:
Capacity Count
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
16 14
What am I doing wrong?
For clarification, if the user enters 5, the output should be:
0 0 0 0 0 0
0 1 2 3 4 5
0 2 4 6 8 10
0 3 6 9 12 15
0 4 8 12 16 20
0 5 10 15 20 25
I am not worried about column or row headers at this time.
The problem is with your items Source.
a list< list < ?? > > is not a good choice (as i think).
For a Linear view you can use this approach
Code Snippet
var objList = new List<object>();
for (int i = 0; i < 5; i++)
{
var temp = new { operation = string.Format("{0} * {1}", i, i + 1), result = i * (i + 1) };
objList.Add(temp);
}
GridView does not support 2d list binding, consider using another methode.
For exemple, use a simple List , each string will represent a row, you can fill up each string by using a loop that goes like :
(first loop)
{
string s;
for(int x = 0; x < val; x ++)
{
s += (x * y).Tostring() + " ");
}
nestedList.Add(s);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
using System;
using System.IO;
namespace Sudoku
{
class Game
{
private int[,] puzzle = new int[9, 9];
public void saveToFile()
{
StreamWriter str = new StreamWriter("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
str.Write(puzzle[i, j] + " ");
}
str.Write("\t\n");
}
str.Close();
}
public void readFromFile()
{
clear();
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(str.Read());
}
}
str.Close();
}
}
}
I can not download the data from the file.
Saving works fine and has a view of the txt file:
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2
How it all written in my array 9x9 skipping all the gaps that would be all the data is written correctly?
Instead of using str.Read() which would require you to read single characters (or a buffer that you specified), try using str.Readline() to read a single line for each iteration of i.
public void readFromFile()
{
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
string[] lineNumbers = str.ReadLine().Split(' ');
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(lineNumbers[j]);
}
}
str.Close();
}
This reads a single line at a time (each iteration of i), splits the line into lineNumbers by separating the current line by space characters. Each number on the current line can then be accessed by lineNumbers[j] within your inner loop (each iteration of j).
I have a 9 x 9 matrix. (think of suduko).
4 2 1 6 8 1 8 5 8
3 1 5 8 1 1 7 5 8
1 1 4 0 5 6 7 0 4
6 2 5 5 4 4 8 1 2
6 8 8 2 8 1 6 3 5
8 4 2 6 4 7 4 1 1
1 3 5 3 8 8 5 2 2
2 6 6 0 8 8 8 0 6
8 7 2 3 3 1 1 7 4
now I wanna be able to get a "quadrant". for example (according to my code) the quadrant 2 , 2 returns the following:
5 4 4
2 8 1
6 4 7
If you've noticed, this is the matrix from the very center of the 9 x 9. I've split everything up in to pairs of "3" if you know what i mean. the first "ROW" is from 0 - 3, the second from 3 - 6, the third for 6 - 9.. I hope this makes sense ( I am open to alternate ways to go about this)
anyways, heres my code. I dont really like this way, even though it works. I do want speed though beccause i am making a suduko solver.
//a quadrant returns the mini 3 x 3
//row 1 has three quads,"1", "2", 3"
//row 2 has three quads "1", "2", "3" etc
public int[,] GetQuadrant(int rnum, int qnum) {
int[,] returnMatrix = new int[3, 3];
int colBegin, colEnd, rowBegin, rowEnd, row, column;
//this is so we can keep track of the new matrix
row = 0;
column = 0;
switch (qnum) {
case 1:
colBegin = 0;
colEnd = 3;
break;
case 2:
colBegin = 3;
colEnd = 6;
break;
case 3:
colBegin = 6;
colEnd = 9;
break;
default:
colBegin = 0;
colEnd = 0;
break;
}
switch (rnum) {
case 1:
rowBegin = 0;
rowEnd = 3;
break;
case 2:
rowBegin = 3;
rowEnd = 6;
break;
case 3:
rowBegin = 6;
rowEnd = 9;
break;
default:
rowBegin = 0;
rowEnd = 0;
break;
}
for (int i = rowBegin ; i < rowEnd; i++) {
for (int j = colBegin; j < colEnd; j++) {
returnMatrix[row, column] = _matrix[i, j];
column++;
}
column = 0;
row++;
}
return returnMatrix;
}
Unless I'm missing something, why not do math? Fist of all, only store rowBegin and colBegin.
Now, simply issue:
rowBegin = (rnum-1)*3
colBegin = (qnum-1)*3
This maps 1 -> 0, 2 -> 3, and 3-> 6.
Now, you loop from colBegin to colBegin + 3, and rowBegin to rowBegin + 3. Is your default behavior really necessary? If it is, special case when rnum < 1 || rnum > 3 and qnum < 1 || qnum > 3
The common pattern for this in Python is to use a dict to map:
qmap = {
1: (0, 3),
2: (3, 6),
3: (6, 9),
}
print qmap.get(qnum, (0, 0))
I'm sure that C# supports something similar.
For a general solution (i.e.: an NxN grid) I would use some maths (you'll need the modulo operator).
If you're always using a 9x9 sudoku grid then you can pre-calculate the answers and stick them in a map or array.
Of course you can combine those ideas and pre-calculate the answers in your init() function and then store them in a map.