[C Brush Question] Matrix Equality Judgment and Deletion of Specified Numbers in the Sequence

Table of contents

BC105- Matrix Equality Judgment

Method 1: After the two matrices are input, compare them

Method 2: Direct comparison during reception

BC98 - Delete the specified number from the sequence

Method 1: Change the element to be deleted to 0

Method 2: Print elements that do not need to be deleted

Method 3: Define two subscripts i and j (motion picture demonstration)


This article is about the problem-solving ideas and code sharing of Niuke.com, thank you for your visit and guidance.

Before the C99 standard:
the size of the array can only be a constant to specify int arr[10]

C99 supports constants to specify the size of arrays, and also supports variable-length arrays.

In the C99 standard, one is introduced: the concept of variable-length array, which allows the size of the array to be specified by variables.

int n = 10;

int arr[n];

Need to pay attention to the points of variable length arrays:

It should be noted that when using a variable-length array, the size of the array must be determined at the time of definition, so the size of the array cannot be changed after definition.

Let me illustrate with a question I have done before:

Correct way of writing: first have n input, and then use n.

int n=0;
scanf("%d",&n);
int arr[n];

Wrong way of writing: n has already been used, and it is useless to enter it again.

int n=0;
int arr[n];
scanf("%d",&n);//这样写是错误的。

BC105- Matrix Equality Judgment

The meaning of the title:

The meaning of the title is simply that, when the first input is required, input the number of rows and columns of the matrix, then input the elements of the first matrix, and then input the elements of the second matrix. If the elements of the two matrices are all equal, Then output "Yes", otherwise there is an unequal element and output "NO"

Method 1: After the two matrices are input, compare them

Ideas:

1. Enter the number of rows and columns of the matrix,

2. Then use a for loop to input elements, and the array is recorded as arr1.

3. The other uses a for loop to input elements, and the array is recorded as arr2.

4. Define a flag as a condition for judging whether the matrices are equal

5. Compare the two matrices. If there are unequal elements between the two matrices, set flag=0, and then use the goto statement to jump out of the loop.

6.打印Yes or No。

#include <stdio.h>
 int main() {
     int n=0;
     int m=0;
     scanf("%d %d",&n,&m);
     int arr1[n][m];
     int arr2[n][m];
     int i=0;
     int j=0;
//接收第1个数组的内容
     for(i=0;i<n;i++)
     {
       for(j=0;j<m;j++)
        {
           scanf("%d",&arr1[i][j]);
        }
     } 
     //接收第2个数组的内容
     for(i=0;i<n;i++)
     {   
           for(j=0;j<m;j++)
        {
           scanf("%d",&arr2[i][j]);
        }
     } 
     //比较
    int flag=1;//假设是相等的
     for(i=0;i<n;i++)
     {
       for(j=0;j<m;j++)
        {
            if(arr1[i][j]!=arr2[i][j])
            {
                flag=0;
                goto end;
            }
        }
     }
 end:
     if(flag==1)
        printf("Yes\n");
     else
        printf("No\n");
     return 0;
 }

submit: 

Method 2: Direct comparison during reception

int main() {
    int n=0;
    int m=0;
    scanf("%d %d",&n,&m);
    int arr1[n][m];
    int i=0;
    int j=0;
 //接收第一个数组的内容
    for(i=0;i<n;i++)
    {
          for(j=0;j<m;j++)
       {
          scanf("%d",&arr1[i][j]);
       }
    } 
    //接收第2个数组的内容,并比较
   int tmp=0;
   int flag=1;//假设是相等的
    for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
       {
          scanf("%d",&tmp);
          if(tmp != arr1[i][j])
           {
             flag=0;
             goto end;
           }
       }
    }
end:
    if(flag==1)
    printf("Yes\n");
      else
      printf("No\n");
    return 0;
}

implement:

BC98 - Delete the specified number from the sequence

Method 1: Change the element to be deleted to 0

Ideas:

1. First define the size of the array as n, and then input the elements of the array through the for loop.

2. Define the variable del as the element to be deleted, and input the element to be deleted. Traversing the array, if arr[i] encounters del at this moment, change arr[i] to 0

3. Print all the elements in the input array that are not 0.

int main() {
   int n=0;
   int m=0;
   scanf("%d",&n);
   int arr[n];
   int i=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int del=0;
    scanf("%d",&del);
   for(i=0;i<n;i++)
   {
        if(arr[i]==del)
            arr[i]=0;//将要删除的元素改为0
   }
    for(i=0;i<n;i++)
      {
    if(arr[i]!=0)//打印不是0的元素
      {
        printf("%d ",arr[i]);
      }
   }
    return 0;
}

 

Method 2: Print elements that do not need to be deleted

Idea: After inputting the element del to be deleted, print the array directly, use a judgment condition to skip the element to be deleted, and print other items that do not need to be deleted.

int main() {
   int n=0;
   int m=0;
   scanf("%d",&n);
   int arr[n];
   int i=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int del=0;
    scanf("%d",&del);
    for(i=0;i<n;i++)
   {
    if(arr[i]!=del)
      {
        printf("%d ",arr[i]);
      }
   }
    return 0;
}

Method 3: Define two subscripts i and j (motion picture demonstration)

The process of animation demonstration:

#include<stdio.h>
int main()
{
   int n=0;
   //int m=0;
   scanf("%d",&n);
   int arr[n];
    int i=0;
    int j=0;
    for(i=0;i<n;i++)
      {
        scanf("%d",&arr[i]);
      }
     int del=0;
     scanf("%d",&del);
     for(i=0;i<n;i++)
     {
       if(arr[i]!=del)
        {
            arr[j++]=arr[i];
        }
      }
     for(i=0;i<j;i++)
        {
            printf("%d ",arr[i]);
        }
      return 0;
}

 This article is over, if there are any mistakes, please correct me, thank you for your support!

Guess you like

Origin blog.csdn.net/weixin_65186652/article/details/131960291