【C基础刷题】第九讲

本系列博客为个人刷题思路分享,有需要借鉴即可。

1.目录大纲:
【C基础刷题】第九讲_第1张图片

2.题目链接:

  1. 统计成绩 00:00:00⸺00:09:00题号:BC33 链接:https://www.nowcoder.com/practice/
    cad8d946adf64ab3b17a555d68dc0bba?tpId=290&tqId=39821&ru=/exam/oj
  2. 密码验证 00:09:00⸺00:15:11题号:(⽆)链接:https://www.nowcoder.com/question
    Terminal/df51b60733c94e6fbb0b18fbbf45d76a
  3. 矩阵计算 00:15:11⸺00:20:30题号:BC132 链接:nowcoder.com/practice/0c83231923
    c541d2aa15861571831ee5?tpId=290&tqId=39920&ru=/exam/oj
  4. 逆序输出 00:20:30⸺00:24:50 题号:BC117 链接:https://www.nowcoder.com/practic
    e/bb66c27cb7aa48f9ba7d9f88e4068285?tpId=290&tqId=39905&ru=/exam/oj
  5. 统计数据正负个数 00:24:50⸺00:29:35题号:BC62 链接:https://www.nowcoder.com/
    practice/3f33889582934a09b4e3ddd3cc976226?tpId=290&tqId=39850&ru=/exam/oj
  6. N个数之和 没讲 题号:BC118 链接:https://www.nowcoder.com/practice/b007744eb43b4f7b9
    5d9e87de01528db?tpId=290&tqId=39906&ru=/exam/oj
  7. 最低分和最⾼分之差 00:30:11⸺00:34:25题号:BC119 链接:https://www.nowcoder.co
    m/practice/e0e4f81dcd55408a8973f8033bbeb1d2?tpId=290&tqId=39907&ru=/exam/oj
  8. 有序序列判断 00:34:25⸺00 :44:50题号:BC122 链接:https://www.nowcoder.com/pra
    ctice/22e87f8a8d764a6582710f38d1b40c6e?tpId=290&tqId=39910&ru=/exam/oj
  9. 序列中删除指定数字 00:44:50⸺01:00:05 题号:BC124 链接:https://www.nowcoder.co
    m/practice/7bbcdd2177a445a9b66da79512b32dd7?tpId=290&tqId=39912&ru=/exam/oj
  10. 序列整数去重 01:00:05⸺01:17:50题号:BC125 链接:https://www.nowcoder.com/pr
    actice/6564a2f6e70f4153ad1ffd58b2b28490?tpId=290&tqId=39913&ru=/exam/oj
  11. 有序序列合并:01:17:50⸺01:34:55 题号:BC121 链接:https://www.nowcoder.com/p
    ractice/a9e943b0dab142759807d0cfb6863897?tpId=290&tqId=39909&ru=/exam/oj

3.详解思路:

T1:
【C基础刷题】第九讲_第2张图片

#include 

int main()
{
    int n = 0;
    scanf("%d",&n);
    double min = 100;
    double max = 0;
    double average = 0;
    double sum = 0;
    double x = 0;
    for(int i = 0;i<n;i++)
    {
        scanf("%lf",&x);
        if(x>max)
        max = x;
        if(x<min)
        min = x;
        sum+=x;
    }
    average = sum/n;
    printf("%.2lf %.2lf %.2lf\n",max,min,average);
    return 0;
}

T2:
【C基础刷题】第九讲_第3张图片

#include 
#include

int main() {
    char ch1[20] = {0};
    char ch2[20] = {0};
    //读取
    scanf("%s",ch1);
    scanf("%s",ch2);
    //比较
    if(strcmp(ch1,ch2)==0)
    {
        printf("same\n");
    }
    else {
    {
        printf("different\n");
    }
    }
    return 0;
}

T3:
【C基础刷题】第九讲_第4张图片

#include 

int main() {
    int n = 0;
    int m = 0;
    scanf("%d %d",&n,&m);
    int i = 0;
    int sum = 0;
    int data = 0;
    //读入数据
    for(i = 0;i<n*m;i++)
    {
        scanf("%d",&data);
        if(data>=0)
        {
            sum+=data;
        }
    }
    printf("%d\n",sum);
    return 0;
}

T4:
【C基础刷题】第九讲_第5张图片

#include 

int main() {
    int i = 0;
    int arr[10] = {0};
    //读入
    for(i = 0;i<10;i++)
    {
        scanf("%d",&arr[i]);    
    }
    //输出
    for(i = 0;i<10;i++)
    {
        printf("%d ",arr[10-i-1]);
    }

    return 0;
}

T5:
【C基础刷题】第九讲_第6张图片

#include 

int main() 
{
    int arr[10] = {0};
    int i = 0;
    int p = 0;
    int n = 0;
    for(i = 0;i<10;i++)
    {
    scanf("%d",&arr[i]);
    if(arr[i]>0)
    p++;
    else
    n++;;
    }
    printf("positive:%d\nnegative:%d\n",p,n);
    return 0;
}

T6:
【C基础刷题】第九讲_第7张图片

#include 

int main() {
    int n = 0;
    scanf("%d",&n);
    int i = 0;
    int data = 0;
    int sum = 0;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&data);
        sum+=data;               
    }
    printf("%d\n",sum);
    return 0;
}

T7:
【C基础刷题】第九讲_第8张图片

#include 

int main() {
    int n = 0;
    scanf("%d",&n);
    int max = 0;
    int min = 100;
    int data = 0;
    //读入
    int i = 0;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&data);
        if(data>max)
        max = data;
        if(data<min)
        min = data;        
    }
    printf("%d\n",max-min);
    return 0;
}

T8:
【C基础刷题】第九讲_第9张图片

#include 

int main() {
    int n = 0;
    scanf("%d",&n);

    int arr[50] = {0};
    int on = 0;//升序标识符
    int under = 0;//降序标识符
    
    int i = 0;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&arr[i]);

        if(i>0)
        {
            if(arr[i]>arr[i-1])
            on = 1;
            else if(arr[i]<arr[i-1])
            under = 1;
        }
    }
    //if on+under>1 乱序
    //if on+under ==1 升序/降序
    if(on+under>1)
    printf("unsorted\n");
    else
     {
        printf("sorted\n");
     };    
    return 0;
}

T9:
【C基础刷题】第九讲_第10张图片

#include 

int main() {
    int n = 0;
    scanf("%d",&n);
    //读入数据
    int i = 0;
    int arr[50] = {0};
    for(i = 0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int m = 0;
    scanf("%d",&m);
    int count = n;//计数器
    //遍历数组
    for(i = 0;i<count;i++)
    {
        //遍历数组找到了要删除的数据
        if(m==arr[i])
        {
            //删除数据(后面的数据往前挪动一位)
            int j = 0;
            for(j = i+1;j<n;j++)
            {
                arr[j-1] = arr[j];
            }
            i--;
            count--;
        }
    }
        //打印
        for(i = 0;i<count;i++)
        {
            printf("%d ",arr[i]);
        }
    
    return 0;
}

T10:
【C基础刷题】第九讲_第11张图片

#include 

int main() {
    int n = 0;
    scanf("%d",&n);
    int arr[n];
    //读取数据
    int i = 0;
    for(i = 0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    //去重
    
    for(i = 0;i<n;i++)
    {
        int j = 0;
        for(j = i+1;j<n;j++)
        {
            if(arr[i]==arr[j])
            {
                //挪动数据
                int k = 0;
                for(k = j+1;k<n;k++)
                {
                     arr[k-1] = arr[k];
                }
                n--;//防止遗漏
                j--;//计数
            }

        }
    }
        for(i = 0;i<n;i++)
        {
            printf("%d ",arr[i]);
        }
    
    return 0;
}

T11:
【C基础刷题】第九讲_第12张图片

//思路1:存入数据,之后排序
#include
int main()
{
    int n, m;
    scanf("%d %d\n", &n, &m);
    int i, j, arr[2000];
    int tmp;
    //读入两个数组的数据
    for (i = 0; i < n; i++)
    {
        scanf("%d ", &arr[i]);
    }
    for (i = n; i < n + m; i++)
    {
        scanf("%d ", &arr[i]);
    }
    //排序
    for (i = 0; i < n + m; i++)
    {
        for (j = 0; j < n + m - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
            }
        }
    }
    //打印
    for (i = 0; i < n + m; i++)
    {
        printf("%d ", arr[i]);
    }
}
//思路2:选择性放入一个新的数组
#include
int main() {
    int n = 0;
    int m = 0;
    scanf("%d %d", &n, &m);
    int arr1[n];
    int arr2[m];
    int arr[n + m];

    //读入数据
    int i = 0;
    for (i = 0; i < n; i++) {
        scanf("%d", &arr1[i]);
    }
    for (i = 0; i < m; i++) {
        scanf("%d", &arr2[i]);
    }
    //选择性放入
    int j = 0;
    int k = 0;
    i = 0;
    while (j < n && k < m) {
        
        if (arr1[j] < arr2[k]) 
        {
            arr[i++] = arr1[j];
            j++;
        } 
        else 
        {
            arr[i++] = arr2[k];
            k++;
        }
    }
    if(j==n)
    {
        while(k!=m)
        arr[i++] = arr2[k++];
    }
    if(k==m)
    {
        while(j!=n)
        {
            arr[i++] = arr1[j++];
        }
    }

    i = 0;
    for(i = 0;i<m+n;i++)
    {
        printf("%d ",arr[i]);
    }

}

完。

你可能感兴趣的:(C语言初阶基础刷题,c语言,开发语言)