Peking University C Language Learning Day 12

Evaluation of four arithmetic expressions
Example: Evaluation
of four arithmetic expressions The input is the expression of four arithmetic expressions, which consists only of integers, +,-,
*
, /, (,)
, and there are no spaces. Assume that the operator results are all integers
. "/" The result is also an integer
resolve recursive recursive form of problem
solving ideas:
the expression is defined in a recursive:
Expressions Item
+
-
items
*
/
factor
factor
(expression)
integer
so for expression analysis can be performed recursively

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int factor_value();
int term_value();
int expression_value();
int main()
{
cout << expression_value() << endl;
return 0;
}
输入:(2+3)*(5+7)+9/3
输出: 63
int expression_value() //求一个表达式的值
{
int result = term_value(); //求第一项的值
bool more = true;
while( more) {
char op = cin.peek(); //看一个字符,不取走
if( op == '+' || op == '-' ) {
cin.get(); //从输入中取走一个字符
int value = term_value();
if( op == '+' ) result += value;
else result -= value;
}
else more = false;
}
return result;
}
int term_value() //求一个项的值
{
int result = factor_value(); //求第一个因子的值
while(true) {
char op = cin.peek();
if( op == '*' || op == '/') {
cin.get();
int value = factor_value();
if( op == '*') 
result *= value;
else result /= value;
}
else 
break;
}
return result;
}
int factor_value() //求一个因子的值
{
int result = 0;
char c = cin.peek();
if( c == '(') {
cin.get();
result = expression_value();
cin.get();
}
else {
while(isdigit(c)) {
result = 10 * result + c - '0';
cin.get();
c = cin.peek();
}
}
return result;
}

Example: Climbing stairs
Resolve the problem into smaller sub-problems to solve it
Example: Climbing the stairs The
teacher climbs the stairs, he can walk 1 or 2 steps at a time, enter the number of stairs, and
find different numbers of steps
For example: There are three stairs, he could have gone every level, or for the first time to go one
level, two second walk, you can go two for the first time, to go a second time, a
total of 3 methods.
Input The
input contains several lines, each line contains a positive integer N, representing the number of stairs, 1
<= N <= 30 outputs different number of steps, each line input corresponds to a line,
climbing stairs
outputs
different number of steps, each line input Corresponding to one line of output
Sample input
5
8
10
Sample output
8
34
89
Climbing stairs
n-steps = walking
one level first, n-1 steps +
two steps first, n-2 steps the walk
f (n) = f (n -1) + f (n-2)
boundary conditions:
stairs
n = the walk stairs
after one go, n-1 + and the walk stairs
go After two steps, the walking method of n-2 steps
f (n) = f (n-1) + f (n-2)
boundary condition: n <0 0
n = 0 1
Climbing stairs
n steps: walking
after one level first, n-1 steps walking +
two steps first, n-2 steps
f (n) = f (n -1) + f (n-2)
boundary condition: n <0 0 n = 0 1 n = 1 1
n = 0 1 n = 1 1 n = 2 2

#include <iostream>
using namespace std;
int N;
int stairs(int n)
{
if( n < 0)
return 0;
if( n == 0 )
return 1;
return stairs(n-1) + stairs(n-2);
}
int main()
{
while(cin >> N) {
cout << stairs(N) << endl;
}
return 0;
}

Example: Putting Apples
Put M identical apples in N same dishes. Some dishes are allowed to be left empty.
How many different points are there? 5,1,1 and 1,5,1 are the same division method.
Input The
first line is the number of test data t (0 <= t <= 20). Each line below contains two integers
M and N, separated by spaces. 1 <= M, N <= 10.
Output
For each set of input data M and N, use a line to output the corresponding K.
Sample input
1
7 3
Sample output
8
sample questions: put apples
Let i apples be placed on k plates The total number of methods is f (i, k), then:
Example: Put apples
i set apples on k plates The total number of putting methods is f (i, k), then: when
k> i, f (i, k) = f (i, i)
Example: Put apples and
set i apples on k plates. The total number of putting methods is f (i, k), then: when
k> i, f (i, k) = f (i, i)
k <= i, the total put method = put with empty plate + empty without plate Put method
f (i, k) = f (i, k-1) + f (ik, k)
boundary condition?
Example: Putting Apples

#include <iostream>
using namespace std;
int f(int m,int n) {
if( n > m )
return f(m,m);
if( m == 0)
return 1;
if( n <= 0 )
return 0;
return f(m,n-1) + f(m-n,n);
}
int main() {
int t,m,n;
cin >> t;
while( t--) {
cin >> m >> n;
cout << f(m,n) << endl; 
}
return 0;
}

Example: Count 24

Given 4 positive integers less than 10, you can use the addition, subtraction, multiplication and division of 4 operations and parentheses to
connect these 4 numbers to get an expression. The question now is whether there is
a way to make the result of the expression equal to 24.
The addition, subtraction, multiplication and division, and the operation results and priority of the brackets are
consistent with our usual definitions (the definition of division here is real division).
For example, for 5, 5, 5, 1, we know that 5 * (5 – 1/5) = 24, so we
can get 24. As another example, for 1, 1, 4, 2, we can never get
24.
Example: Counting 24
inputs The
input data includes multiple lines, each line gives a set of test data, including 4 less than 10 positive integers
. The last set of test data includes 4 zeros, indicating the end of the input, and this set of data does not need to be
processed.
Output
For each set of test data, output one line. If 24 is available, output "YES";
otherwise, output "NO".
Sample input
5 5 5 1
1 1 4 2
0 0 0 0
Sample output
YES
NO
Example problem: Count 24
n numbers to calculate 24, there must be two numbers to be calculated first. The results of these two numbers, and the remaining n-2 numbers,
constitute the problem of finding 24 for n-1 numbers.
Enumerate the two numbers that are calculated first, and the calculation method of the two numbers.
Boundary conditions?
Note: Whether floating point comparisons are equal, you cannot use ==
Example: Count 24
n numbers to calculate 24, there must be two numbers to be calculated first. The results of these two numbers, and the remaining n-2 numbers,
constitute the problem of finding 24 for n-1 numbers.
Enumerate the two numbers that are calculated first, and the calculation method of the two numbers.
Boundary conditions: a count number 24
Note: Floating-point comparison for equality can not ==
Example: Count 24

#include <iostream>
#include <cmath>
using namespace std;
double a[5];
#define EPS 1e-6
bool isZero(double x) {
return fabs(x) <= EPS; 
}
bool count24(double a[],int n) 
{//用数组a里的 n个数,计算24
if( n == 1 ) {
if(isZero( a[0] - 24) )
return true;
else
return false;
}
double b[5];
for(int i = 0;i < n-1; ++i)
for(int j = i+1;j < n; ++j) { //枚举两个数的组合
int m = 0; //还剩下m个数, m = n - 2
for(int k = 0; k < n; ++k) 
if( k != i && k!= j)
b[m++] = a[k];//把其余数放入b
b[m] = a[i]+a[j];
if(count24(b,m+1))
return true;
b[m] = a[i]-a[j];
if(count24(b,m+1))
return true;
b[m] = a[j]-a[i];
if(count24(b,m+1))
return true;
b[m] = a[i]*a[j];
if(count24(b,m+1))
return true;
if( !isZero(a[j])) {
b[m] = a[i]/a[j];
if(count24(b,m+1))
return true;
}
if( !isZero(a[i])) {
b[m] = a[j]/a[i];
if(count24(b,m+1))
return true;
}
}
return false;
}
int main()
{
while(true) {
for(int i = 0;i < 4; ++i)
cin >> a[i];
if( isZero(a[0]))
break;
if( count24(a,4))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
Published 193 original articles · Likes 66 · Visits 10,000+

Guess you like

Origin blog.csdn.net/qq_42403069/article/details/105549791