The selection structure in C++ still uses if selection structure, if-else selection structure, else-if multi-selection structure and switch multi-selection structure, which are no different from C language. Let’s use question 1119 in the training ground to demonstrate how to use the structure selection to deepen your understanding:
#include<iostream>usingnamespacestd;intmain(){inta;//The three-digit number to be judged intge;//The ones digit in the three-digit number intshi;//The tens digit in the three-digit number intbai;//Three digits Hundreds digit in number cin> >a;ge=a%10;shi=a%100/10;bai=a/100;if(ge*ge*ge+shi*shi*shi+bai*bai*bai==a)cout<<1 <<endl;elsecout<<0<<endl;return0;}
Please be sure to test it on the computer, complete questions 1119 and submit them correctly.
Let’s look at another question. Question 1057, piecewise function question, is a Level 2 question. After reading the question, it is obvious that we should use the else –if multi-selection structure to implement it. Finally, pay attention to using setprecision to control the number of decimal places.
The C++ code is implemented as follows:
#include<iostream>#include<iomanip>usingnamespacestd;intmain(){doublex;doubley;cin>>x;if(x<1){y=x;}elseif(x>=1&&x<10){y=2 *x-1;}else{y=3*x-11;}cout<<fixed<<setprecision(2)<<y<<endl;return0;}
The test run results are as follows:
Please complete the 1057 questions on the computer by yourself, and then read the following chapters after submitting them correctly.