[C language] one question per day (a collection of errors)

Recently, I spent a lot of time doing questions on Niuke and Likou, and I was miserable. Sometimes I couldn’t think of it after racking my brains. After learning from the pain, I recorded and wrote more difficult questions every day.
The wrong set of
insert image description here
titles is shown in the figure above

The topic seems very simple at first glance, but looking at the example again, isn’t it just an ordered array to find duplicates and missing ones? It feels like it can be killed in seconds, but I don’t know, I’m already dead.
After seeing the example, the subject is preconceived and misleads himself. Pay attention to the review ! !

Method 1: Violent cycle, through the double for loop of the inner layer and the outer layer , because the title clearly states that it is an integer from 1 to n
, so

static int arr[2];
int* findErrorNums(int* nums, int numsSize, int* returnSize)
{
    
    
    *returnSize=2;
    int i=0;
    int j=0;
    for(i=1;i<=numsSize;i++)//外层循环,从1开始
    {
    
    
        int count=0;//计数器
        for(j=0;j<numsSize;j++)
        {
    
    
           if(i==nums[j])
             count++;//对于i,出现相同时+1,正常情况下,count为1
        }
         if(count==2)//当为2时,说明出现重复
            arr[0]=i;
         if(count==0)//为0时,缺失
            arr[1]=i;
    }
    return arr;
}

Method 2.
Use bubble sorting (because I am not good at learning and can only bubble sorting), rewrite the array into an ordered array. At this time,
when finding the number of repetitions, you only need to traverse the array and compare it with the previous one to get
the result . When the number covered
needs to be classified and discussed
1. When the beginning is lost

When it is 22 compare nums[0] with 1

2. When the end is missing

When it is 1233, compare nums[numssize-1] with numssize

3. When in the middle,
the difference with the front or back is 2,
for example

When it is 1224, 4-2=2
When it is 1334, 2-1=2

static int arr[2];
int* findErrorNums(int* nums, int numsSize, int* returnSize)
{
    
    
    *returnSize=2;
    int i=0;
    int j=0;
    int tmp=0;
    for(i=0;i<numsSize-1;i++)//冒泡排序
    {
    
    
        for(j=0;j<numsSize-1-i;j++)
        {
    
    
            if(nums[j]>nums[j+1])
            {
    
    
                tmp=nums[j];
                nums[j]=nums[j+1];
                nums[j+1]=tmp;
            }
        }
    }
    for(i=0;i<numsSize-1;i++)//找重复
    {
    
    
        if(nums[i]==nums[i+1])
        {
    
    
            arr[0]=nums[i];
            break;
        }
    }
     //找被覆盖的数,然后分情况
    if(nums[0]!=1)
        arr[1]=1;
    if(nums[numsSize-1]!=numsSize)
        arr[1]=numsSize;
    for(i=1;i<numsSize;i++)
    {
    
    
        if(nums[i]-nums[i-1]==2)
        {
    
    
            arr[1]=nums[i]-1;
            break;
        }
    }
    return arr;
}

It will be updated every day in the future, if there is something wrong, please point it out in time

Guess you like

Origin blog.csdn.net/2301_78636079/article/details/132264410