C语言中如何检查矩阵中是否存在唯一元素

发布于 2025-01-16 10:40:47 字数 485 浏览 2 评论 0原文

我需要帮助编写一个程序来检查矩阵中是否存在唯一元素或根本没有。 首先,程序询问行数和列数。然后它询问矩阵的元素。 如果在矩阵中找到唯一元素,则会打印“找到唯一元素”。 如果没有唯一元素,则会打印“No unique element”。

int rows, cols;
 printf("Enter the number of rows: ");
 scanf("%d", &rows);
 printf("Enter the number of columns: ");
 scanf("%d", &cols);
 
 int matrix[rows][cols];
 for(int row = 0; row < rows; row++){
     for(int col = 0; col < cols; col++){
         scanf("%d", &matrix[row][col]);
     }
}

我感谢您的帮助,非常感谢。

I need help in making a program to check whether there is a unique element in a matrix or none at all.
First, the program asks for the number of the rows and columns. Then it asks for the elements of the matrix.
If there is a unique element found in the matrix, then it prints "Unique element found."
If there is no unique element, then it prints "No unique element."

int rows, cols;
 printf("Enter the number of rows: ");
 scanf("%d", &rows);
 printf("Enter the number of columns: ");
 scanf("%d", &cols);
 
 int matrix[rows][cols];
 for(int row = 0; row < rows; row++){
     for(int col = 0; col < cols; col++){
         scanf("%d", &matrix[row][col]);
     }
}

i appreciate the help, thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2025-01-23 10:40:47

即使是 n 维数组在内存中也是线性的。您可以就地对线性数组进行排序并查找唯一元素。

int compare_ints(const void* a, const void* b) {
  const int arg1 = *(const int*)a;
  const int arg2 = *(const int*)b;
 
  if (arg1 < arg2) return -1;
  if (arg1 > arg2) return 1;
  return 0;
 
  // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
  // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
}

// ... your Q code here

const size_t n = rows * cols;
int* arr = (int*)matrix;
qsort(arr, n, sizeof(int), compare_ints);
for (size_t i = 0; i < n - 1; i++) {
  if (arr[i] == arr[i + 1]) {
    puts("No unique element.");
    return;
  }
}
puts("Unique element found.");

Even n-dimensional arrays are linear in memory. You may sort a linear array inplace and look for unique elements.

int compare_ints(const void* a, const void* b) {
  const int arg1 = *(const int*)a;
  const int arg2 = *(const int*)b;
 
  if (arg1 < arg2) return -1;
  if (arg1 > arg2) return 1;
  return 0;
 
  // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
  // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
}

// ... your Q code here

const size_t n = rows * cols;
int* arr = (int*)matrix;
qsort(arr, n, sizeof(int), compare_ints);
for (size_t i = 0; i < n - 1; i++) {
  if (arr[i] == arr[i + 1]) {
    puts("No unique element.");
    return;
  }
}
puts("Unique element found.");
乖不如嘢 2025-01-23 10:40:47

一种简单的方法是将矩阵解释为一维数组。

例如,

int unique = 0;
size_t n = rows * cols;
int *a = ( int * ) matrix;

for ( size_t i = 0; !unique && i < n; i++ )
{
    size_t j = 0;
    while ( j < i && a[j] != a[i] ) ++j;

    if ( j++ == i )
    {
        while ( j != n && a[j] != a[i] ) ++j;

        unique = j == n;
    }
}  

if ( unique ) puts( "Unique element found." );
else puts( "No unique element." );

要获取唯一元素的行和列,您只需在嵌套循环外部声明变量 i 即可。例如

int *a = ( int * ) matrix;
int unique = 0;
size_t n = rows * cols;
size_t i = 0;

for ( ; !unique && i < n; i += !unique )
{
    size_t j = 0;
    while ( j < i && a[j] != a[i] ) ++j;

    if ( j++ == i )
    {
        while ( j != n && a[j] != a[i] ) ++j;

        unique = j == n;
    }
}  

if ( unique ) 
{
    size_t row = i / cols;
    size_t col = i % cols;
    printf( "Unique element found at %zu row and %zu column.\n", row, col );
}
else 
{
    puts( "No unique element." );
}

A straightforward approach is to interpret the matrix as a one-dimensional array

For example

int unique = 0;
size_t n = rows * cols;
int *a = ( int * ) matrix;

for ( size_t i = 0; !unique && i < n; i++ )
{
    size_t j = 0;
    while ( j < i && a[j] != a[i] ) ++j;

    if ( j++ == i )
    {
        while ( j != n && a[j] != a[i] ) ++j;

        unique = j == n;
    }
}  

if ( unique ) puts( "Unique element found." );
else puts( "No unique element." );

To get the row and the column of the unique element all you need is to declare the variable i outside the nested loops. For example

int *a = ( int * ) matrix;
int unique = 0;
size_t n = rows * cols;
size_t i = 0;

for ( ; !unique && i < n; i += !unique )
{
    size_t j = 0;
    while ( j < i && a[j] != a[i] ) ++j;

    if ( j++ == i )
    {
        while ( j != n && a[j] != a[i] ) ++j;

        unique = j == n;
    }
}  

if ( unique ) 
{
    size_t row = i / cols;
    size_t col = i % cols;
    printf( "Unique element found at %zu row and %zu column.\n", row, col );
}
else 
{
    puts( "No unique element." );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文