时间换算问题
输出计算时不对,这怎么搞啊?#include <stdio.h>
int main()
{
int i,h,m,s,n_s,t,repeat;
int nh,nm,ns;
int h_1,m_1,s_1;
scanf("%d",&repeat);
for(i=1;i<=repeat;i++)
{
for(i=1;i<=repeat;i++)
{
scanf("%d:%d:%d\n",&h,&m,&s);
scanf("%d",&n_s);
}
for(i=1;i<=repeat;i++)
{
if(n_s<=60)
{
ns+=n_s;
if(ns>=60)
{
nm=m+1;
ns=s-60;
if(nm>=60)
{
nh=h+1;
}
}
}
if(n_s>=60)
{
nm=m+n_s/60;
ns=s+n_s%60;
if(m>=60)
{
nh=h+1;
}
}
printf("time: %d:%d:%d\n",nh,nm,ns);
}
}
return 0;
} #include <stdio.h>
typedef struct {
int h, m, s;
}Time;
Time add(Time T, int n) {
Time res = T;
int over;
res.s += n;
over = res.s / 60;
res.s %= 60;
res.m += over;
over = res.m / 60;
res.m %= 60;
res.h += over;
over = res.h / 24;
res.h %= 24;
return res;
}
int main() {
Time A, B;
int repeat, n;
scanf("%d", &repeat);
for(int i = 0; i < repeat; i++) {
scanf("%d:%d:%d", &A.h, &A.m, &A.s);
scanf("%d", &n);
B = add(A, n);
printf("time: %d:%d:%d\n", B.h, B.m, B.s);
}
}输入3
0:0:1
59
11:59:40
30
23:59:40
301输出time: 0:1:0
time: 12:0:10
time: 0:4:41 #include <stdio.h>
#include <time.h>
int main(void) {
int repeat; scanf("%d", &repeat);
while(repeat--) {
int sec, min, hour;
scanf("%d:%d:%d", &hour, &min, &sec);
int n; scanf("%d", &n);
time_t time = sec + (time_t)min * 60 + (time_t)hour * 60 * 60;
time += n;
struct tm tm;
localtime_r(&time, &tm);
printf("time: %02d:%02d:%02d\n", tm.tm_hour - 8, tm.tm_min, tm.tm_sec);
}
return 0;
}
页:
[1]