題目描述
zjm 有四個數列 a,b,c,d,每個數列都有 n 個數字。zjm 從每個數列中各取出乙個數,他想知道有多少種方案使得 4 個數的和為 0。input當乙個數列中有多個相同的數字的時候,把它們當做不同的數對待。
請你幫幫他吧!
第一行:n(代表數列中數字的個數) (1≤n≤4000)output接下來的 n 行中,第 i 行有四個數字,分別表示數列 a,b,c,d 中的第 i 個數字(數字不超過 2 的 28 次方)
輸出不同組合的個數。sample input
6sample output 解題思路-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
1.我們可以兩個for迴圈計算出a,b兩個陣列的所有和,然後再兩個for迴圈找出c,d兩個陣列的和,然後找a+b的相反數再c+d**現的次數
2.顯然以上方法很複雜過不去,那麼們就要進行乙個二分查詢的優化,我們將a+b的和存在陣列中,然後對這個陣列進行從小到大的排序,這樣,我們再查詢c+d的相反數再上面陣列中左邊出現的第乙個位置和右邊出現的最後乙個位置ans+=右-左+1,okk
#include
#include
#include
using
namespace std;
const
int maxn=
4010
;int a[maxn]
,b[maxn]
,c[maxn]
,d[maxn]
;int n;
int mp[maxn*maxn]
;int
findl
(int x,
int a)
else l=mid+1;
}return ans;
}int
findr
(int x,
int a)
else
if(a[mid]
>x) r=mid-1;
else l=mid+1;
}return ans;
}int
main()
}sort
(mp,mp+mm)
;int temp;
for(
int i=
0;i} cout
}
第四周C 作業
1.完成課本每乙個程式設計題。要求先畫出流程演算法圖或n s圖,然後程式設計實現,有可能的話使用兩種以上方法 課本76頁程式設計題 求自然對數e的近似值 include using namespace std int main cout e int main cout pai 4 paiint ma...
C 第四周作業
第一題 習題5 功能 求自然對數 e的近似值 include include using namespace std int main int i,j double e 1,s 1 for i 1 i 10 i for j 1,j i j s j e 1 s s 1 return 0 習題6 功能 ...
C 第四周作業
一 自然對數e includeusing namespace std int main cout 自然對數為 圓周率 include using namespace std int main cout 圓周率為 定義範圍 include includeusing namespace std int ...