鱼C论坛

 找回密码
 立即注册
查看: 2196|回复: 3

为什么不对

[复制链接]
发表于 2023-11-5 11:12:52 | 显示全部楼层
找出相邻两数商最大值 - C++代码

以下是一个用于找出相邻两数商最大值的示例代码:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main() {
  5.     int n;
  6.     cout << "请输入数组中浮点数的个数(n > 2): ";
  7.     cin >> n;
  8.     vector<float> nums(n);
  9.     cout << "请输入每个浮点数: " << endl;
  10.     for (int i = 0; i < n; i++) {
  11.         cin >> nums[i];
  12.     }
  13.     float maxQuotient = 0;
  14.     int maxIndex = -1;
  15.     for (int i = 1; i < n; i++) {
  16.         float quotient = nums[i] / nums[i - 1];
  17.         if (quotient > maxQuotient) {
  18.             maxQuotient = quotient;
  19.             maxIndex = i;
  20.         }
  21.     }
  22.     cout << nums[maxIndex - 1] << " / " << nums[maxIndex] << " = " << maxQuotient << endl;
  23.     return 0;
  24. }
复制代码

上述代码中,我们首先输入浮点数的个数和每个浮点数,然后使用循环遍历数组,计算相邻两数的商,并记录最大的商和对应的索引。最后输出最大商对应的两个数和商的值。

希望以上代码能够满足你的需求。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-3 06:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表