C ++ questions (18)

Source: Internet
Author: User

In the geography lesson, four students answered the question of the hour of China's four major freshwater lakes:
A: Dongting Lake is the largest, Hong ze Lake is the smallest, and Poyang Lake is the third.
B: The largest lake in Hong Ze, the smallest Lake in Dongting Lake, the second largest lake in Poyang Lake, and the third largest lake in Taihu Lake
C: The smallest in Poyang Lake, and the third in Dongting Lake.
Ding: the largest in Poyang Lake, the smallest in Taihu Lake, the second in Hong ze Lake, and the third in Dongting Lake.
Each lake has only one correct answer. Determine the size of the four lakes.
Write the analysis ideas and code. This is a teacher's question. Because of the tight schedule, I am also working on it. At the same time, I would like to ask for the best design ideas!

// A, B, c, d = Dongting Lake, Hongze Lake, Poyang Lake, Taihu Lake
# Define N 4
Char A [n] = {'A', 'B', 'C', 'D '}
Void fun (char a [], const int I, cosnt int N)
{
If (I = N-1)
{
Bool C1 = (a [0] = 'A' & A [1]! = 'B' & A [2]! = 'C ')
| (A [0]! = 'A' & A [1] = 'B' & A [2]! = 'C ')
| (A [0]! = 'A' & A [1]! = 'B' & A [2] = 'C'
)
Bool C2 = ............................................................
Bool C3 = ............................................................
If (C1 & C2 & C3 & C4)
For (Int J = 0; j <n; j ++)
Cout <"no." <j + 1 <"yes" <A [J] <Endl;
}
Else
For (int s = I; S <n; s ++)
{
Char temp = A [s];
A [s] = A [I];
A [I] = temp;
Fun (A, I + 1, n );
Temp = A [s];
A [s] = A [I];
A [I] = A [s];
}
}

 

# Include <iostream>
# Include <fstream. h>

Class run
{
Public:

Void read ()
{
M_a = new int [4];
M_ B = new int [4];
M_c = new int [4];
M_d = new int [4];
Ifstream in;
In. Open ("rr.txt ");
Char T;
// While (! In. EOF ())
//{
For (INT I = 0; I! = 4; ++ I)
{
In. Get (t );
M_a [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_ B [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_c [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_d [I] = atoi (& T );
}
//}
}

Void set ()
{
M_a = new int [4];
M_ B = new int [4];
M_c = new int [4];
M_d = new int [4];
Cout <"lake1 | lake2 | lake3 | lake4" <Endl;
Cout <":";
For (INT I = 0; I! = 4; ++ I)
Cin> M_a [I];
Cout <"B :";
For (I = 0; I! = 4; ++ I)
Cin> m_ B [I];
Cout <"C :";
For (I = 0; I! = 4; ++ I)
Cin> M_c [I];
Cout <"D :";
For (I = 0; I! = 4; ++ I)
Cin> m_d [I];
}

Void sort ()
{
For (lake1 = 1; lake1! = 5; ++ lake1)
{
For (lake2 = 1; lake2! = 5; ++ lake2)
{
If (lake2 = lake1)
{
Continue;
}
For (lake3 = 1; lake3! = 5; ++ lake3)
{
If (lake3 = lake1 | lake3 = lake2)
{
Continue;
}
For (lake4 = 1; lake4! = 5; ++ lake4)
{
If (lake1 = lake4 | lake2 = lake4 | lake3 = lake4)
{
Continue;
}
If (isright (M_a) & isright (m_ B) & isright (M_c) & isright (m_d ))
{
Cout <"lake1:" <lake1;
Cout <"lake2:" <lake2;
Cout <"lake3:" <lake3;
Cout <"lake4:" <lake4;
}
}
}
}
}
}

Protected:
Bool isright (INT order [4])
{
Int COUNT = 0;
If (lake1 = order [0]) + + count;
If (lake2 = order [1]) + + count;
If (lake3 = order [2]) + + count;
If (lake4 = order [3]) + + count;
Return COUNT = 1;
}

Protected:
Int * M_a;
Int * m_ B;
Int * M_c;
Int * m_d;

Int lake1;
Int lake2;
Int lake3;
Int lake4;
};

Void main ()
{
Run Arun;
// Arun. Set ();
Arun. Read ();
Arun. Sort ();
}

I just wrote one '.
Contains a "RR" in the program folder. TXT "contains (1243421334553124). The numbers indicate that lake 1 is the largest, Lake 2 is the second largest, and Lake 3 is 4th largest, lake 4 is 3rd good.
A total of 16 numbers represent this.
But I don't know why the program cannot find the correct answer ''' depressing ing

Wrong or wrong. The above algorithm is incorrect. This is a reasoning problem.
The idea is like this.
Int A [4], B [4], C [4], d [4]; // the answers of the four students. The first and fourth digits of each array represent Pan Taihong, respectively.
A [4] = {1, 3, 2, 4}; // For the answers of four people, initialize the array first. This is the answer of Jia.
B [4] = {....};
C [4] = {....};
D [4] = {....}; // If there is no, It is 0.
Int I;
For (I = 0; I <4; I ++)
{(A [I] = B [I])? A [I] = B [I] = 0;
(B [I] = C [I])? B [I] = C [I] = 0;
...
...
...
}
// There must be an array with only one element not 0;
Continue to judge that the corresponding project of this item is given to 0;
Then there won't be a few numbers.
Then I want to scan the array to judge the pull. I want to pull it out, but I don't have time to pull it.

The proposed algorithm is incorrect. This is a reasoning problem.
The idea is like this.
Int A [4], B [4], C [4], d [4]; // the answers of the four students. The first and fourth digits of each array represent Pan Taihong, respectively.
A [4] = {1, 3, 2, 4}; // For the answers of four people, initialize the array first. This is the answer of Jia.
B [4] = {....};
C [4] = {....};
D [4] = {....}; // If there is no, It is 0.
Int I;
For (I = 0; I <4; I ++)
{(A [I] = B [I])? A [I] = B [I] = 0;
(B [I] = C [I])? B [I] = C [I] = 0;
...
...
...
}
// There must be an array with only one element not 0;
Continue to judge that the corresponding project of this item is given to 0;
Then there won't be a few numbers.
Then I want to scan the array to judge the pull. I want to pull it out, but I don't have time to pull it.

 

 

# Include <iostream>
# Include <fstream. h>

Class run
{
Public:

Void read ()
{
M_a = new int [4];
M_ B = new int [4];
M_c = new int [4];
M_d = new int [4];
Ifstream in;
In. Open ("rr.txt ");
Char T;
// While (! In. EOF ())
//{
For (INT I = 0; I! = 4; ++ I)
{
In. Get (t );
M_a [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_ B [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_c [I] = atoi (& T );
}
For (I = 0; I! = 4; ++ I)
{
In. Get (t );
M_d [I] = atoi (& T );
}
//}
}

Void set ()
{
M_a = new int [4];
M_ B = new int [4];
M_c = new int [4];
M_d = new int [4];
Cout <"lake1 | lake2 | lake3 | lake4" <Endl;
Cout <":";
For (INT I = 0; I! = 4; ++ I)
Cin> M_a [I];
Cout <"B :";
For (I = 0; I! = 4; ++ I)
Cin> m_ B [I];
Cout <"C :";
For (I = 0; I! = 4; ++ I)
Cin> M_c [I];
Cout <"D :";
For (I = 0; I! = 4; ++ I)
Cin> m_d [I];
}

Void sort ()
{
For (lake1 = 1; lake1! = 5; ++ lake1)
{
For (lake2 = 1; lake2! = 5; ++ lake2)
{
If (lake2 = lake1)
{
Continue;
}
For (lake3 = 1; lake3! = 5; ++ lake3)
{
If (lake3 = lake1 | lake3 = lake2)
{
Continue;
}
For (lake4 = 1; lake4! = 5; ++ lake4)
{
If (lake1 = lake4 | lake2 = lake4 | lake3 = lake4)
{
Continue;
}
If (isright (M_a) & isright (m_ B) & isright (M_c) & isright (m_d ))
{
Cout <"lake1:" <lake1;
Cout <"lake2:" <lake2;
Cout <"lake3:" <lake3;
Cout <"lake4:" <lake4;
}
}
}
}
}
}

Protected:
Bool isright (INT order [4])
{
Int COUNT = 0;
If (lake1 = order [0]) + + count;
If (lake2 = order [1]) + + count;
If (lake3 = order [2]) + + count;
If (lake4 = order [3]) + + count;
Return COUNT = 1;
}

Protected:
Int * M_a;
Int * m_ B;
Int * M_c;
Int * m_d;

Int lake1;
Int lake2;
Int lake3;
Int lake4;
};

Void main ()
{
Run Arun;
// Arun. Set ();
Arun. Read ();
Arun. Sort ();
}

I just wrote one '.
Contains a "RR" in the program folder. TXT "contains (1243421334553124). The numbers indicate that lake 1 is the largest, Lake 2 is the second largest, and Lake 3 is 4th largest, lake 4 is 3rd good.
A total of 16 numbers represent this.
But I don't know why the program cannot find the correct answer ''' depressing ing

Problem Analysis:
The variables A, B, C, and D can be used for Dongting Lake, Hongze Lake, Poyang Lake, and Taihu Lake. The value of each variable is 1 ~ 4.
Because only one correct answer is given, their statement can be expressed:
A: (A = 1) + (B = 4) + (C = 3) =-1
B: (B = 1) + (A = 4) + (C = 2) + (D = 3) =-1
C: (B = 4) + (A = 3) =-1
Ding :( c = 1) + (D = 4) + (B = 2) + (A = 3) =-1
The correct answer can be found only when all the preceding conditions are met.

For a = 1 to 4
For B = 1 to 4
For c = 1 to 4
D = 10-a-B-c
If a * B * C * D <> 24 Then goto 5
P1 = (a = 1) + (B = 4) + (C = 3)
P2 = (B = 1) + (A = 4) + (C = 2) + (D = 3)
P3 = (B = 4) + (A = 3)
P4 = (C = 1) + (D = 4) + (B = 2) + (A = 3)
If not (p1 =-1 and P2 =-1 and P3 =-1 and P4 =-1) Then goto 5
Print "Dongting Lake", "Hongze Lake", "panyang Lake", "Tai Lake"
Print a, B, c, d
5 next C, B,
End

There are two groups of answers:
1. The largest Dongting Lake, second Hong ze Lake, third Taihu Lake, and fourth pan yang Lake
2. largest Taihu Lake, second in Poyang Lake, third in Dongting Lake, and fourth in Hongze Lake
The code can be debugged in VC ++ 6.0 + Windows2000
**************************************** *******
Conditional Analysis:
1. Lake name analysis:
Lake name: Dongting Lake, Hongze Lake, Poyang Lake, Taihu Lake
Abbreviation: 1, 2, 3, 4
In a single bit
2. Size Analysis:
0, 1, and 2 respectively indicate the size of the lake, 3 is the maximum, and 0 is the minimum.
In 10
3. The comments of the I-th person are the first subscript of the Two-dimensional array.

Add conditions to the array a [4] [4] based on the above principles

For example, the "largest Dongting Lake" is a [0] [0] = 31.
**************************************** *******
# Include "stdio. H"
# Include "iostream. H"

Int COUNT = 1;

Bool valid2 (int A, int B)
{
Int AI = 0;
Int AJ = 0;
Int Bi = 0;
Int bj = 0;

Ai = A/10;
Bi = B/10;
AJ = A % 10;
Bj = B % 10;

If (AI = Bi) & (AJ = BJ) | (Ai! = Bi) & (AJ! = BJ )))
Return true;
Else
Return false;

}

Bool valid (int A, int B, int C, int D)
{
If (valid2 (a, B) & valid2 (a, c) & valid2 (a, d) & valid2 (B, c) & valid2 (B, d) & valid2 (c, d ))
Return true;
Else
Return false;
}

Void printlake (int)
{
Switch (a % 10)
{
Case 1: cout <"Dongting Lake" <"" <A/10 <Endl;
Break;
Case 2: cout <"Hong ze Lake" <"" <A/10 <Endl;
Break;
Case 3: cout <"Poyang Lake" <"" <A/10 <Endl;
Break;
Case 4: cout <"Taihu" <"" <A/10 <Endl;
Break;
Default:
Break;
}

}

Void printall (int A, int B, int C, int D)
{
Cout <Endl;
Cout <"----------------- The Right" <"<count <" <"-- begin (the bigger the larger) -------" <Endl;
Printlake ();
Printlake (B );
Printlake (C );
Printlake (d );
Cout <"----------------- The Right" <"<count <" <"-- end --------------------------------" <Endl;
Count ++;
}

Void main ()
{
Int I;
Int J;
Int K;
Int m;

Int A [4] [4] = {0}; // array of Initialization Conditions
A [0] [0] = 31;
A [0] [1] = 02;
A [0] [2] = 13;
A [0] [3] = 24; // It must be filled in; otherwise, it may be less than 24 cases.

A [1] [0] = 32;
A [1] [1] = 01;
A [1] [2] = 23;
A [1] [3] = 14;

A [2] [0] = 03;
A [2] [1] = 11;

A [3] [0] = 33;
A [3] [1] = 22;
A [3] [2] = 11;
A [3] [3] = 04;
Cout <"---- begin -------" <Endl;
For (I = 0; I <3; I ++)
For (j = 0; j <4; j ++)
{
If (valid2 (A [0] [I], a [1] [J]) // accelerates the search speed and exits from an unreasonable branch
{
For (k = 0; k <2; k ++)
For (m = 0; m <4; m ++)
{
If (valid2 (A [2] [K], a [3] [m]) // accelerates the search speed and exits from an unreasonable branch
{
If (valid (A [0] [I], a [1] [J], a [2] [K], a [3] [m]) // validity Determination
Printall (A [0] [I], a [1] [J], a [2] [K], a [3] [m]); // parse and output POSITIVE SOLUTIONS
}
}
}
}
Cout <"---- end ---------" <Endl;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.