c语言计算追赶上问题

追赶问题
不能正确输出

```c
#include
#include
int main()
{
int h=0,m=0,s=0;
double d,v1,v2,x1,x2,t,r,o,p,q;
scanf("%lf%lf%lf",&d,&v1,&v2);
scanf("%lf%lf%lf",&o,&p,&q);
x1=v15/18,x2=v15/18;r=1000d;
if(v1>=v2)
printf("None\n");
else
{
double i=3600;
t=r/(x2/x1);
while(t/i!=0)
{
h++;
i
=(h+1);
}
t=fmod(t,3600);double j=60;
while(t/j!=0)
{
m++;
j*=(m+1);
}
s=fmod(t,60);
s=s+q;
if(s>=60)
{
s++;
s=fmod(s,60);
}
if(m>=60)
{
h++;
m=m%60;
}
while(h>24)
{
h=h-24;
}
printf("%d:%d:%d\n",h,m,s);
}
return 0;
}
输入样例2
16.56
1.05 2.67
12:36:4.5
输出样例2
22:49:24.50

```a.c: In function ‘main’:
a.c:7:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lf%lf%lf",&d,&v1,&v2);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:8:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lf%lf%lf",&o,&p,&q);
^~~~~~~~~~~~~~~~~~~~~~~~~~~


#include<stdio.h>
#include<math.h>

int main()
{
    int h=0,m=0,s=0;
    double d,v1,v2,x1,x2,t,r,o,p,q;
    scanf("%lf%lf%lf",&d,&v1,&v2);
    scanf("%lf%lf%lf",&o,&p,&q);
    x1=v1*5/18,x2=v1*5/18;r=1000*d;
    if(v1>=v2)
        printf("None\n");
    else
    {
        t=r/(x2/x1);
        while(t >= 3600)
        {
            h++;
            t -= 3600;
        }
        while(t >= 60)
        {
            m++;
            t -= 60;
        }
        s = t;
        s += q;
        if(s >= 60)
        {
            m++;
            s -= 60;
        }
        if(m >= 60)
        {
            h++;
            m -= 60;
        }
        while(h > 24)
        {
            h -= 24;
        }
        printf("%d:%d:%d\n", h, m, s);
    }
    return 0;
}

你的代码有几个问题:

  • 你使用了 scanf 函数读取输入,但是没有检查返回值以确保输入已成功读取。如果输入格式不正确,这可能会导致问题。你应该检查 scanf 的返回值,以确保已读取正确数量的值。
  • 变量 x1 和 x2 被赋予相同的值,即 v1 除以 18 的值。在问题的上下文中,这是没有意义的。
  • 变量 t 的值被计算为 r/(x2/x1),但是 x1 和 x2 的值相同,所以这将始终等于 r/1,也就是 r。
  • 接下来的 while 循环使用变量 i 控制循环,但是在循环内部修改了 i,这会导致意想不到的行为。你应该使用一个单独的变量来控制循环。
  • while 循环检查 t/i 是否等于 0,但是这个条件永远不会为真,因为 t 是一个正值,而 i 从 3600 开始。
  • 第二个 while 循环也存在同样的问题,其中使用变量 j 控制循环并在循环内部修改。
  • 变量 s 的值被 q 增加,但是 q 是一个 double,所以这会导致 s 的值被转换为 double。当你尝试将 s 作为整数打印时,这可能会导致问题。
#include<stdio.h>
#include<math.h>

int main()
{
    int h=0,m=0,s=0;
    double d, v1, v2, x1, x2, t, r, o, p, q;

    // Read input and check return value of scanf
    if (scanf("%lf%lf%lf",&d,&v1,&v2) != 3) {
        printf("Error reading input\n");
        return 1;
    }
    if (scanf("%lf%lf%lf",&o,&p,&q) != 3) {
        printf("Error reading input\n");
        return 1;
    }

    x1 = v1 * (d / v1 - d / v2); // Calculate x1
    x2 = v2 * (d / v1 - d / v2); // Calculate x2
    r = 1000;
    t = r / (x2 / x1); // Calculate time

    // Convert time to hours, minutes, and seconds
    while(t >= 3600) {
        h++;
        t -= 3600;
    }
    while(t >= 60) {
        m++;
        t -= 60;
    }
    s = t;

    // Add time passed since last hour
    s += q;
    if (s >= 60) {
        s -= 60;
        m++;
    }
    if (m >= 60) {
        m -= 60;
        h++;
    }
    while(h > 24) {
        h -= 24;
    }

    printf("%d:%d:%d\n",h,m,s);

    return 0;
}