请问为什么零分呢?do while 用的不对么

P1035 [NOIP2002 普及组] 级数求和

shirley97 @ 2016-08-16 21:56:17

#include<iostream>
using namespace std;
int main()
{ 
  double s,k;
  int n;
  s=0; n=1; 
  cin>>k;
  do {s=s+1/n; n++; } while (s>k);
  cout<<n<<endl;
 return 0;    
}

by shirley97 @ 2016-08-16 22:03:54

#include<iostream>
using namespace std;
int main()
{ 
  double s,k;
  int n;
  s=0; n=0; 
  cin>>k;
  while (s<=k)
  { n++; s+=1.0/n;}      //1.0为什么就可以,1就不可以???
  cout<<n<<endl;
 return 0;    
}

by 太阳之神2015 @ 2016-08-17 11:05:47

这里n是整型,整型除以整型‘/’号当做div


by 太阳之神2015 @ 2016-08-17 11:07:40

c++中除号两边有一个数是实数结果就为实数


by 太阳之神2015 @ 2016-08-17 11:08:36

@shirley97


by 韩小布2007 @ 2016-10-16 15:59:47

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    int k;
    double s=0;
    cin>>k;
    int i=0;
    while(s<=k)
    {
        i++;
        s+=1.0/i;
    }
    cout<<i<<endl;
    return 0;
}

|