鱼C论坛

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

为什么不对

[复制链接]
发表于 2023-11-5 11:11:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
描述
给一组数,前后相邻的两个数相除,输出商最大的两个数及商的值。
输入
第一行为数组中浮点数的个数n,n大于2,其余n行为每一行一个浮点数。
输出
以下面的形式输出结果,前两个数是相除的两个数,最后一个数为最大的商值。
printf("%f / %f = %f\n", a[maxi-1], a[maxi], maxq);
#include <iostream>
using namespace std;
#include <vector>
int main()
{
        int n;
        float maxbeichu = 1, maxchu = 1;
        float shang = 1;
        cin >> n;
        cin.ignore();
        vector<float> nums;
        float num;
        for (int i = 1; i <= n; i++)
        {
                cin >> num;
                nums.push_back(num);
        }
        float maxshang = 0;
        for (int i = 1; i < nums.size(); i++)
        {
                float shang = nums[i] / nums[i - 1];
                if (shang > maxshang)
                {
                        maxshang = shang;
                        maxbeichu = nums[i];
                        maxchu = nums[i - 1];
                }
        }
        printf("%f/%f = %f", maxbeichu, maxchu, maxshang);
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-5 11:11:41 | 显示全部楼层
问题在于,输入时使用了  cin.ignore() ,这会导致程序忽略掉第一个数。所以应该将其删除或将其移动到正确的位置上。

以下是修改后的代码:

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. int main()
  5. {
  6.     int n;
  7.     float maxbeichu = 1, maxchu = 1;
  8.     float shang = 1;
  9.     cin >> n;
  10.     vector<float> nums(n);
  11.     for (int i = 0; i < n; i++)
  12.     {
  13.         cin >> nums[ i];
  14.     }
  15.     float maxshang = 0;
  16.     for (int i = 1; i < nums.size(); i++)
  17.     {
  18.         float shang = nums[ i] / nums[i - 1];
  19.         if (shang > maxshang)
  20.         {
  21.             maxshang = shang;
  22.             maxbeichu = nums[ i];
  23.             maxchu = nums[i - 1];
  24.         }
  25.     }
  26.     printf("%f/%f = %f", maxbeichu, maxchu, maxshang);
  27.     return 0;
  28. }
复制代码


此代码已删除  cin.ignore()  并使用了  vector  存储输入的数字。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2023-11-5 11:12:52 | 显示全部楼层
isdkz 发表于 2023-11-5 11:11
问题在于,输入时使用了  cin.ignore() ,这会导致程序忽略掉第一个数。所以应该将其删除或将其移动到正确 ...

还是不对
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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-4-21 14:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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