自学内容网 自学内容网

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;

    // 计算BMI
    bmi = 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;
}


原文地址:https://blog.csdn.net/m0_52484587/article/details/142370546

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!