這些是我對codeforces問題的回答,我不知道為什么第一個片段給出了錯誤的答案。
不過第二個被接受了。
我想知道判斷測驗用例是否有問題,因為它們似乎給出了相同的輸出。
問題說明如下:
給定 2 個區間的邊界。列印它們交叉點的邊界。
注:邊界是指區間的兩端,即起始編號和結束編號。
輸入:只有一行包含兩個區間 [l1,r1], [l2,r2] 其中 (1≤l1,l2,r1,r2≤109), (l1≤r1,l2≤r2)。
保證l1≤r1且l2≤r2。
輸出:如果這兩個間隔之間有交集,則列印其邊界,否則列印 -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;
}
uj5u.com熱心網友回復:
在第一個程式中,您只檢查一個條件
if(c > b)
{
cout << -1;
}
但是您還需要檢查以下條件
if ( d < a )
{
cout << -1;
}
例如
if(c > b || d < a )
{
cout << -1;
}
else
{
//...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/346310.html
