創建一個程式來查找矩陣D(m, n)中的所有元素,其中在考慮中的一個之前的行的所有元素的總和大于在考慮的一個之前的列的元素的總和. 如果元素是行或列中的第一個元素,則認為前面元素的總和為零。從找到的元素形成一個陣列。將矩陣輸出為矩陣,在其下方輸出陣列的元素。(Windows Forms應用程式)
結果證明創建了第一個矩陣。我無法根據給定的規則創建陣列。而且我不知道如何輸出陣列元素(不是控制臺!它的 Windows 表單應用程式)
我已經為這項任務痛苦了一個星期。
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int M = 0;
int N = 0;
int[,] Numbers;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) // create matrix
{
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
dataGridView1.AllowUserToAddRows = true;
M = int.Parse(M_input.Text);
N = int.Parse(N_input.Text);
Numbers = new int[0, 0];
Numbers = new int[N, M];
for (int i = 0; i < M; i )
{
dataGridView1.Columns.Add("", "");
}
for (int i = 0; i < N; i )
{
dataGridView1.Rows.Add("", "");
}
dataGridView1.AllowUserToAddRows = false;
}
//this button should create an array and output array elements (in the form of a string, most likely)
private void button2_Click(object sender, EventArgs e)
{
int n = dataGridView1.RowCount;
int m = dataGridView1.ColumnCount;
double[,] array1 = new double[n, m];
for (int i = 0; i < n; i )
for (int j = 0; j < m; j )
{
array1[i, j] = double.Parse(dataGridView1.Rows[i].Cells[j].Value.ToString());
}
int times = 0;
dataGridView2.RowCount = 1;
dataGridView2.ColumnCount = 0;
for (int i = 0; i <= n; i )
for (int j = 0; j <= m; j )
{
double sum1 = 0;
double sum2 = 0;
if (i == 0)
sum1 = 0;
else
for (int k = 0; k < i; k )
sum1 = array1[i, k];
if (j == 0)
sum2 = 0;
else
for (int k = 0; k < j; k )
sum2 = array1[k, j];
if (sum1 > sum2)
{
dataGridView2.ColumnCount ;
dataGridView2.Rows[0].Cells[times].Value = array1[i, j];
times ;
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void LUM1_Click(object sender, EventArgs e)
{
}
private void LUM2_Click(object sender, EventArgs e)
{
}
private void LUM3_Click(object sender, EventArgs e)
{
}
private void Matrix_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void M_input_TextChanged(object sender, EventArgs e)
{
}
private void N_input_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Matrix2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
uj5u.com熱心網友回復:
想象一下,你打開了 excel,在一個 10x10 的網格中有從 1 到 100 的數字。A1 是 1,B1 是 2,A2 是 11 等等
您的作業要求您選擇一個單元格,例如 B2。第 1 行中的值的總和是否大于 A 列中的值的總和?是-> 將 B2 的值放在一個串列中。對另一個單元格重復。做所有的細胞
我從 B2 開始,因為它上面有一行,左邊有一列,所以它不需要處理“沒有行或列意味著總和為 0”的特殊情況,你可以在任何單元格上得到第 1 行或 A 列
- 讓你的生活變得輕松。寫兩個方法:
public int SumColumn(int whichCol)
//if whichCol is -1 return 0
//declare variable to hold the sum
//loop over the grid from [row 0, col whichCol] to [row N, col whichCol], adding up into sum
//return sum
public int SumRow(int which)
//copy paste and adapt code above to work across rows not down columns
- 現在檢查你的網格
//declare a list to hold the cells we find with SumRow>SumColumn
//for r in rows
//for c in columns
//add current cell value to a textbox ("output the matrix")
//if SumRow(r-1) > SumColumn(c-1)
//add to list
//add the contents of the list to the TextBox too with another loop
撰寫此代碼應該需要大約 15 分鐘,而不是幾周。當你不熟悉任何東西時的代碼設計程序就像我在這里所做的那樣。用你認為的語言寫評論;在您開始撰寫代碼并迷路之前,請在腦海中直接記下演算法并寫下來。評論是你的方向,你的論文計劃,食譜書,而廚房里的一切都在瘋狂。您絕對必須像說外語一樣計劃您的課程;首先你想象你想用你的母語說的句子,然后也許你將它重新排列成外國人的說法(詞序),然后你翻譯單詞并進行共軛、修改等,最后你說出來。從事English->C#的翻譯程序是一樣的;你懂一種語言但不懂另一種
完成后,留下評論,這樣如果你出錯了,你可以看到它并修復它,或者你的主管可以看到你的想法和你對 c# 理解的分歧,給你演算法的分數并知道教你什么幫助解決哪里出錯了
uj5u.com熱心網友回復:
我已經解決了矩陣影像的問題
namespace Matrix
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int m; // columns
int n; // rows
int[,] MatrixArray; // array of matrix elements
private void Create_Click(object sender, EventArgs e)// By clicking, a matrix grid is created
{
GridMatrix.AllowUserToAddRows = true;
m = int.Parse(Size_M.Text); // Enter the number of columns
n = int.Parse(Size_N.Text); // Enter the number of rows
_Answer.Text = "Elements (answer):";
GridMatrix.Columns.Clear();
GridMatrix.Rows.Clear();
MatrixArray = new int[n, m]; // Creating an empty array of dimension m x n
for (int i = 0; i < m; i )// Creating columns
{
GridMatrix.Columns.Add("", "");
}
for (int i = 0; i < n; i )// Creating rows
{
GridMatrix.Rows.Add("", "");
}
GridMatrix.AllowUserToAddRows = false;
}
private void Answer_Click(object sender, EventArgs e)// When pressed, the answer appears
{
for(int i = 0; i < n; i )// Transferring a matrix to an array
{
for(int j = 0; j < m; j )
{
MatrixArray[i, j] = int.Parse(GridMatrix[j, i].Value.ToString());
}
}
int[] AnswerArray=new int[m*n];
int Counter=0;
for (int i = 0; i < n; i ) // The algorithm for finding elements
{
for (int j = 0; j < m; j )
{
int RowSumm = 0,ColumnSumm=0; // Sums of row and column elements
for (int b=0;b<j;b )// Sum of rows
{
RowSumm = MatrixArray[i, b];
}
for(int b = 0; b < i; b )// Sum of columns
{
ColumnSumm = MatrixArray[b, j];
}
if (RowSumm>ColumnSumm)
{
AnswerArray[Counter] = MatrixArray[i, j];
Counter ;
}
}
}
_Answer.Text = "Elements (answer):";
for (int i=0;i<Counter;i )// Output of array elements in turn
{
_Answer.Text = "" AnswerArray[i];
if (i!=Counter-1)
{
_Answer.Text = ", ";
}
}
}
private void M_Label_Click(object sender, EventArgs e)
{
}
private void N_Label_Click(object sender, EventArgs e)
{
}
private void Answ1_Click(object sender, EventArgs e)
{
}
private void GridMatrix_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void SizeLabel_Click(object sender, EventArgs e)
{
}
private void Size_M_TextChanged(object sender, EventArgs e)
{
}
private void Size_N_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393538.html
上一篇:數獨求解器在無效的地方重復數字1
