Use the formula below to find the approximate value of pi. π/4≈1-1/3+1/5-1/7+ ... Until the absolute value of the last item is less than 10-7. According to the given algorithm it is easy to write programs as follows:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
int s=1;
Double n=1,t=1,pi=0;
while ((Fabs (t)) >1e-7)
{
pi=pi+t;
n=n+2;
S=-s;
t=s/n;
}
pi=pi*4;
cout<< "pi=" <<setiosflags (ios::fixed) <<setprecision (6) <<pi<<endl;
return 0;
}
Run result is
Note: Do not define n as an integer variable, otherwise in the execution of "t=s/n;" , the value of the T is 0 (the reason is divided by two integers).
"Example" Fibonacci the first 40 number of series. This series has the following characteristics: 1th, 2 numbers are 1, 1. Starting with the 3rd number, each number is the sum of the previous two digits. That
F1=1 (n=1)
f2=1 (n=2)
fn=fn-1+fn-2 (n≥3)
This is an interesting classical math problem: there are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, small rabbit after the 3rd month after the birth of a pair of rabbits each month, assuming that all rabbits are not dead, ask the number of rabbits each month for how much?
According to the monthly number of rabbits given, the program can be written as follows:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
long f1,f2;
int i;
f1=f2=1;
for (i=1;i<=20;i++)
{
cout<<setw () <<f1<<setw (a) <<f2;
The device output field width is 12, each output two number
if (i%2==0) cout<<endl;
After each output 4 number of lines, so that each row output 4 number of
f1=f1+f2;
The F1 on the left represents the 3rd number and the f2=f2+f1 of the 1th 2 numbers
;
The F2 on the left represents the 4th number, the 2nd 3 number and the
0.
"Example" to find all prime numbers between 100~200. The program is written as follows:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
int m,k,i,n=0;
BOOL prime;//defines the Boolean variable prime for
(m=101;m<=200;m=m+2)//discriminant M is prime, m varies from 101 to 200, and the increment is 2
{
prime=true;// At the beginning of the loop, the prime is assumed to be true, that is, M is the prime
k=int (sqrt (m));//The integral part of the square root m with K for
(i=2;i<=k;i++)//This circular effect is to divide m by 2~ square root m, check whether divisible
if (m%i==0)//If divisible, means M is not prime
{
prime=false;//make Prime into false break
;//Terminate execution of this loop
} if
(prime)//if M is prime
{
COUT<<SETW (5) <<m; Output prime m, field width is 5
n=n+1;//n is used to accumulate the number of output primes
}
if (n%10==0) cout<<endl;//Output 10 numbers after the line is changed
}
cout <<endl;//last run a newline return
0;
}
"Example" translation code
In order to keep the message confidential, the message is often converted into a cipher according to certain rules, and the addressee is translated back to the original text according to the law of the Agreement. For example, a message can be converted to a password by turning the letter A into a letter e,a to E, which becomes the 4th letter thereafter, and W becomes a,x into b,y into a c,z into D. See figure 3.20, the letters are converted according to the above rules, and non-alphabetic characters are invariant, such as "wonderful!" Convert to "asrhivjyp!".
Enter a line of characters that requires the corresponding password to be exported.
The procedure is as follows:
#include <iostream>
using namespace std;
int main ()
{
char C;
while ((C=getchar ())!= ' \ n ')
{
if (c>= ' a ' && c<= ' z ') | | (c>= ' A ' && c<= ' Z '))
{
c=c+4;
if (c> ' z ' && c<= ' z ' +4 | | c> ' z ')
c=c-26;
}
cout<<c;
}
cout<<endl;
return 0;
}
The results of the operation are as follows:
I am going to Beijing!↙
M eq ksmrk xs fimnmrk!
The expression in parentheses in the while statement has 3 effects:
Read a character from the keyboard, which is implemented using the GetChar function;
Assignments The read Word to the character variable C;
Determine if the character is ' \ n ' (that is, a newline character). If the newline character executes the compound statement in the while statement (that is, the statement within the curly braces), converts the characters of the input, which are not line-breaks.
The input characters are processed according to the preceding analysis, and one thing to note to the reader is that the embedded if statement cannot be written as:
if (c> ' z ' | | | c> ' z ') c=c-26;
The "C=c-26" is also performed because all lowercase letters satisfy the "c> ' Z" condition; Statement, this can be an error. It is therefore necessary to limit the range to "C> ' z ' && c<= ' z ' +4", i.e. the original letter is ' W ' to ' Z ', not the original uppercase letter w~z, and should not be converted according to this rule.