| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5443 人关注过本帖
标题:浮点型数据的大小比较
只看楼主 加入收藏
空白先生
Rank: 2
等 级:论坛游民
帖 子:115
专家分:95
注 册:2012-12-9
结帖率:92.86%
收藏
已结贴  问题点数:40 回复次数:7 
浮点型数据的大小比较
浮点型的大小比较:判断a和b的大小的语句可以写成if(fabs(a-b)<1e-5),可是在循环里判断为什么我老是出现死循环,
比如while(fabs(x-a)<1e-5),其实俺想问的是下面这道题目啦
问题描述:The three hands of the clock are rotating every second and meeting each other many times everyday. Finally, they get bored of this and each of them would like to stay away from the other two. A hand is happy if it is at least D degrees from any of the rest. You are to calculate how much time in a day that all the hands are happy.
输入要求:The input contains many test cases. Each of them has a single line with a real number D between 0 and 120, inclusively. The input is terminated with a D of -1.输出要求:For each D, print in a single line the percentage of time in a day that all of the hands are happy, accurate up to 3 decimal places.
Sample Input
0 120 90 -1
Sample Output
100.000 0.000 6.251
来自杭电的基础题,我写的东西如下:
#include<stdio.h>
#include<math.h>
float Degree(float A,float B)
{
    float C;
    C=fabs(A-B);
    if(C-180>1e-5)
        C=360-C;
    return C;
}//判断各种针之间的角度
int main()
{
    float time;
    int i,j;
    float D[100]={0};
    float second,minute,hour;
    i=0;
    scanf("%f",&D[i]);
    while(D[i]+1>1e-5)
    {
        i++;
        scanf("%f",&D[i]);
    }
    j=i;
    i=0;
    while(i<j)
    {
        time=hour=minute=hour=0;
       while(360-hour>1e-5)
        {
           second=second+6;
           minute=minute+6/60;
           hour=hour+6/(60*15);
           if(Degree(second,minute)-D[i]>1e-5&&Degree(second,hour)-D[i]>1e-5&&Degree(minute,hour)-D[i]>1e-5)
               time++;
           if(360-second<1e-5)
               second=0;
           if(360-minute<1e-5)
               minute=0;
        }
        printf("%.3f ",time/(12*60*60));
        i++;
    }
    return 0;
}
死循环,坨坨的,感觉是红色部分出问题了,可是不知道怎么改,求大神帮助,狂送40分

[ 本帖最后由 空白先生 于 2013-5-20 20:02 编辑 ]
搜索更多相关主题的帖子: second happy least second happy least 
2013-05-20 19:21
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
收藏
得分:0 
hour能到360吗?
2013-05-20 19:33
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
收藏
得分:0 
int main()
{
    float time;
    int i,j;
    float D[100]={0};
    float second,minute,hour;
    i=0;
    scanf("%f",&D[i]);
    while(D[i]!=-1)<--这个判断就是死循环了!!!
    {
        i++;
        scanf("%d",&D[i]);<---错了!!!
    }
    j=i;
    i=0;


[ 本帖最后由 YJ_Hao 于 2013-5-20 19:58 编辑 ]
2013-05-20 19:52
空白先生
Rank: 2
等 级:论坛游民
帖 子:115
专家分:95
注 册:2012-12-9
收藏
得分:0 
回复 2楼 YJ_Hao
能呀,360度

printf("My goal is to be  good at computer")
2013-05-20 20:00
空白先生
Rank: 2
等 级:论坛游民
帖 子:115
专家分:95
注 册:2012-12-9
收藏
得分:0 
回复 4楼 空白先生
不好意思不好意思,改了这个以后还是死循环

printf("My goal is to be  good at computer")
2013-05-20 20:01
空白先生
Rank: 2
等 级:论坛游民
帖 子:115
专家分:95
注 册:2012-12-9
收藏
得分:0 
回复 3楼 YJ_Hao
仁兄在看看撒

printf("My goal is to be  good at computer")
2013-05-20 20:03
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
收藏
得分:40 
你这样写hour永远等于0的,因为6/(60*15)这个在C中只能等于0,要改成6.0/(60*15)

printf("%.3f ",time/(12*60*60));这个也是0.000,汗!!!


[ 本帖最后由 YJ_Hao 于 2013-5-20 20:59 编辑 ]
2013-05-20 20:54
空白先生
Rank: 2
等 级:论坛游民
帖 子:115
专家分:95
注 册:2012-12-9
收藏
得分:0 
回复 7楼 YJ_Hao
soga。。。。。。

printf("My goal is to be  good at computer")
2013-05-20 21:01
快速回复:浮点型数据的大小比较
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015287 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved