两个(正)整数k和n以及1.打印所有长度为k的数字1-.n的函数。2.返回数字序列

时间:2019-10-29 09:18:19

标签: c recursion

这是完整的问题:

问题3 [30分]。 编写一个由两个正整数k和n组成的函数,其工作原理如下 1.打印长度为k的所有递增序列,其中包含数字1 ... n 2.返回此类序列的数量。例如

int print_k_sequences( int n, int k)
For example, print_k_sequences( 5 , 3) prints the following sequences
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
and returns 10

由您决定打印顺序的具体顺序。仅添加一个空间 在数字之间。不要忘了用新行分隔序列。 您的函数应该在合理的时间内对输入n,k最多进行20次运算。 [提示:使用递归。您可能还需要使用辅助功能]

我的实现仅打印第一个序列,然后停止。我要去哪里错了?

//helper for printing array
void printArr( int arr[], int size){

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

    return;
}

int findSeq ( int arr[], int n, int k){

/* start from last index and find first elelment less than n if righmost element
is n then we have to incremenet arr value with 1 at starting index*/
    int p = k -1;
    while(arr[p == n])
    {
        p=0;
    }

    //  if the last element is the n and the difference of n and k is one greater
    // thant the first elelment that means last sequence is generated
    if(arr[k-1] ==n && (n-k) == arr[0]-1)
    {
        return 0;
    }

    /* else increase the value of array element till n*/
    arr[p] = arr[p] + 1;

    /* the nextr index value of array shoul always be greater than previous value */
    for (int i = p+1; i<k; i++)
    {
        arr[i] = arr[i-1] +1;
    }

    return 1;

}

int print_k_sequences(int n,int k) {
  // implement me

    int arr[k];

    int count = 0;

    /*values of first seq*/
    while (1){
        printArr(arr, k);

        count++;

        if(findSeq(arr, n, k) == 0)
        {
            break;
        }
    }

  return count;
}

这是对其进行测试的代码:请注意,主功能的任何参数都不能更改。

bool test_q3_1()  {
  int ans = print_k_sequences(6, 2);
  if (ans == 15)  {
    // need to also check the actual sequences
    printf("Q3-1 ok\n");
    return true;
  }
  else  {
    printf("Q3-2 ERROR: answer=%d, correct=15 \n", ans);
    return false;
  }
}

bool test_q3_2()  {
  int ans = print_k_sequences(8, 3);
  if (ans == 56)  {
    // need to also check the actual sequences
    printf("Q3-2 ok\n");
    return true;
  }
  else  {
    printf("Q3-2 ERROR: answer=%d, correct=56 \n", ans);
    return false;
  }
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

弄清楚了:)

对于遇到此问题的任何人:

#include <stdio.h>

int numberOfSequences; // global variable to count number of sequences generated

// function to print contents of arr[0..k-1]
void OutputSequence(int arr[], int k) {
    for (int i = 0; i < k; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// function to generate all increasing sequences from 1..n of length k
void generateSequence(int n, int k, int *len, int arr[]) {

    // If length of the array sequence becomes k
    if (*len == k) {
        numberOfSequences++; // we increment the counter by 1
        OutputSequence(arr, k); // and print that sequence
        return;
    }

    int i;
    // If length is 0, then start putting new numbers in the sequence from 1.
    // If length is not 0, then start from previous element +1.
    if (*len == 0)
        i = 1;
    else
        i = arr[*len - 1] + 1;

    // Increase length of the sequence so far
    (*len)++;

    // Put all numbers (which are greater than the previous element) at new position.
    while (i <= n) {
        arr[(*len) - 1] = i;    // adding the new element to the sequence
        generateSequence(n, k, len, arr);// generating the subsequent elements in the sequence
        i++;
    }

    (*len)--;
}

// driver function to print all increasing sequences from 1..n of length k
// and return the number of such sequences
int print_k_sequences(int n, int k) {
    int arr[k]; // array to store individual sequences
    int len = 0; // Initial length of current sequence
    numberOfSequences = 0; // counter to count number of sequences
    generateSequence(n, k, &len, arr);
    return numberOfSequences;
}

int main() {
    int k = 3, n = 5;
    printf("The sequences between 1.. %d  of length %d are:\n", n, k);
    int ans = print_k_sequences(n, k);
    printf("No of sequences= %d\n", ans);
    return 0;
}