zoukankan      html  css  js  c++  java
  • 华为机试——删除含7元素和被7整除的元素

    C_C++_WP_04. Remove Particular Elements in an Array

    • Description:

    Input an array, in which each element must be greater than 0 and less than 999. Then, remove following elements:

    l Elements that can be evenly divided by 7

    l Elements containing the digit 7.

    The number of elements left in the array is returned. The order of the remaining elements in the array does not change.

    • Function to be implemented:

    int vDelete7Data(int arrIn[],int Num, int arrOut[]);

    [Input] arrIn: array consisting of integers

    Num: number of elements in the array

    [Output] arrOut: array with the particular elements removed

    The output is the number of elements in the array with the particular elements removed by the function.

    [Note] The order of the numbers in the array must not change.

    • Example 1

    Input: 1,2,3,5,4,6,7,14,10

    Output: 1,2,3,5,4,6,10

    The value 7 is returned.

    • Example 2

    Input: 73,64,70,75,6,3

    Output: 64,6,3

    The value 3 is returned.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53

    #include <iostream>
    using namespace std;
     
    int vDelete7Data(int arrIn[],int Num, int arrOut[])
    {
        if ((arrIn == NULL) || (Num <= 0) || arrOut == NULL)
        {
            return 0;
        }
     
        int count = 0;
        for (int i = 0; i < Num; i++)
        {
            int tmp = arrIn[i];
     
            if (tmp % 7 == 0)
            {
                continue;
            }
            else
            {
                while ((tmp != 0) && (tmp % 10 != 7))
                {
                    tmp /= 10;
                }
                if (tmp == 0)
                {
                    *arrOut = arrIn[i];
                    arrOut++;
                    count++;
                }
                else
                {
                    continue;
                }
            }
        }
     
        return count;
    }
     
    int main() {
     
        int arrIn[] = {73,64,70,75,6,3};
        int arrOut[10] = {0};
        cout << vDelete7Data(arrIn,6, arrOut) << endl;
     
        for (int i = 0; arrOut[i] != 0; i++)
        {
            cout << arrOut[i] << endl;
        }
        return 0;
    }
  • 相关阅读:
    使用Docker Swarm搭建分布式爬虫集群
    如果你不知道做什么,那就学一门杂学吧
    正则表达式re.sub替换不完整的问题现象及其根本原因
    Visual Studio 2019 正式版今日发布 key
    net core 记录自定义端口多个方式
    HTTP Error 500.0
    来自后端的逆袭 blazor简介 全栈的福音
    创建一个RAS 非对称 公私密钥示例
    树莓派安装window ioT
    WPF USB设备采集开源工具介绍
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3193058.html
Copyright © 2011-2022 走看看