超简单问题啊!赚分了!

ar7043 2007-04-16 08:20:25
#include "iostream.h"
void main()
{
float temp=0;
float c;
int n=1;
float t;
while(c!=temp)
{
cout<<"please input number"<<endl;
cin>>c;
if(c<0.01||c>5.20||c==0)
{
cout<<"ERRor"<<endl;
break;
}
else if(0.01<c&&c<5.20)
{
while(c!=(int)(temp))
{
* temp=temp+1/(n+1);
* t=1/(n+1);
* n++;
}
}
}
cout<<n;
}
t=1/(n+1)为什么总是零啊。怎么才能让除下来不是零啊?
...全文
252 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lidongri 2007-04-17
  • 打赏
  • 举报
回复
问题多多
wltg2001 2007-04-17
  • 打赏
  • 举报
回复
看一下你的代码,发现很多不理解的地方
#include "iostream.h"
void main()
{
float temp=0;
float c;
int n=1;
float t;
while(c!=temp) //c没有初始化,能用吗?
{
cout<<"please input number"<<endl;
cin>>c;
if(c<0.01||c>5.20||c==0)
{
cout<<"ERRor"<<endl;
break;
}
else if(0.01<c&&c<5.20)
{
while(c!=(int)(temp)) c是float型的,temp也是float型的,干吗要把它转成int呢?
{
*temp=temp+1/(n+1);
*t=1/(n+1);
*n++; 这个循环一定能出去吗?1/(n+1)是int型的,它的值始终比1小,舍出后永远是0
}
}
}
cout<<n;
}
dmt9697 2007-04-17
  • 打赏
  • 举报
回复
#include "iostream.h"
void main()
{
float temp=0;
float c;
int n=1;
float t;
while(c!=temp)
{
cout<<"please input number"<<endl;
cin>>c;
if(c<0.01||c>5.20)
{
cout<<"ERRor"<<endl;
break;
}
else if(0.01<=c&&c<=5.20)
{
while((int)c!=(int)(temp))
{
temp=temp+float(1) /float(n+1);
(int)n++;
}
}
}
cout<<n;
}
egghitler 2007-04-16
  • 打赏
  • 举报
回复
t=1.0/(n+1)就可以了。。
不想低调 2007-04-16
  • 打赏
  • 举报
回复
1\(n+1)
1、n+1都是整形,所以1\n也是整形。。
freshui 2007-04-16
  • 打赏
  • 举报
回复
float t
t=float(1) /float(n+1)

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧