注意:每次需要更新s值。
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main(){ double c; int i=0; while(cin>>c){ double s=0.0; if(c==0) break; for(i=2;;i++){ s+=(1.0/i); if(s>=c){ cout<<i-1<<" card(s)"<<endl; break; } } } return 0; }