我有撰寫同心方陣的編碼問題(最大的數字在中間)例如用戶需要撰寫一個矩陣例如:
5 5 5 5 5
5 6 6 6 5
5 6 7 6 5
5 6 6 6 5
5 5 5 5 5
我的程式必須輸出“是”,因為根據我的程式規則,這是一個同心方陣。
5 5 5 5 5
5 6 6 6 5
5 6 7 8 5
5 6 6 6 5
5 5 5 5 5
這不是同心方陣,因為 8 在第 4 列和第 3 行。
這是我的代碼:
#include <stdio.h>
int main() {
int mat[100][100];
int i,j;
int n;
scanf("%d",&n);
printf("Unesite matricu; ");
for(i=0;i<n;i )
{
for(j=0;j<n;j )
{
scanf("%d",&mat[i][j]);
}
}
}
我不知道如何做剩下的事情,所以如果有人可以幫助我,我會很高興:))
uj5u.com熱心網友回復:
請您嘗試以下方法:
#include <stdio.h>
int main() {
int mat[100][100];
int ii[] = {0, 1, 0, -1}; // incremental numbers of i
int jj[] = {1, 0, -1, 0}; // incremental numbers of j
int i, j;
int n;
int u, v, w; // variables to walk on edges
int val; // value of the element
int prev = -1; // previous value in one outer edge (assuming the matrix values are positive)
int length; // length of the side of the edge
// read matrix size and values
printf("Enter the number:\n");
scanf("%d", &n);
printf("Enter the matrix:\n");
for (i = 0; i < n; i ) {
for (j = 0; j < n; j ) {
scanf("%d", &mat[i][j]);
}
}
// loop on the edges
for (u = 0; u < n / 2; u ) { // from the outmost edge to inner
i = u; j = u; // index of the north west corner
val = mat[u][u]; // initial value to compare
for (v = 0; v < 4; v ) { // four sides
length = n - u * 2 - 1; // length of the side of the edge
for (w = 0; w < length; w ) {
i = ii[v]; // one step ahead on the edge
j = jj[v]; // same as above
if (mat[i][j] != val || mat[i][j] <= prev) {
printf("No at [%d][%d] (val=%d)\n", i, j, mat[i][j]);
return 1;
}
}
}
prev = mat[i][j];
}
// finally examine the center value
if (mat[u][u] <= prev) {
printf("No at [%d][%d] (val=%d)\n", u, u, mat[u][u]);
return 1;
}
printf("Yes\n");
return 0;
}
基本概念是生成邊緣的一系列索引,例如:
[0, 1], [0, 2], [0, 3], [0, 4],
[1, 4], [2, 4], [3, 4], [4, 4],
[4, 3], [4, 2], [4, 1], [4, 0],
[3, 0], [2, 0], [1, 0], [0, 0]
通過使用變數i和j陣列ii[],jj[]。
上面的示例是最外邊緣的索引,并在下一次迭代中進入內邊緣。然后將索引的值與同一邊緣中的另一個值和外邊緣中的前一個值進行比較。
[編輯]
這是一個不使用陣列以外的替代方法mat[100][100]:
#include <stdio.h>
int main() {
int mat[100][100];
int i, j;
int ii, jj; // incremental values for i and j
int n;
int u, v, w; // variables to walk on edges
int val; // value of the element
int prev = -1; // previous value in one outer edge
int length; // length of the edge
// read matrix size and values
printf("Enter the number:\n");
scanf("%d", &n);
printf("Enter the matrix:\n");
for (i = 0; i < n; i ) {
for (j = 0; j < n; j ) {
scanf("%d", &mat[i][j]);
}
}
// loop on the edges
for (u = 0; u < n / 2; u ) { // from outmost edge to inner
i = u; j = u; // index of the north west corner
val = mat[u][u]; // initial value to compare
for (v = 0; v < 4; v ) { // four sides
ii = (v & 1) * ((v & 1) - (v & 2));
// assigned to {0, 1, 0, -1} in order
jj = ((v 1) & 1) * (((v 1) & 1) - ((v 1) & 2));
// assigned to {1, 0, -1, 0} in order
length = n - u * 2 - 1; // length of the edge
for (w = 0; w < length; w ) {
i = ii; // one step ahead on the edge
j = jj; // same as above
if (mat[i][j] != val || mat[i][j] <= prev) {
printf("No at [%d][%d] (val=%d)\n", i, j, mat[i][j]);
return 1;
}
}
}
prev = mat[i][j];
}
// finally examine the center value
if (mat[u][u] <= prev) {
printf("No at [%d][%d] (val=%d)\n", u, u, mat[u][u]);
return 1;
}
printf("Yes\n");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417890.html
標籤:
