实在没想到什么提高效率的办法,写了简单的暴力函数,居然AC了。后来找了找别人家的代码,才发现原来可以用预先处理数组和二分查找这样的特殊解法。
// Problems.cpp : 定义控制台应用程序的入口点。
//
#define DEBUG
#include <iostream>
#include <string>
#include <sstream>
//#include "poj.h"
#ifdef DEBUG
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
#endif
using namespace std;
int main()
{
#ifdef DEBUG
Stopwatch ^ stopWatch;
stopWatch = Stopwatch::StartNew();
#endif
string cards = " card(s)";
float len;
int n = 0,index = 1;
float sum = 0;
while(cin>>len, len != 0)
{
n = 0,index = 1;sum=0;
while(true)
{
n++;
index++;
sum += 1.0f / index;
if(len <= sum){
cout<<n<<cards<<endl; break;
}
}
}
#ifdef DEBUG
stopWatch->Stop();
Int64 ticksThisTime = stopWatch->ElapsedMilliseconds;
cout<<"time:"<<ticksThisTime<<endl;
system("PAUSE");
#endif
return 0;
}