【问题标题】:C - 2 arrays, check if one is a sub array and return indexC - 2个数组,检查一个是否是子数组并返回索引
【发布时间】:2021-03-15 11:11:06
【问题描述】:

我的任务是编写一个函数,该函数将获取 2 个数组及其大小:

int contain(int big[], int size_b, int small[], int size_s) 

程序应该检查小数组是否是大数组的子数组,如果是则返回小数组中第一个数字的索引号,否则返回-1。

示例: 2 4 61 5 8 5 56 89 3 -2

5 56 89 3 -2

函数应该返回 5。

9 5 12 7 8 -2 4 32 900 13

9 5 12 8 7

函数应该返回 -1。

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我是编码新手,所以我从一开始就卡住了,看视频我应该如何解决这个问题
  • 首先对参数进行完整性检查,如果 size_b -1。然后做一个从 0 到小于 size_b - size_s 的外循环,和一个从 0 到小于 size_s 的内循环,比较 big[] 和 small[] 的元素,如果不匹配则提前中断。如果你在没有提前中断的情况下一直通过内循环,那么你已经在大数组中找到了小数组,并且可以返回外循环的当前索引。

标签: arrays c size dimensional


【解决方案1】:

查看 cmets 以查看说明

int contain(int big[], int size_b, int small[], int size_s) {
    int contains, k;
    for(int i = 0; i < size_b; i++){
        // assume that the big array contains the small array
        contains = 1;
        // check if the element at index i in the big array is the same as 
        // the first element in the small array
       if(big[i] == small[0]){
           // if yes, then we start form k = 1, because we already know that
           // the first element in the small array is the same as the element 
           // at index i in the big array
           k = 1;
           // we start to check if the next elements in the big array
           // are the same as the elements in the small array
           // (we start from i+1 position because we already know that the element 
           // at the position i is the same as the first element in the small array)
           for(int j = i + 1; j < size_b; j++){
               // range for k must be from 1 to size_s-1
               // if we reached the end of the small array or if the small 
               // array contains only one element then break the for loop
               if(k >= size_s - 1) {
                   break;
               }
               // if the element at the position j in the big array is different
               // from the element at the position k in the small array then we
               // flag that we did not find the sequence we were looking for (contains=0)
               if(big[j] != small[k]){
                   contains = 0;
                   break;
               }
               // increment k because we want the next element in the small array
               k++;
           }
           // if contains flag is not 0 that means we found the sequence we were looking
           // for and that sequence starts from index i in the big array
           if(contains) {
               return i;
           }
       }
    }
    // if the sequence we were looking for was not found
    // then -1 is returned
    return -1;
}

这里是没有cmets的代码

int contain(int big[], int size_b, int small[], int size_s) {
    int contains, k;
    for(int i = 0; i < size_b; i++){
        contains = 1;
       if(big[i] == small[0]){
           k = 1;
           for(int j = i + 1; j < size_b; j++){
               if(k >= size_s - 1) {
                   break;
               }
               if(big[j] != small[k]){
                   contains = 0;
                   break;
               }
               k++;
           }
           if(contains) {
               return i;
           }
       }
    }
    return -1;
}

【讨论】:

  • 第一个 for 循环可以迭代 (size_b - size_s) 次,以在最后节省不必要的迭代。
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多