poj1840
题意:给出ai(i=1~5),求a1 * x1^3+ a2 * x2^3+ a3 * x3^3+ a4 * x4^3+ a5 * x5^3=0在-50到50之间的x的解的个数
分析:用map,二重循环枚举x1,x2计算结果在map中对应位++
三重循环枚举x3,x4,x5计算结看map中对应的相反数的个数。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
usingnamespace std;
int a[6];
map<int, int> h;
int main()
{
//freopen("t.txt", "r", stdin);
for (int i =1; i <6; i++)
scanf("%d", &a[i]);
for (int i =-50; i <=50; i++)
for (int j =-50; j <=50; j++)
{
if (i ==0|| j ==0)
continue;
int temp = i * i * i * a[1] + j * j * j * a[2];
if (h.find(temp) == h.end())
h[temp] =1;
else
h[temp]++;
}
int ans =0;
for (int i =-50; i <=50; i++)
for (int j =-50; j <=50; j++)
for (int k =-50; k <=50; k++)
{
if (i ==0|| j ==0|| k ==0)
continue;
int temp = i * i * i * a[3] + j * j * j * a[4] + k * k * k
* a[5];
if (h.find(0- temp) == h.end())
continue;
ans += h[0- temp];
}
printf("%d\n", ans);
return0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
usingnamespace std;
int a[6];
map<int, int> h;
int main()
{
//freopen("t.txt", "r", stdin);
for (int i =1; i <6; i++)
scanf("%d", &a[i]);
for (int i =-50; i <=50; i++)
for (int j =-50; j <=50; j++)
{
if (i ==0|| j ==0)
continue;
int temp = i * i * i * a[1] + j * j * j * a[2];
if (h.find(temp) == h.end())
h[temp] =1;
else
h[temp]++;
}
int ans =0;
for (int i =-50; i <=50; i++)
for (int j =-50; j <=50; j++)
for (int k =-50; k <=50; k++)
{
if (i ==0|| j ==0|| k ==0)
continue;
int temp = i * i * i * a[3] + j * j * j * a[4] + k * k * k
* a[5];
if (h.find(0- temp) == h.end())
continue;
ans += h[0- temp];
}
printf("%d\n", ans);
return0;
}