five Einstein ladder
Title No.: Exp02-Basic10,GJBook3-04-15
Title: Einstein ladder
Problem Description: there are steps. I don't know the number, but I know: each step spans 2 steps, and there is 1 step left at the end; Each step spans 3 steps, and there are 2 steps left at last; Each step spans 5 steps, and finally there are 4 steps left; Each step spans 7 steps, just to the roof. How many steps are there in programming.
Input: None
Output: number of steps
Example: none. See input / output description for details.
#include <iostream> using namespace std; int main() { int a; for (a = 7;;a += 14) { if ((a % 3 == 2) && (a % 5 == 4)) { cout << a; break; } } return 0; }
Combining mathematical ideas with algorithms will simplify operations
From "step by step 7, just to the roof", we know that a should be a multiple of 7, that is, a must be an odd number, so we don't need to add the condition (a%2==1) (a is an even number) to the for loop;
Then it should be noted that the title requires "how many steps are there in programming?" based on this, we need to exit the for loop after outputting the first a.
six Nested function
Title No.: Exp02-Basic05
Title: nested function
Title Description: write a program. When x=1.0, 2.0,..., 20.0, calculate the following functions to 5 levels of nesting. F(x)=1+1/(1+1/(1+1/(1+1/(1+1/x))))
Input: a floating-point number represents the value of X. input ensures that x is not zero.
Output: the value of a floating-point number F(x), with 3 decimal places reserved.
Example:
1
1.625
Seeing that some students directly copy this nested function into the code and then print it out, I don't recommend this practice (the loop structure is not strengthened, and the symptoms are not the root cause. What if y nested functions are output?), so let's write a for loop
#include <iostream> #include <iomanip> using namespace std; int main() { double x = 1.0, y = 1.0; cin >> x; if (x != 0) { for (int i = 0;i < 5;i++) { y = 1 + 1.0 / x; x = y; } cout << fixed << setprecision(3) << y; } return 0; }
seven Bracket statistics
Title No.: Exp02-Basic02
Title: bracket statistics
Topic Description: program to judge whether the number of '(' and ', [' and ']', '{' and '}' in a given character sequence ending with '@' is equal.
Input: enter a string of characters ending with the character '@', which may contain several white space characters.
Output: unequal number of parentheses (in the order of curly brackets, square brackets and parentheses); If the number of three parentheses is equal, NULL is output.
Example 1:
Input: {a+b*c+(d/e-f]}}@
Output: {} [] ()
Example 2:
Input: {a + b*c+(d/e-f]}@
Output: [] ()
#include <iostream> using namespace std; int main() { char ch=0; int a1 = 0, a2 = 0, b1 = 0, b2 = 0, c1 = 0, c2 = 0;//Counter while(ch!='@') { cin >> ch; if (ch == '(') a1++; if (ch == ')') a2++; if (ch == '[') b1++; if (ch == ']') b2++; if (ch == '{') c1++; if (ch == '}') c2++; } if (a1 != a2) cout << "()"; if (b1 != b2) cout << "[]"; if (c1 != c2) cout << "{}"; if((a1==a2)&&(b1==b2)&&(c1==c2)) cout << "NULL"; return 0; }
Seeing the above code, the old audience must jump up and say, "what? Don't you love the for loop anymore?", hahaha, actually not
The while loop is very useful when the number of cycles is unknown, which is also a small disadvantage of the for loop
eight (program questions)
Title No.: Exp02-Enhance04,GJBook3-04-14
Title: letter matrix
Title Description: use circular statements to control the printing of the following graphics, in which each letter output occupies 2 character width (space first and letter last).
Input: None
Output: letter matrix as shown in the figure above
Note: please realize the program according to the law of letters and positions. Play the table for a while and test two lines~
Talk less and go straight to the code
#include <iostream> using namespace std; int main() { cout << " A B C D E F G H I" << endl; cout << " B C D E F G H I A" << endl; cout << " C D E F G H I A B" << endl; cout << " D E F G H I A B C" << endl; cout << " E F G H I A B C D" << endl; cout << " F G H I A B C D E" << endl; cout << " E F G H I A B C D" << endl; cout << " D E F G H I A B C" << endl; cout << " C D E F G H I A B" << endl; cout << " B C D E F G H I A" << endl; cout << " A B C D E F G H I" << endl; return 0; }
Wrong, let's start again! No students will really type their watches in the way above, no, no
Let's write it with circular statements
#include <iostream> using namespace std; int main() { char arr[18] = {'A','B','C','D','E','F','G','H','I','A','B','C','D','E','F','G','H','I' }; for (int i = 0;i < 6;i++)//Number of rows { //Content of line for (int j = i;j < 9 + i;j++) { cout << " "<<arr[j]; } cout << endl; } for (int i = 5;i >0;i--) { for (int j = i-1;j <i+8;j++) { cout << " "<<arr[j]; } cout << endl; } return 0; }
Ingenious definition of array
You can see that when defining an array, you do not select A to I, but from A to I and then from A to I. ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha
This is actually done to avoid writing another function that shifts left in a loop
The for loop is the same. The outer layer defines the line and the inner layer defines the content of the line
In particular, when outputting the contents of each line, make sure to output 9 characters