luoxinran @ 2017-01-30 20:41:22
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
double n,sn=0,a;
int k;
cin>>k;
for(n=1;n<=15;n++)
{ a=1/n;
sn=sn+a;
if(sn>k&&k>=0&&k<=15)
{ cout<<n<<endl;
break;
}
}
return 0;
}
by kongksora @ 2017-01-30 20:48:21
楼主的代码风格好那什么……
by kongksora @ 2017-01-30 20:48:57
主函数main不要带参数
by kongksora @ 2017-01-30 20:51:07
还有,n并不是从1到15,这样不行的,你得while(1),然后判断是否找到解,如果是就break退出,否则继续,还有,if里判断k的范围的去掉,题目给出范围了,而且不用判错,所以去掉,加上也没效果
by kongksora @ 2017-01-30 20:52:57
不用while(1),用不带条件的for也可以,反正差不多
#include<iostream>
using namespace std;
main()
{
double a=1,h=1;
int k;
cin>>k;
for(int i=2;;i++)
{
a=a+h/i;
if(a>k){cout<<i;return 0;}
}
}
by 清风之上 @ 2017-02-08 13:32:43
#include<iostream>
using namespace std;
int main()
{
float Sn=1,k,n;
cout<<"please input a number between 1 and 15"<<endl;
cin>>k;
for(n=2;;n++)
{
Sn=Sn+1.0/n;
if(Sn>k)
{
cout<<n<<endl;
break;
}
else
continue;
}
return 0;
}
too many or too little line ,怎么解决