#include
Int a [10], the book [10], n.
Void DFS (int step)
{
int i;
If (step==n + 1)//determine whether for n + 1 box, then n in front of the box has been put good CARDS
{
An arrangement//output
for(i=1; i<=n; I++)
Printf (" % d ", a [I]);
printf("\n");
return;//return to the previous step, also is the last place DFS function called
}
//every time by size rafting
for(i=1; i<=n; I++)
If (book [I]==0)//the card is not used I
{
A [step]=I;//put the card in the first step in a box
The book [I]=1;//tags have been used
DFS (step + 1);//a box under the
The book [I]=0;//will just try playing CARDS, good the next attempt, this step is very important!!!!!!!!!!
}
return;
}
Int main ()
{
The scanf (" % d ", & amp; n);//n should be between 1 ~ 9
DFS (1);//the first standing in front of the number one box
return 0;
}
As above, I will in the DFS statement for 3 input n
The output for 3 full order
But when will I as a global variable can only output 123
Why is this
CodePudding user response:
Because if defined as a local variable, that each call to DFS, creates an I, I and the second layer of the first layer of fineIf defined as global, I will have the same in each layer, I changed in this layer, the next layer will also change, therefore the cycle will be "in advance" in the lower end of the
Welcome to ask if you have any questions, no problem, please timely knot, thank you
CodePudding user response:
Why does it feel so wronged, don't know how to say, with you, because your problem is probably "is defined as a global variable, and why is defined as a local variable gap so big?" Rather than "what's the difference between a global variable and a local variable?" ,First of all, I want to ask you, you understand the global variables, and local variables, is what kind of a difference?
Then, if you have fully understand your meaning of this code?
If you understand right, I don't suppose there will be such a question,
Simple said of the reasons for this problem:
If you put the I declared a global variable, is clearly not enough, you will change each time the DFS call I value, then return to the loop, as the meaning of the index were destroyed,
Speech,
If your example to illustrate the difference between global and local variables, it make me feel like the following scene:
Said there is a bus, you have n of departure, arrived at the first stop on the 2 people, down from the 1 person, arrived at the second stop, the three men down the 2 people,... (omitted here 1000 words) to the first station m, m + 1 person, the m people,
When I read the title, was happy to give the car a few people left, the problem is that the bus stops after the total,,,,,,,
Don't know if you know what I want to express meaning,
CodePudding user response:
Because you have recursive calls function, if it is a local variable, each call to the I are independent of each other,And if it is a global variable, it is the same I
CodePudding user response:
Local variables with each function call is not the same, a global variable is the sameCodePudding user response: