C++ prime plus-3-编程练习
#include <iostream>int main() {// 定义转换因子常量const int INCHES_PER_FOOT = 12;// 声明变量用于存储用户输入的身高int height_inches;// 提示用户输入身高(单位为英寸)std::cout << "请输入您的身高(单位为英寸): ";std::cin >> height_inches;// 计算英尺和剩余的英寸int feet = height_inches / INCHES_PER_FOOT;int inches = height_inches % INCHES_PER_FOOT;// 输出转换结果std::cout << "您的身高是 " << feet << " 英尺 " << inches << " 英寸。" << std::endl;return 0;
}
2,
#include <iostream>
#include <iomanip>int main() {// 定义转换因子常量const double INCHES_PER_FOOT = 12.0;const double INCHES_TO_METERS = 0.0254;const double POUNDS_TO_KILOGRAMS = 0.453592;// 声明变量用于存储用户的身高和体重int feet;int inches;double pounds;double height_meters;double weight_kilograms;double bmi;// 提示用户输入身高(英尺和英寸)std::cout << "请输入您的身高(英尺): ";std::cin >> feet;std::cout << "请输入您的身高(英寸): ";std::cin >> inches;std::cout << "请输入您的体重(磅): ";std::cin >> pounds;// 将身高转换为米height_meters = (feet * INCHES_PER_FOOT + inches) * INCHES_TO_METERS;// 将体重转换为千克weight_kilograms = pounds * POUNDS_TO_KILOGRAMS;// 计算BMIbmi = weight_kilograms / (height_meters * height_meters);// 输出BMI结果std::cout << std::fixed << std::setprecision(2);std::cout << "您的BMI是: " << bmi << std::endl;return 0;
}
3,
#include <iostream>
#include <iomanip>int main() {// 定义转换因子常量const double MINUTES_PER_DEGREE = 60.0;const double SECONDS_PER_MINUTE = 60.0;// 声明变量用于存储用户的纬度信息int degrees;int minutes;double seconds;double decimal_degrees;// 提示用户输入纬度的度、分、秒std::cout << "Enter a latitude in degrees, minutes, and seconds:" << std::endl;std::cout << "First, enter the degrees: ";std::cin >> degrees;std::cout << "Next, enter the minutes of arc: ";std::cin >> minutes;std::cout << "Finally, enter the seconds of arc: ";std::cin >> seconds;// 将度、分、秒转换为度decimal_degrees = degrees + (minutes / MINUTES_PER_DEGREE) + (seconds / (MINUTES_PER_DEGREE * SECONDS_PER_MINUTE));// 输出转换后的纬度std::cout << std::fixed << std::setprecision(4);std::cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << decimal_degrees << " degrees" << std::endl;return 0;
}
4,
#include <iostream>
#include <iomanip>int main() {// 定义转换因子常量const long long SECONDS_PER_MINUTE = 60;const long long MINUTES_PER_HOUR = 60;const long long HOURS_PER_DAY = 24;// 声明变量用于存储用户输入的秒数long long seconds;long long days, hours, minutes, remaining_seconds;// 提示用户输入秒数std::cout << "Enter the number of seconds: ";std::cin >> seconds;// 计算天数days = seconds / (SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY);// 计算剩余秒数seconds %= (SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY);// 计算小时数hours = seconds / (SECONDS_PER_MINUTE * MINUTES_PER_HOUR);// 计算剩余秒数seconds %= (SECONDS_PER_MINUTE * MINUTES_PER_HOUR);// 计算分钟数minutes = seconds / SECONDS_PER_MINUTE;// 计算剩余秒数remaining_seconds = seconds % SECONDS_PER_MINUTE;// 输出转换后的时间std::cout << std::fixed << std::setprecision(0);std::cout << seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << remaining_seconds << " seconds" << std::endl;return 0;
}
5,
#include <iostream>
#include <iomanip>int main() {// 定义转换因子常量const long long SECONDS_PER_MINUTE = 60;const long long MINUTES_PER_HOUR = 60;const long long HOURS_PER_DAY = 24;// 声明变量用于存储全球人口和美国人口long long world_population;long long us_population;// 提示用户输入全球人口和美国人口std::cout << "Enter the world's population: ";std::cin >> world_population;std::cout << "Enter the population of the US: ";std::cin >> us_population;// 计算美国人口占全球人口的百分比double percentage = (static_cast<double>(us_population) / world_population) * 100;// 输出结果std::cout << std::fixed << std::setprecision(5);std::cout << us_population << " people in the US is " << percentage << "% of the world population." << std::endl;return 0;
}
6,
#include <iostream>
#include <iomanip>int main() {// 定义转换因子常量const long long SECONDS_PER_MINUTE = 60;const long long MINUTES_PER_HOUR = 60;const long long HOURS_PER_DAY = 24;// 声明变量用于存储全球人口和美国人口long long world_population;long long us_population;// 提示用户输入全球人口和美国人口std::cout << "Enter the world's population: ";std::cin >> world_population;std::cout << "Enter the population of the US: ";std::cin >> us_population;// 计算美国人口占全球人口的百分比double percentage = (static_cast<double>(us_population) / world_population) * 100;// 输出结果std::cout << std::fixed << std::setprecision(5);std::cout << us_population << " people in the US is " << percentage << "% of the world population." << std::endl;return 0;
}
7,
#include <iostream>
#include <iomanip>
#include <cmath>int main() {// 定义转换因子常量const double KM_TO_MI = 62.14;const double LITERS_TO_GALLONS = 3.875;// 声明变量用于存储欧洲风格的耗油量double european_style;double american_style;// 提示用户输入欧洲风格的耗油量std::cout << "Enter the fuel consumption in liters per 100 kilometers: ";std::cin >> european_style;// 计算美国风格的耗油量american_style = (KM_TO_MI / european_style) / LITERS_TO_GALLONS;// 输出结果std::cout << std::fixed << std::setprecision(2);std::cout << european_style << " L/100km is equivalent to " << american_style << " mpg" << std::endl;return 0;
}