这个题对精度要求很高啊,本来想这个水题求个公式就A了,没想到连公式都不好弄,只好一个一个试,用Java写完之后超时啊!!!用C++改写之后AC。参考了网上各位大神的答案。遇到这种对时间和精度要求都很高的题就要凸显能力了。好好练吧还是。
上代码:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int main() { double high, last; while(cin>>high>>last && high && last) { int free, sumhigh; int N; if (last == 1) { free = (ceil)(log(high) / log(2)); sumhigh = 2 * high - 1; } else { for (N = 1; N < high; N++) { if (abs(log(high)*log(N) - log(last)*log(N + 1)) < 1e-8) break; } free = (last - 1) / (N - 1); sumhigh = (N + 1) * high - last * N; } cout <<free<<" "<<sumhigh<<endl; } }