带有运行时错误的c++代码
C++ code with Runtime error
当我在ideone上运行代码时,我无法找出问题所在,它给了我运行时错误,这是UVa问题(UVa 11321)。
我有N个数字和一个正整数m,我要对这N个数字排序模M值按升序排列
-
如果奇数和偶数之间存在平局(即它们的模M值是相同的)那么奇数将在偶数。
-
如果两个奇数之间存在联系(即它们的模M)值相同),那么较大的奇数将排在较小的奇数前面
-
如果两个偶数之间存在平局(表示它们的模M值是一样的)那么小的偶数会在大的偶数之前号码。
-
对于负数的余数值遵循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 ) ) ;
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值