当前位置: 首页 > news >正文

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;
}


http://www.mrgr.cn/news/30562.html

相关文章:

  • 比ChatGPT更酷的AI工具
  • 【机器学习】数学知识:标准差,方差,协方差,平均数,中位数,众数
  • python共享全局变量的方案
  • 低代码、配置式web组态软件
  • 前端知识点---Javascript的对象(Javascript)
  • 网站运营:如何从零开始做好网站内容建设?
  • C++标准的一些特性记录:C++11的auto和decltype
  • 每天五分钟深度学习框架pytorch:pytorch中已经定义好的损失函数
  • 【洛谷】P1546 [USACO3.1] 最短网络 Agri-Net 的题解
  • 倍增练习(1)
  • pg入门3—详解tablespaces1
  • 恶意Bot流量识别分析实践
  • 计算机毕业设计 奖学金评定管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • Python 入门教程(4)数据类型 | 4.6、列表
  • vulnhub靶机:Breach 2.1详细过程
  • [Unity Demo]从零开始制作空洞骑士Hollow Knight第四集:制作更多的敌人
  • keep-alive的应用场景
  • C8T6--SPI读FLASH和双通信
  • 目前人工智能时代,程序员如何保持核心竞争力?
  • 项目小总结
  • Web开发:ABP框架2——入门级别的增删改查Demo
  • C++ Primer Plus(速记版)-类和数据抽象
  • LeetCode 876
  • Mysql中DML和DQL
  • 代码随想录八股训练营第四十天| C++
  • Activiti7《第二式:破剑式》——工作流中的以柔克刚