简体   繁体   English

C - 来自用户输入数字的所有数字组合

[英]C - all combinations of numbers from user's input number

I have the following code but it is not working correctly:我有以下代码,但它无法正常工作:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>

#define MAXN 100
const unsigned n=4;
const unsigned k=2;
int taken[MAXN];

void print(unsigned i)
{
    unsigned l;
    printf(" ( ");
    for (l=0; l<=i-1; l++) printf("%u ", taken[l] + 1);
    printf(")\n");
}

void variate(unsigned i) 
{
    unsigned j;
    if (i>=k) 
    {
        print(i);
        return;
    }
    for (j=0;j<n;j++)
        {
        taken[i]=j;
        variate(i+1);
        }    
}

void condition(unsigned number) 
{
    unsigned j;
    if (number>=k) 
    {
        print(number);
        return;
    }
    for (j=0;j<n;j++)
        {
        taken[number]=j;
        variate(number+1);
        }

}

int main(void)
{
    variate(0);

      int number;
      printf("Enter number from 1 to 4: \n", number);
      scanf("%d", &number);

      printf("All varians of the combinations with your number are: \n");
    condition(0);

 system ("pause");
  return 0;
}

The program is printing all possible combinations of the numbers 1, 2, 3 and 4 and it works correctly.该程序正在打印数字 1、2、3 和 4 的所有可能组合,并且可以正常工作。 But the void condition is not working fine.但是void condition不能正常工作。 After printing all possible combinations of the four numbers the user have to enter a number between 1 and 4 and all combinations with user's number have to appear and none of the rest of the combinations.打印完四个数字的所有可能组合后,用户必须输入 1 到 4 之间的数字,并且必须出现所有包含用户号码的组合,其余的组合均不出现。

More the remarks already done there are several strange things in your program更多已经完成的评论在您的程序中有一些奇怪的事情

  • the read number is never used, so there is no way to give a result depending on it从未使用读取的编号,因此无法根据它给出结果
  • you modify taken to give all the couples but you do not reset it before to give the couples with the read number, supposing taken is useful how that can works in this case ?修改采取给所有的夫妻,但你不前重置它给与读取数情侣,假设采取非常有用如何在这种情况下可以工作?
  • and globally why so complicated ?全球范围内为什么如此复杂?

You can do like that, with two ways to do the second part :你可以这样做,有两种方法可以做第二部分:

#include <stdio.h>

#define MIN 1
#define MAX 4

int main()
{
  /* print all couples */
  for (unsigned i = MIN; i <= MAX; ++i) {
    for (unsigned j = MIN; j <= MAX; ++j) {
      printf("(%u %u)\n", i, j);
    }
  }

  unsigned number;
  printf("Enter number from %d to %d\n", MIN, MAX);
  scanf("%u", &number);

  /* first way following the same order as before */
  puts("same order");

  if ((number >= MIN) && (number <= MAX)) {
    for (unsigned i = MIN; i <= MAX; ++i) {
      for (unsigned j = MIN; j <= MAX; ++j) {
        if ((i == number) || (j == number))
          printf("(%u %u)\n", i, j);
      }
    }
  }

  /* an other way, faster but not in the same order */
  puts("different order");

  printf("(%u %u)\n", number, number);
  for (unsigned i = MIN; i < number; ++i) {
    printf("(%u %u)\n(%u %u)\n", number, i, i, number);
  }
  for (unsigned i = number + 1; i <= MAX; ++i) {
    printf("(%u %u)\n(%u %u)\n", number, i, i, number);
  }

  return 0;
}

Execution :执行 :

(1 1)
(1 2)
(1 3)
(1 4)
(2 1)
(2 2)
(2 3)
(2 4)
(3 1)
(3 2)
(3 3)
(3 4)
(4 1)
(4 2)
(4 3)
(4 4)
Enter number from 1 to 4
2
same order
(1 2)
(2 1)
(2 2)
(2 3)
(2 4)
(3 2)
(4 2)
different order
(2 2)
(2 1)
(1 2)
(2 3)
(3 2)
(2 4)
(4 2)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM