这两个c++代码片段有什么区别
What is the difference between these two snippets of c++ code
这些是我对 codeforces 问题的回答,我不知道为什么第一个片段给出了错误的答案。
虽然第二个被接受了。
我想知道判断测试用例是否有问题,因为它们似乎给出相同的输出。
问题如下:
Given the boundaries of 2 intervals. Print the boundaries of their intersection.
Note: Boundaries mean the two ends of an interval which are the starting number and the ending number.
Input:
Only one line contains two intervals [l1,r1], [l2,r2] where (1≤l1,l2,r1,r2≤109), (l1≤r1,l2≤r2).
It's guaranteed that l1≤r1 and l2≤r2.
Output:
If there is an intersection between these 2 intervals print its boundaries , otherwise print -1.
片段 1
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d;
int z;
cin >> a >> b >> c >> d;
if(a > b)
{
z = a;
a = b;
b = z;
}
if(c > d)
{
z = c;
c = d;
d = z;
}
if(c > b)
{
cout << -1;
}
else
{
cout << max(a, c) << " " << min(b, d);
}
return 0
}
片段 2
#include <bits/stdc++.h>
using namespace std;
int main()
{
int l1 , r1 , l2 , r2;
cin >> l1 >> r1 >> l2 >> r2;
int _begin = max(l1,l2);
int _end = min(r1,r2);
if (_begin > _end)
cout << -1;
else
cout << begin << " " << end;
return 0;
}
在第一个程序中你只检查一个条件
if(c > b)
{
cout << -1;
}
但您还需要检查以下条件
if ( d < a )
{
cout << -1;
}
例如
if(c > b || d < a )
{
cout << -1;
}
else
{
//...
}
这些是我对 codeforces 问题的回答,我不知道为什么第一个片段给出了错误的答案。
虽然第二个被接受了。
我想知道判断测试用例是否有问题,因为它们似乎给出相同的输出。
问题如下:
Given the boundaries of 2 intervals. Print the boundaries of their intersection.
Note: Boundaries mean the two ends of an interval which are the starting number and the ending number.
Input: Only one line contains two intervals [l1,r1], [l2,r2] where (1≤l1,l2,r1,r2≤109), (l1≤r1,l2≤r2).
It's guaranteed that l1≤r1 and l2≤r2.
Output: If there is an intersection between these 2 intervals print its boundaries , otherwise print -1.
片段 1
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d;
int z;
cin >> a >> b >> c >> d;
if(a > b)
{
z = a;
a = b;
b = z;
}
if(c > d)
{
z = c;
c = d;
d = z;
}
if(c > b)
{
cout << -1;
}
else
{
cout << max(a, c) << " " << min(b, d);
}
return 0
}
片段 2
#include <bits/stdc++.h>
using namespace std;
int main()
{
int l1 , r1 , l2 , r2;
cin >> l1 >> r1 >> l2 >> r2;
int _begin = max(l1,l2);
int _end = min(r1,r2);
if (_begin > _end)
cout << -1;
else
cout << begin << " " << end;
return 0;
}
在第一个程序中你只检查一个条件
if(c > b)
{
cout << -1;
}
但您还需要检查以下条件
if ( d < a )
{
cout << -1;
}
例如
if(c > b || d < a )
{
cout << -1;
}
else
{
//...
}