【发布时间】:2010-01-27 14:41:16
【问题描述】:
好吧,在用天真的 STL 集解决了这个problem 之后,我正在阅读论坛条目,在那里我找到了这个条目:
#include <iostream>
#include <cmath>
#define MAX 100
using namespace std;
int main(){
int res=(MAX-1)*(MAX-1);
for(int i=2;i<MAX;i++)
for(int j=i*i;j<=MAX;j=j*i)
res = res-int(MAX*(log(i)/log(j)))+1;
cout<<res<<endl;
return 0;
}
作者的解释:
Maximum will be 99*99. I subtracted occurrences of those numbers which are powers of some lower numbers (2-100): -
For example: -
4^2,4^3,4^4 (i.e. 3 should be subtracted) as they will be duplicates from lower number powers as in 2^4,2^6,2^8
这个程序给出了正确的答案check here,但我无法得到实现的逻辑,确切地说我没有得到如何确定重复项。有人可以帮忙吗?
【问题讨论】:
-
static const int Max = 100;会更好。 C++ 程序员使用#define的常量,tsk tsk。 -
那不是我,我只是引用了作者的话:P
-
这个算法不起作用。尝试 MAX = 8 或 9。对于 8,它应该给出 44,但给出 45。对于 9,它应该给出 54,但给出 56。