简体   繁体   English

检查 C 的数组中是否存在范围内的所有值的最佳方法是什么?

[英]What is the best way to check if ALL values in a range exist in an array in C?

How do I test that an array contains at least once, every integer value from 1 to 3?如何测试一个数组至少包含一次,每个 integer 值从 1 到 3? for example:例如:

#include <stdio.h>

int main()
{
  int array[7] = {2, 3, 4, 6, 5, 1};
  if (/* array contains 1, 2 and 3 */) {
    printf("TRUE\n");
  } else {
    printf("FALSE\n");
  }
}

Thanks for your help in advance!提前感谢您的帮助!

You first need to decide how to specify the range you want.您首先需要决定如何指定您想要的范围。 For instance, do you want to enumerate each value or do you simply want to specify the minimum and maximum values?例如,您想枚举每个值还是只想指定最小值和最大值?

For instance, if you only want to use contiguous values then specifying the minimum and maximum values will be satisfactory.例如,如果您只想使用连续值,那么指定最小值和最大值将是令人满意的。 On the other hand, if you want to specify the first eight even numbers, or the first ten prime numbers, then a minimum and maximum value are not as much help, and an enumerated list may be more useful.另一方面,如果您想指定前 8 个偶数或前 10 个素数,那么最小值和最大值并没有多大帮助,枚举列表可能更有用。

Consider defining a struct containing a single value in the value series you want to identify along with a count of the occurrences of that value.考虑定义一个结构,该结构包含要识别的值系列中的单个值以及该值的出现次数。 Construct an array of those structs sufficiently large to represent each value in your value series.构造一个由足够大的结构组成的数组,以表示值系列中的每个值。

Now iterate through the array you want to study.现在遍历您要研究的数组。 Compare each array value with values in your array of structs.将每个数组值与结构数组中的值进行比较。 If it matches a value increment the corresponding count.如果它匹配一个值,则增加相应的计数。

After examining each value in the array you want to study examine the array of structs.在检查了要研究的数组中的每个值之后,检查结构数组。 If all the counts in the array of struct are greater than 0 your original array contains at least one instance of every value in the series.如果 struct 数组中的所有计数都大于 0,则原始数组包含该系列中每个值的至少一个实例。 If one or more of the counts is 0 then one or more of the values is missing from the array you want to study.如果一个或多个计数为 0,则您要研究的数组中缺少一个或多个值。

what do you think of this?你觉得这怎么样? Basically, you create an array with the values you want to find in your array.基本上,您使用要在数组中找到的值创建一个数组。 You create a function to do that and you send your array and the values you want to find in it.您创建一个 function 来执行此操作,然后发送您的数组和您想要在其中找到的值。 Also send the size of each array to avoid errors.还要发送每个数组的大小以避免错误。 Then for each value you try to find, iterate on the array to find it.然后对于您尝试查找的每个值,迭代数组以找到它。 If you go threw the whole array without finding it, return false cause your value is not inside your array.如果您 go 扔了整个数组而没有找到它,则返回 false ,因为您的值不在数组中。 If you never return false in your for loop, it means you always found the value, therefore you can return true.如果您在 for 循环中从不返回 false,则意味着您始终找到该值,因此您可以返回 true。 All your values are inside the array.您所有的值都在数组内。

bool   areValuesInArray(int *array, int *value, int sizeArray, int sizeValue)
{
    int j;
    for (int i = 0, i < sizeValue; i++)
    {
        j = 0;
        while (j < sizeArray && array[j] != values[i])
             j++;
        if (j == sizeArray)
             return false;
    }
    return true;
}

there are 2 ways, either use the map function to check for all the 3 variables, but the issue is this might take longer with lot of data有两种方法,要么使用 map function 检查所有 3 个变量,但问题是这可能需要更长的时间与大量数据

so probably you can use the includes method.所以可能你可以使用包含方法。

so it becomes array.includes(1) .This returns a boolean.所以它变成array.includes(1) 。这将返回一个boolean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用 c 中的两个数组对 n 的所有值进行插值的最佳方法是什么? - What is the best way to interpolate for all values of n, with two arrays in c? 在c中初始化字符串数组的最佳方法是什么? - What is the best way to initialize the string array in c? 用C逐步填充数组的最佳方法是什么? - What is the best way to progressively fill an array in C? 在Erlang中表示C数组的最佳方法是什么? - What is the best way to represent a C array in Erlang? 在 C 中制作二维数组的最佳方法是什么 - What is the best way to make 2 dimensional array in C 有谁知道 C 检查字符是否在字符串中的最佳方法是什么? - Does anyone know what is the best way in C to check if a character is in a string? 检查文件是否存在于 C 中的最佳方法是什么? - What's the best way to check if a file exists in C? 检查数组是否包含C中另一个数组的所有数组值 - check if array contains all array values from another array in C 在C / C ++中清除char数组的最佳方法是什么? - What is the best way of clearing a char array in C/C++? 将数组的所有元素设置为值的最佳方法是什么? - What's the best way to set all elements of array to a value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM