# Super star MOOC learning pass high level language programming C + + experiment 02 branch and loop programming of Jilin University (level 2021)

## nine   Three digit Armstrong number

Title No.: Exp02-Basic08, GJBook3-04-12

Title: three digit Armstrong number

Title Description: write a program to print all 3-bit Armstrong numbers. Armstrong number refers to the number whose value is equal to the sum of each number cube. For example, 153 is an Armstrong number. 153=

Input: None
Output: print all 3-bit Armstrong numbers. Each Armstrong number is separated by a Western space, and there are no redundant characters after the last number.

Example: none. See input / output description for details

```#include<iostream>

using namespace std;

int main()
{
int a, b, c;

for(int i=100;i<1000;i++)
{
a = i % 10;        //Bit
b = i / 10 % 10;   //Ten
c = i / 100;       //Hundredth

if ((a * a * a + b * b * b + c * c * c) == i)
cout << i<<" ";
else continue;
}

return 0;

}```

This question is quite friendly. Points to note:

1. Use the number between (100-1000) to represent three digits

2. Constantly take the modulus to find the number on each bit of i

## ten   Qualified natural number

Title No.: Exp02-Basic06, GJBook3-04-09

Title: qualified natural number

Title Description: write a program to print all natural numbers less than positive integer data and divisible by 11.

Input: input a positive integer data from the keyboard

Output: output all natural numbers less than data and divisible by 11. There is a space between numbers, and there are no redundant characters after the last number.

Example 1:

`Input: 50`
`Output: 0   eleven   twenty-two   thirty-three   forty-four`

Example 2:

`Input: 80`
`Output: 0   eleven   twenty-two   thirty-three   forty-four   fifty-five   sixty-six   seventy-seven`
```#include <iostream>

using namespace std;

int main()
{
long long data = 0;
cin >> data;

for (int i = 0;i < data;i+=11)
{
cout << i << " ";
}

return 0;

}```

He's coming, he's coming, beautiful for loop, he's coming

Observe the output sample, which contains 0. The result is a multiple of 11. On this basis, add 11 directly

There is no need to write another if statement to judge whether i "is a multiple of 11, and if so, output"

## eleven   Calculate e^x

Title No.: Exp02-Enhance01, GJBook3-04-02

Title: calculating e^x

Title Description: please calculate the sum of the first 101 items of the above sequence (up to n, take 100)

Input: a floating point number corresponding to the x value.

Output: a floating-point number, that is, the approximate value of e^x, which is reserved to the second place after the decimal point.

Note: the math.h header file and related pow and exp functions are not allowed in this topic.

Example 1:

`Input: 0`
`Output: 1.00`

Example 2:

`Input: 4.3`
`Output: 73.70`

```#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double x = 0, y = 1.00, z = 1.00;
cin >> x;

for (int i = 1.00;i <= 100;i++)
{
z *= x / i;
y += z;
}

cout << fixed << setprecision(2) << y << endl;

return 0;

}```

Carefully observe whether this formula is very similar to Taylor expansion

To be safe, floating point numbers are always set to double

Recall again how C + + and C control the decimal places after the standard output. What is the specific method

## twelve   sort ascending

Title No.: Exp02-Basic01, GJBook3-03-03

Title: incremental sorting

Title Description: any three real numbers a, b and c are output in the order from small to large.

Input: enter three real numbers (all test data have only one decimal place).

Output: the three real numbers input before are output in the order from small to large, separated by a Western space, and each real number retains 1 digit after the decimal point.

Example:

```Input:
2.3  5.6  1.2```
```Output:
1.2 2.3 5.6```

Here are two ways to sort:

1. "Hand on hand" method     2. Pointer array method (recommended)

First look at the "hands on the line" method  ， Logic is also clear, requiring high observation and memory. a. There are 6 sorting methods in B and C. constantly compare the relationship between the two of the three to determine their position. The disadvantage is that it is only applicable to the sorting of two or three numbers. If there is too much data, the complexity increases greatly, and the pointer array method is needed

```#include <iostream>

using namespace std;

int main()
{
double a, b, c;
cin >> a >> b >> c;

if (a > b)
{
if (b > c)
cout << c << " " << b << " " << a << endl;

else if(a < c)
cout << b << " " << a << " " << c << endl;

else if((a > c)&&(c > b))
cout << b << " " << c << " " << a << endl;

}
else
{
if (b < c)
cout << a << " " << b << " " << c << endl;

else if ((a < c) && (c < b))
cout << a << " " << c << " " << b << endl;

else if (c < a)
cout << c << " " << a << " " << b << endl;

}

return 0;

}```

Let's look at the pointer array method. Bubble sort is a basic sort type. Students need to master the following basic structure

It is also applicable to the sorting of multiple numbers

```#include <iostream>

using namespace std;

int main()
{
double arr[3];

for (int i = 0;i < 3;i++)//Enter three data
{
cin >> arr[i];
}

for (int i = 0;i < 3 - 1 ;i++)//Bubble sorting
{
for (int j = 0;j < 3 - 1 - i;j++)
{
if (arr[j] > arr[j + 1])
{
double tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}

for (int i = 0;i < 3;i++)//Output printed array
{
cout << arr[i]<<" ";
}

return 0;

}```

Tags: C++ Back-end

Posted on Thu, 18 Nov 2021 19:06:14 -0500 by eddierosenthal