我怎样才能使用 scanf 来改变这个算法

How can I use scanf to alter this algorithm

如何使用 Scanf 改变组合?

Write a program that prompts the user for a five digit lab access code, read it in as an unsigned integer then print to the screen each of the five digit access codes (with possible leading zeroes) that are equivalent (there are 32). Be aware that 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button, as well. The picture below is an example of a key access device that has 0 and 1 on the first button, 2 and 3 on the next button and so on.

0|_|{1,2}

1|_|{3,4}

2|_|{5,6}

3|_|{7,8}

4|_|{9,0}

#include <stdio.h>

int main(void) {
#define foreach( intpvar, intary ) int* intpvar; for( intpvar=intary; intpvar < (intary + (sizeof(intary)/sizeof(intary[0]))) ; intpvar++)
    int a1[]={1,2};
    int a2[]={3,4};
    int a3[]={5,6};
    int a4[]={7,8};
    int a5[]={9,0};
    foreach (p1, a1) {
        foreach (p2, a2) {
            foreach (p3, a3) {
                foreach (p4, a4) {
                    foreach (p5, a5) {
                         printf("%d %d %d %d %d\n",*p1,*p2,*p3,*p4, *p5);
                        }
                }
            }
        }
    }
    return 0;
}

since 5 sets of 2 only gives me 10 combinations.

不,它给你 2^5 = 32 种组合,因为你可以选择两个值之一,五次。假设代码是 13579 - 等效代码是:

13579
23579
14579
24579
13679
23679
14679
24679
13589
23589
14589
24589
13689
23689
14689
24689
13570
23570
14570
24570
13670
23670
14670
24670
13580
23580
14580
24580
13680
23680
14680
24680

所有这些代码都对应于按顺序按下五个按钮。总之:你的算法基本上是:

foreach option in group 0
  foreach option2 in group 1
     foreach option3 in group 2
       foreach option4 in group 3
         foreach option5 in group 4
           print option option2 option3 option4 option5