c++ prime plus-5-編程練習
1,
#include <iostream>
using namespace std;
int main() {
int num1,num2,sum=0;
cout << "請輸入第一個整數(較小的整數):";
cin >> num1;
cout << "請輸入第二個整數(較大的整數):";
cin >> num2;
if(num1 > num2){
int temp = num1;
num1 = num2;
num2 = temp;
}
for(int i = num1;i <= num2; ++i){
sum +=i;
}
cout << num1 << "到" << num2 << "之間所有整數的和為:" << sum << endl;
return 0;
}
2,
#include <iostream>
#include <array>
#include <iomanip>
const int Arsize = 101; // 因为我们要计算到100!,所以需要101个元素
int main() {
std::array<long double, Arsize> factorials; // 使用array对象存储阶乘结果
// 初始化阶乘的前两个元素
factorials[0] = factorials[1] = 1.0L;
// 计算从2到100的阶乘
for (int i = 2; i < Arsize; i++) {
factorials[i] = static_cast<long double>(i) * factorials[i - 1];
}
// 打印100的阶乘
std::cout << "100! = " << factorials[100] << std::endl;
return 0;
}
3,
#include <iostream>
int main() {
int number, sum = 0;
std::cout << "请输入数字(输入0结束): ";
while (true) {
std::cin >> number;
// 检查用户是否输入了0
if (number == 0) {
break; // 如果输入为0,则结束循环
}
// 累加用户的输入
sum += number;
// 报告当前的累计和
std::cout << "到目前为止,所有输入的累计和为: " << sum << std::endl;
std::cout << "请输入数字(输入0结束): ";
}
std::cout << "程序结束。" << std::endl;
return 0;
}
4,
#include <iostream>
#include <iomanip>
int main() {
const double daphne_annual_interest_rate = 0.10; // Daphne的年利率
const double cleo_annual_interest_rate = 0.05; // Cleo的年利率
double daphne_investment = 100.0; // Daphne的初始投资
double cleo_investment = 100.0; // Cleo的初始投资
int years = 0;
// 循环计算每年的投资价值,直到Cleo的投资超过Daphne
while (cleo_investment <= daphne_investment) {
// Daphne的单利计算
daphne_investment += daphne_investment * daphne_annual_interest_rate;
// Cleo的复利计算
cleo_investment += cleo_investment * cleo_annual_interest_rate;
years++; // 增加一年
}
// 输出结果
std::cout << "经过 " << years << " 年后,Cleo的投资价值将超过Daphne。" << std::endl;
std::cout << "Daphne的投资价值: $" << std::fixed << std::setprecision(2) << daphne_investment << std::endl;
std::cout << "Cleo的投资价值: $" << std::fixed << std::setprecision(2) << cleo_investment << std::endl;
return 0;
}
5,
#include <iostream>
#include <string>
int main() {
const int months = 12; // 一年的月份数
std::string monthNames[months] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int sales[months] = {0}; // 存储每个月销售量的数组
int totalSales = 0; // 总销售量
// 输入每个月的销售量
for (int i = 0; i < months; ++i) {
std::cout << "请输入 " << monthNames[i] << " 的《C++ For Fools》销售量: ";
std::cin >> sales[i];
totalSales += sales[i]; // 累加到总销售量
}
// 报告销售情况
std::cout << "《C++ For Fools》全年销售情况如下:" << std::endl;
for (int i = 0; i < months; ++i) {
std::cout << monthNames[i] << " : " << sales[i] << " 本" << std::endl;
}
std::cout << "全年总销售量: " << totalSales << " 本" << std::endl;
return 0;
}
6,
#include <iostream>
#include <string>
int main() {
const int years = 3; // 年数
const int months = 12; // 一年的月份数
std::string monthNames[months] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int sales[years][months] = {0}; // 存储三年每月销售量的数组
int yearlySales[years] = {0}; // 存储每年总销售量的数组
int totalSales = 0; // 三年总销售量
// 输入三年中每个月的销售量
for (int year = 0; year < years; ++year) {
std::cout << "请输入第 " << year + 1 << " 年每个月的《C++ For Fools》销售量:" << std::endl;
for (int month = 0; month < months; ++month) {
std::cout << "请输入 " << monthNames[month] << " 的销售量: ";
std::cin >> sales[year][month];
yearlySales[year] += sales[year][month]; // 累加到每年的总销售量
totalSales += sales[year][month]; // 累加到三年的总销售量
}
}
// 报告每年的销售情况
std::cout << "《C++ For Fools》三年销售情况如下:" << std::endl;
for (int year = 0; year < years; ++year) {
std::cout << "第 " << year + 1 << " 年《C++ For Fools》销售情况:" << std::endl;
for (int month = 0; month < months; ++month) {
std::cout << monthNames[month] << " : " << sales[year][month] << " 本" << std::endl;
}
std::cout << "第 " << year + 1 << " 年总销售量: " << yearlySales[year] << " 本" << std::endl;
}
// 报告三年的总销售量
std::cout << "三年总销售量: " << totalSales << " 本" << std::endl;
return 0;
}
7,
#include <iostream>
#include <string>
// 定义Car结构
struct Car {
std::string make; // 汽车生产商
int year; // 生产年份
};
int main() {
int numCars;
std::cout << "How many cars do you wish to catalog? ";
std::cin >> numCars; // 用户输入汽车数量
// 使用new创建动态数组
Car* carCollection = new Car[numCars];
// 输入每辆汽车的信息
for (int i = 0; i < numCars; ++i) {
std::cout << "Car #" << i + 1 << ":" << std::endl;
std::cout << "Please enter the make: ";
std::getline(std::cin, carCollection[i].make); // 输入生产商
std::cout << "Please enter the year made: ";
std::cin >> carCollection[i].year; // 输入生产年份
std::cin.ignore(); // 忽略换行符,为下一次getline准备
}
// 显示所有汽车信息
std::cout << "Here is your collection:" << std::endl;
for (int i = 0; i < numCars; ++i) {
std::cout << carCollection[i].year << " " << carCollection[i].make << std::endl;
}
// 删除动态分配的内存
delete[] carCollection;
return 0;
}
8,
#include <iostream>
#include <cstring>
int main() {
const std::string endWord = "done";
char word[100]; // 假设单词长度不超过99
int count = 0;
std::cout << "Enter words (to stop, type the word " << endWord << "):\n";
while (true) {
std::cin >> word;
if (std::strcmp(word, endWord.c_str()) == 0) { // 使用 strcmp() 比较
break;
}
count++;
}
std::cout << "You entered a total of " << count << " words.\n";
return 0;
}
9,
#include <iostream>
#include <cstring>
int main() {
const std::string endWord = "done";
char word[100]; // 假设单词长度不超过99
int count = 0;
std::cout << "Enter words (to stop, type the word " << endWord << "):\n";
while (true) {
std::cin >> word;
if (std::strcmp(word, endWord.c_str()) == 0) { // 使用 strcmp() 比较
break;
}
count++;
}
std::cout << "You entered a total of " << count << " words.\n";
return 0;
}
10,
#include <iostream>
int main() {
int rows;
std::cout << "Enter number of rows: ";
std::cin >> rows;
for (int i = 1; i <= rows; ++i) {
// 打印前置的句点
for (int j = 1; j < i; ++j) {
std::cout << ".";
}
// 打印星号
for (int k = 1; k <= i; ++k) {
std::cout << "*";
}
std::cout << std::endl; // 换行
}
return 0;
}
原文地址:https://blog.csdn.net/m0_52484587/article/details/142411670
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!