每天一學習,記錄一下
阿新 • • 發佈:2018-12-08
原題
時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 32768K,其他語言65536K 64bit IO Format: %lld 題目描述 給出n個整數和x,請問這n個整數中是否存在三個數a,b,c使得ax2+bx+c=0,數字可以重複使用。 輸入描述: 第一行兩個整數n,x 第二行n個整數a[i]表示可以用的數 1 <= n <= 1000, -1000 <= a[i], x <= 1000 輸出描述: YES表示可以 NO表示不可以 示例1 輸入
複製 2 1 1 -2 輸出
複製 YES
解法
/*
資料量1000 ,雙重迴圈+hash
*/
程式碼
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n,x;
int a[maxn];
map<int,int> mp;
int main() {
ios::sync_with_stdio(false);
cin >> n >> x;
for(int i=0; i<n; i++)
{
cin >> a[i];
mp[a[i]]++;
}
int b,c,tt;
for(int i=0; i<n; i++)
{
b = a[i];
for(int j=0; j<n; j++)
{
c = a[j];
tt = -(b*x*x+c*x);
if(mp[tt])
{
cout << "YES" <<endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
/*
如果數字不是可以重複使用的呢?
重不重複,判斷abc的所表示的下標即可。
此次map存a[i] 的下標。
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
if(j!=i){
就這樣。
}
}
*/