鱼C论坛

 找回密码
 立即注册
查看: 1023|回复: 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是修改后的代码:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    float maxbeichu = 1, maxchu = 1;
    float shang = 1;
    cin >> n;
    vector<float> nums(n);
    for (int i = 0; i < n; i++)
    {
        cin >> nums[ i];
    }
    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;
}

此代码已删除  cin.ignore()  并使用了  vector  存储输入的数字。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

还是不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

以下是一个用于找出相邻两数商最大值的示例代码:
#include <iostream>#include <vector>using namespace std;int main() {    int n;    cout << "请输入数组中浮点数的个数(n > 2): ";    cin >> n;    vector<float> nums(n);    cout << "请输入每个浮点数: " << endl;    for (int i = 0; i < n; i++) {        cin >> nums[i];    }    float maxQuotient = 0;    int maxIndex = -1;    for (int i = 1; i < n; i++) {        float quotient = nums[i] / nums[i - 1];        if (quotient > maxQuotient) {            maxQuotient = quotient;            maxIndex = i;        }    }    cout << nums[maxIndex - 1] << " / " << nums[maxIndex] << " = " << maxQuotient << endl;    return 0;}
上述代码中,我们首先输入浮点数的个数和每个浮点数,然后使用循环遍历数组,计算相邻两数的商,并记录最大的商和对应的索引。最后输出最大商对应的两个数和商的值。

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

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 13:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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