浮点型数据的大小比较
浮点型的大小比较:判断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 编辑 ]