C++ C++;带有运行时错误的代码
当我在ideone上运行代码时,我不知道问题出在哪里,因为它给了我运行时错误,这是一个UVa问题(UVA11321) 我有N个数字和一个正整数M。我必须对N个数字进行排序 按其模M值的升序排列C++ C++;带有运行时错误的代码,c++,debugging,runtime,runtime-error,C++,Debugging,Runtime,Runtime Error,当我在ideone上运行代码时,我不知道问题出在哪里,因为它给了我运行时错误,这是一个UVa问题(UVA11321) 我有N个数字和一个正整数M。我必须对N个数字进行排序 按其模M值的升序排列 如果奇数和偶数之间存在平局(即 它们的模M值相同)则奇数将位于 偶数 如果两个奇数(即它们的模M)之间有一个平局 值相同)则较大的奇数将先于较小的奇数 奇数和 如果两个偶数(即它们的模M值)之间存在平局 相同)则较小的偶数将先于较大的偶数 号码 对于负数的余数值,遵循C规则 编程语言:负数永远不能有模 大
#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;
}
#包括
#包括
将代码分解为更小的、清晰易懂的部分是很有帮助的
下面是我试图重新编写你的函数的尝试,其中的注释对我来说是有意义的
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);
}
bool-sortPair(常量对&x,
常量对&y)
{
int xmod=x.first;
int xval=x秒;
int-ymod=y.首先;
int-yval=y.秒;
//简单的情况是,mod不相等。
如果(xmod!=ymod)
{
返回xmod
另外,修改创建向量元素的代码,以便在调用对其进行排序之前处理负值的mod
int mod = x%m;
if ( x < 0 )
{
mod -= m;
}
u.push_back( make_pair ( mod, x ) ) ;
int mod=x%m;
if(x<0)
{
mod-=m;
}
u、 向后推(形成一对(模,x));
@kuroineko谢谢你,我会尝试重写它,但你现在能帮我解决运行时错误吗?提前感谢。建议:启动调试器并启动a-steppin'。这是关于“<代码>(真)的意思。我的建议是:忘记STD:C++和调试器。相反,想想你的算法,用一支笔和一张纸。让一个程序编译或运行时没有错误并不意味着它将是一个好程序,也不能保证它将产生预期的结果。我知道这是一个无限循环,但法官接受它,即使我对代码设置了限制条件,它仍然会给我运行时错误@user4581301Hint:“按其模M值的升序”!=false
。非常感谢您的代码让我学到了很多,它使代码变得更简单,我测试了代码,但在您编辑后,它仍然会出现运行时错误@r-sahu