gpt4 book ai didi

复合/while 循环

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:39 26 4
gpt4 key购买 nike

#include <stdio.h>

int main(void)
{
int days, hours, mins;
float a, b, c, total, temp, tempA, tempB;

a = 3.56;
b = 12.50;
c = 9.23;
total = a+b+c;
days = total / 24;

temp = total/24 - days;

hours = temp * 24;

tempA = temp*24 - hours;

mins = tempA*60;

while (hours >= 24)
{
hours= hours-24;
days +=1;
}
while ( mins >= 60)
{
mins=mins-60;
hours +=1;
}
printf("days:%d\n", days);
printf("hours:%d\n", hours);
printf("mins:%d\n", mins);


return 0;
}

我想将十进制小时数转换为实时时间,我可以很好地完成,但如果小时数超过 24 小时并且分钟数超过 60 分钟,我想增加天数小时数。while 循环确实减去并且它确实打印出新值但是小时/天没有得到复合。这是 1 天 1 小时 77 分钟我想让它读 1 天 2 小时 17 分钟但我得到 1 天 1 小时 17 分钟。

最佳答案

使用取模运算符会让你的生活更轻松:它会给出除法的余数。

int total;

/* a=; b=; c=; assignments */

total = a+b+c;
mins = total % 60;
total /= 60;
hours = total % 24;
days = total / 24;

关于复合/while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5137723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com