April 11, 2022

C++ Primer Plus Ch03

只能说转英伦英里,什么加仑balbala至今没搞懂,这题和C Primer Plus简直太像了。–4.11

CH02 Code Answer 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

const short Inch = 12;

int main()
{
using namespace std;

short height;

cout << "Plz enter your height:___\b\b\b";
cin >> height;
cout << "Convert answer: " << height / Inch << "ft " << height % Inch << "inch!";

return 0;
}

CH02 Code Answer 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

const short Inch = 12;
const float Bang = 2.2;
const float Meter = 0.0254;

int main()
{
using namespace std;

float foot, inch, weight;

cout << "Plz enter your foot:___\b\b\b";
cin >> foot;
cout << "Plz enter your inch:___\b\b\b";
cin >> inch;
cout << "Plz enter your weight:___\b\b\b";
cin >> weight;
cout << "Your BMI is " << (weight / Bang) / ((foot * Inch + inch) * Meter);

return 0;
}

CH02 Code Answer 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

const short Degree = 60;
const short Minute = 60;

int main()
{
using namespace std;

float degrees, minutes, seconds;

cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: ";
cin >> degrees;
cout << "Next, enter the minuts of arc: ";
cin >> minutes;
cout << "Finally, enter the seconds of arc: ";
cin >> seconds;
cout << degrees << "degrees, " << minutes << "minutes, " << seconds << "seconds = "
<< degrees + minutes / Degree + seconds / Degree / Minute << " degrees";

return 0;
}

CH02 Code Answer 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

const short Day = 24;
const short Hour = 60;
const short Minute = 60;

int main()
{
using namespace std;

long inputSeconds;
int days, hours, minutes, seconds;

cout << "Enter the number of seconds: ";
cin >> inputSeconds;

days = inputSeconds / Minute / Hour / Day;
hours = (inputSeconds - days * Day * Hour * Minute) / Minute / Hour;
minutes = ((inputSeconds - days * Day * Hour * Minute) - hours * Minute * Hour) / Minute;
seconds = ((inputSeconds - days * Day * Hour * Minute) - hours * Minute * Hour) - minutes * Minute;
cout << inputSeconds << " seconds = " << days << " days, "
<< hours << " hours, "
<< minutes << " minutes, "
<< seconds << " seconds!";

return 0;
}

CH02 Code Answer 5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
using namespace std;

long long worldPopulation, population;

cout << "Enter the world's population: ";
cin >> worldPopulation;
cout << "Enter population of the China: ";
cin >> population;
cout << "The population of the China is " << double(population) / double(worldPopulation) * 100 << "% of the world population!";

return 0;
}

CH02 Code Answer 6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> 

int main()
{
using namespace std;

float miles, calons;

cout << "Enter your miles: ";
cin >> miles;
cout << "Enter your calons: ";
cin >> calons;
cout << "Your drive " << miles / calons << " miles using 1 calon gas";

return 0;
}

// 求 many miles using 1 calon
// miles / calon 即可求多少英里1加仑(距离除燃料)

// 求 many litros using 100 kilometers
// litros / kilometers 即可多少升一百公里(燃料除距离)

CH02 Code Answer 7:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

const float Miles = 62.14;
const float Calons = 3.875;

int main()
{
using namespace std;

float kilometers, litros;

cout << "Enter your kilometers: ";
cin >> kilometers;
cout << "Enter your litros: ";
cin >> litros;
// 转换为整形这个方法比int()更加严格
cout << "Your dirve is " << static_cast<int>((kilometers / 100 * Miles) / (litros / Calons)) << " litros using 100 kilometers!";

return 0;
}
// 求多少英里1加仑,输入x升(3.875升 = 1加仑), y公里(100公里 = 62.14英里)
// y / 100 * 62.14 即英里
// x / 3.875 即加仑
// 所以公式为 (y / 100 * 62.14) / (x / 3.875)

体育课上完回来就搞清楚了!!–4.11

DASCTF X SU
🍬
HFCTF2022
🍪

About this Post

This post is written by P.Z, licensed under CC BY-NC 4.0.