华为机试——删除含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;
}
原文地址:https://www.cnblogs.com/helloweworld/p/3193058.html