C++ 有人知道如何在连续输入之间获得最大的差异吗?

C++ 有人知道如何在连续输入之间获得最大的差异吗?,c++,math,C++,Math,我得到了最大的数字和差异,但我不知道如何显示最大的差异,但我不知道从那里我只是想显示输入之间的最大差异。谢谢 #include <iostream> #include <vector> using namespace std; int main() { int integers; int highest = 0; int difference; int counter = 0; int lastInteger; ve

我得到了最大的数字和差异,但我不知道如何显示最大的差异,但我不知道从那里我只是想显示输入之间的最大差异。谢谢

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int integers;
    int highest = 0;
    int difference;
        int counter = 0;
    int lastInteger;
    vector<int> numbers;
    cout << "Please enter in integers and enter 0 to stop: ";
    while(cin >> integers)
    {
      if(integers == 0)
      {
          break;
      }
      counter ++ ; 
      if(counter > 1)
      {
      difference = integers - lastInteger;
        cout << difference << endl;
      }
      lastInteger = integers;
    }
    cout << "  The highest number is " << highest << endl;
    cout << "the Differnce is " << difference << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
整数;
int最高=0;
智力差异;
int计数器=0;
整数;
向量数;
cout>整数)
{
如果(整数==0)
{
打破
}
计数器++;
如果(计数器>1)
{
差=整数-最后一个整数;

cout引入一个新的整数变量
maxDifference
,以保持最大差值并将其初始化为零。然后每次计算
差值的新值时,将其与
maxDifference
进行比较,如果它更大,则将其值分配给
maxDifference
。类似这样的情况:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int integers;
    int highest = 0;
    int difference;
    int maxDifference = 0;
    int counter = 0;
    int lastInteger;
    vector<int> numbers;
    cout << "Please enter in integers and enter 0 to stop: ";
    while(cin >> integers)
    {
      if(integers == 0)
      {
          break;
      }
      counter ++ ; 
      if(counter > 1)
      {
      difference = integers - lastInteger;
      if (difference > maxDifference)
      {
        maxDifference = difference;
      }
        cout << difference << endl;
      }
      lastInteger = integers;
    }
    cout << "  The highest number is " << highest << endl;
    cout << "the Differnce is " << difference << endl;
    cout << "the maximal differnce is " << maxDifference << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
整数;
int最高=0;
智力差异;
int-maxDifference=0;
int计数器=0;
整数;
向量数;
cout>整数)
{
如果(整数==0)
{
打破
}
计数器++;
如果(计数器>1)
{
差=整数-最后一个整数;
如果(差异>最大差异)
{
最大差异=差异;
}

cout 1)您的缩进样式混乱,使代码读取变得不必要的困难。2)您甚至没有尝试实现“最大差异”的计算。您所做的只是将其分配给最高的
。因此不是代码编写服务。请在您尝试某项操作后返回。要计算“连续输入之间的差异”您需要存储以前的和当前的输入。一旦您有了差异,您可以将其与最大差异进行比较。当您存储每个数字时,您只需计算
数字[i+1]-数字[i]的最大值
。我做了一些事情,但我并不完美,我以为这个网站会帮助我,我是新手,所以下次不要太苛刻,我只是想寻求帮助。谢谢。注意:在极端情况下,
difference=integers-lastInteger;
溢出。当序列不断减少时,例如溢出,此代码会报告错误答案de>4 2 1 0
如“尝试使此方法适用于负面差异”所暗示"@chux我不明白你的意思。对于序列
4 2 1 0
,最大差异应该是-1,此代码可能报告0。进一步检查,此代码确实有UB。第一次调用
difference=integers-lastInteger;
,尚未初始化或分配
lastInteger
值。@chux,我看不出来在问题的任何地方,都要求处理负面差异。