Fix my last time about "the problem of business people crossing the river"

zhaozj2021-02-16  57

/ * Is really embarrassed, in the trans () function I didn't change the 4 to Total-1 when I started debugging, and I have been correct, and I have already indicated that the correction is, thank you, and apologize *

#include "stdio.h" #include "string.h" #include "conio.h" file * fp; / * Set up the file pointer to use it in other functions * / struct a {long M, S; Struct A * next;}; / * Array type A: Record the number of businessmen and servants on the boat, M: represents the number of people

S: Represents the number of servants * / struct a * jj, head; / * head is the chain module unit of the head pointer (the number of people on the board) * / int N, Total = 0, JS = 0; / * Total Indicates the total number of conditions on the ship * / struct aim {long M1, S1, M2, S2; INT N; Struct AIM * back, * next;}; / * Used to establish a two-way pointer chain table, record the case, M1 S1 means that the number of businessmen to be exported and the number of servants; M2, S2 means that the number of businessmen and servants are exported, and n represents the number of times of return * / int K1, K2; Void Freeit (Struct Aim * p) {Struct AIM * p1 = p; p1 = p-> back; free (p); if (p1! = null) p1-> next = null; return;

} / * Release the cell and restore the NEXT pointer of the cell on * /

INT DETERM (STRUCT AIM * P) {STRUCT AIM * P1 = P; if (P-> S1> K2) Return -1; / * The number of servants cannot exceed the total number of servants * / if (p-> m1> k1) Return -1; / * The number of business people cannot exceed the total number of people * / if (p-> s2> k2) return -1; / * Side of the same shore, the same * / if (p-> m2> k1) Return -1; / * Side , The same * / if (p-> s1 <0) return -1; / * The number of servants cannot be negative * / if (p-> s2 <0) return -1; / * Business people cannot be negative * / if ( P-> m1 <0) Return -1; / * Sipay, the same * / if (p-> m2 <0) return -1; / * on the other side, the same * / if (p-> m1! = 0) IF ( P-> S1> P-> m1) return -1; if (p-> m2! = 0) IF (P-> S2> P-> M2) Return -1; / * The number of business people on both sides cannot be less than the number of servants * / while (p1! = null) {p1 = p1-> back; if (p1! = null) IF (p1-> n% 2 == p-> n% 2) IF (P1-> S1 == P -> S1) IF (P1-> S2 == P-> S2) IF (p1-> m1 == p-> m1) IF (p1-> m2 == p-> m2) return -1;} / * Used to resolve duplicate, algorithm idea: to compare each of the chain table units each time, if repeated, then repeat, the loop * / if (P-> S1 == 0 && P-> m1 == 0) IF (P- > N% 2 == 0) Return 1; Else Return -1; / * Obviously if the condition is reached, the * / return 0;} / * Judgment function * / int Sign (int N) {if (n% 2 == 0) RETURN-1; RETURN 1;} / * symbol function * / void copyit (struct aim * p3, struct aim * p) {p3-> S1 = P-> S1; p3-> S2 = P-> S2; p3-> m1 = p-> m1; p3-> m2 = p-> m2; p3-> n = p-> n 1; p3-> back = p; p3-> next = null; P- > next = p3;

} / * Copy the content function, write the contents of the content in P3 in the linked list pointer pointed to the * / void print (STRUCT AIM * P3) {STRUCT AIM * P = P3; JS ; while (p-> back) {P = P-> back;} printf ("/ nit's% d: / n", js); fprintf (fp, "/ nit's% d: / n", js); while (p) {printf ("% LD, % ld ::% ld,% ld / t ", p-> m1, p-> S1, p-> m2, p-> S2); FPRINTF (FP,"% LD,% ld ::% LD,% LD / T ", P-> M1, P-> S1, P-> M2, P-> S2); P = P-> Next ;;} / * Print function, print the content referred to in P3 * * /

Void Trans (STRUCT AIM * P) {structure AIM * P3; / * P3 is the application structure pointer * / struct a * fla; INT I, J, F, E; FLA = & head; p3 ​​= (Struct AIM *) Malloc (Struct AIM); f = Sign (p-> n); for (i = 0; i Next; COPYIT (P3, P); P3-> S1 - = fla-> m * f; p3-> m1- = fla-> s * f; p3-> s2 = fla-> m * f; p3-> m2 = fla-> s * f; / * operation process , That is, the river process * / j = determ (p3); / * Judgment, J record judgment result * / if (j == - 1) {if (i

IF (j == 0) TRANS (P3);} return;} / * Transfer function, will transfer the river * // * n = 0 * /

Main () {struct AIM * P, * P1; Int J, A, E, F; Struct A * flag; / * flag is used with record head pointer * / file * fpt; if ((fpt = fopen) : Result.dat "," W ")) == 0) {PrintF (" can't create it / n "); exit (0);} fp = fpt; clrs CR (); p = (struct aim *) Malloc (Struct AIM); p-> back = null; p-> next = 0; P-> M2 = 0; P-> n = 1; / * Set up an initial head pointer * / printf ("please input the total of people on the board / n"); fprintf (fp, "/ nplease input the total of people on the board / n"); scanf ("% d", & n); fprintf (FP, "/ N% D / N", N); FLAG = & head; for (E = 0; E <= n; E ) for (f = 0; f <= n; f ) IF (E F > 0 && E F <= N) {Total ; JJ = (Struct A *) Malloc (Struct A); JJ-> M = E; JJ-> S = F; Flag-> Next = JJ; JJ- > Next = null; flag = jj;

}

/ ***************************************************** / Printf ("Please Input the Total of Merchant and Salvent As Follow: Mechant , Salvent; / N "); FPRINTF (FP," / NPLEASE INPUT The Total of Merchant and Salvent As Follow: MeChant, Salvent; / N "); Scanf ("% LD,% LD ", & P-> M1, & P -> S1); FPRINTF (FP, "/ N% LD,% ld / n", P-> M1, P-> S1); / **************** ***************** / K1 = P-> m1; K2 = P-> S1; TRANS (P); fclose (fpt); getCh ();} / * one Before the year, I started C language, inadvertently saw this question in several temples: Have n businessman K a servant crossing the river, there is the most M individual in the boat, there is any number of servants on both sides of the shore. Merchants will die, and how to arrange how to arrange the merchant to cross the river, if it is not printed: the recommended start value n = 3, K = 3, M = 2

This problem is that I have thought after I learned my fingers, and I also had a problem in the place where I was raising, and now I wrote that I only have to commemorate, please don't laugh at me, thank you * /

Note: I am not a computer professional, I only want to write fun, and I just want to put myself.

did not expect. . . . However, I have nothing, I only feel that my own style needs to be improved. In fact, the idea is very simple. It is the way to search for the search. First, the number of people on the boat is built into a two-dimensional list, and the number of people on both sides is built into a two-way linked list. In the case where the condition is stored, the loop appearance is solved with the repeated method. In fact, it can be used to solve such a river problem.

Solution to the problem is based on multi-dimensional arithmetic operation, do not add a comment, not me, but I use turboc2.0, I can't use Chinese in its environment, and I am afraid that I am scared in English, but I will increase the misunderstanding. And I know that I don't know much for C, I am two this year.

I learned C in the big two semester, using a "C / C . . "The author is a Tang Dynasty, which is a textbook in Jiangsu Province.

转载请注明原文地址:https://www.9cbs.com/read-22532.html

New Post(0)