原文题目链接:
http://projecteuler.net/problem=124
翻译题目链接:
http://pe.spiritzhang.com/index.php/2011-05-11-09-44-54/126-124k
通过人数:7687
题目分析:
看起来就是个输入排序检索~很简单~
数据量不大,求rad(n)也不用做优化。
解题过程:
一、初始化数组
for (int i = 0; i < 100000; i++)
{
In[i][0] = i + 1;
In[i][1] = 1;
int t = i + 1;
for (int j = 2; j * j <= t; j++)
{
if (t % j == 0)
{
while(t % j == 0)
t /= j;
In[i][1] *= j;
}
}
In[i][1] *= t;
}
因为数据量不大,就没有做优化。In[100000][2]第二维分别为i和rad(i)
二、排序
qsort(In, 100000, 2 * sizeof(int), comp);
直接调用stdlib.h中的qsort
比较函数:
int comp(const void * a, const void * b)
{
int d1 = ((int *)a)[1] - ((int *)b)[1];
int d2 = ((int *)a)[0] - ((int *)b)[0];
if (d1)
return d1;
else
return d2;
}
完全按照题目中说的排序方法写就好了~
三、结果:
printf("%d\n",In[9999][0]);
直接输出即可,为21417.
分析:虽然没有做优化依然秒算...题水...