带有运行时错误的c++代码

C++ code with Runtime error

本文关键字:c++ 代码 运行时错误      更新时间:2023-10-16

当我在ideone上运行代码时,我无法找出问题所在,它给了我运行时错误,这是UVa问题(UVa 11321)。

我有N个数字和一个正整数m,我要对这N个数字排序模M值按升序排列

  1. 如果奇数和偶数之间存在平局(即它们的模M值是相同的)那么奇数将在偶数。

  2. 如果两个奇数之间存在联系(即它们的模M)值相同),那么较大的奇数将排在较小的奇数前面

  3. 如果两个偶数之间存在平局(表示它们的模M值是一样的)那么小的偶数会在大的偶数之前号码。

  4. 对于负数的余数值遵循C规则编程语言:负数永远不能有模数大于零。例如-100 MOD 3 = -1, -100 MOD 4 = 0

有任何帮助请!!!!

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
bool sortPair (const pair < int , int > &x , const pair < int , int > &y)
    {
    if ( x.second > 0 && y.second < 0 )
    {
    return x.second < y.second ;
    }
    if ( x.second < 0 && y.second > 0 )
    {
        return y.second < x.second ;
    }
    if ( (x.second % 2 != 0) && (y.second % 2 == 0) && (x.first == y.first))
    {
        return x.second > y.second ;
    }
   if ( (x.second % 2 == 0) && (y.second % 2 != 0) && (x.first == y.first) )
    {
        return y.second > x.second ;
    }

   if ( (x.second % 2 == 0) && (y.second % 2 == 0) && (x.first == y.first) )
    {
        if ( x.second < y.second )
        {
        return y.second > x.second ;
        }
        else
        {
            return x.second < y.second ;
        }
    }
   if ( (x.second % 2 != 0) && (y.second % 2 != 0) && (x.first == y.first) )
    {
        if( x.second < y.second )
        {
            return x.second < y.second ;
        }
        else
        {
            return x.second > y.second ;
        }
    }
    else
    {
        return false;
    }


    }

    int main()
    {

    while ( true )
    {
        int n , m , x ,idx ;
        cin >> n >> m ;
        idx = n + 1 ;
        vector < pair < int , int > > u ;
        while ( cin >> x && idx != 0 )
        {
            u.push_back( make_pair ( x % m , x ) ) ;
            idx-- ;
         }

        sort ( u.begin() , u.end() - 1 ) ;
        sort ( u.begin() , u.end() - 1 , sortPair ) ;

        cout << n << " " << m << endl ;
        for ( int i = 0 ; i < n ; ++i )
        {
            cout << u.at(i).second << "n" ;
        }
        cout << "0 0" << endl ;
    }
    return 0;
    }

ideone链接问题链接

将代码分解成更小、更容易理解的片段是很有帮助的。

这是我重新编写你的函数的尝试,注释对我来说是有意义的。

PS这是未经测试的代码。

bool sortPair (const pair < int , int > &x,
               const pair < int , int > &y)
{
   int xmod = x.first;
   int xval = x.second;
   int ymod = y.first;
   int yval = y.second;
   // The simple case. The mods are not equal.
   if ( xmod != ymod )
   {
      return xmod < ymod;
   }
   // Add the complicated logic when xmod is equal to ymod.
   bool x_is_odd = ((xval%2) != 0);
   bool y_is_odd = ((yval%2) != 0);
   bool x_is_even = !x_is_odd;
   bool y_is_even = !y_is_odd;
   // Check whether one is odd and the other is even.
   // If there is a tie between an odd number and an even number (that
   // their modulo M value is the same) then the odd number will precede
   // the even number.
   if ( x_is_odd && y_is_even )
   {
      return true;
   }
   if ( y_is_odd && x_is_even )
   {
      return false;
   }
   // Check whether both are odd.
   // If there is a tie between two odd numbers (that is their modulo M value
   // is the same) then the larger odd number will precede the smaller odd
   // number and.
   if ( x_is_odd && y_is_odd )
   {
      return (yval < xval);
   }
   // If we come here, then both are even.
   // If there is a tie between two even numbers (that their modulo M value
   // is the same) then the smaller even number will precede the larger
   // even number.
   assert ( x_is_even && y_is_even );
   return (xval < yval);
}

同时,修改创建vector元素的代码,以便在调用对负值进行排序之前处理它们的mod。

     int mod = x%m;
     if ( x < 0 )
     {
        mod -= m;
     }
     u.push_back( make_pair ( mod, x ) ) ;