用C语言将若干秒数转换为分和秒

本文介绍了如何使用C语言编写一个简单的程序,通过定义常量将用户输入的秒数转换为分钟和秒,并循环处理直到用户选择退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//把秒数转换成分和秒,C primer plus例题
#include<stdio.h>
#define SEC_PER_MIN 60//定义常量:一分钟有60秒
int main(void)
{
    int sec, min, left;//秒,分,剩余
    printf("Convert seconds to minutes and seconds!\n");
    printf("Enter the number of seconds (<=0\n to quit);");
    scanf_s("%d", &sec);//读取(输入的)秒数
    while (sec > 0)
    {
        min = sec / SEC_PER_MIN;//秒数除以常量60,赋值给min
        left = sec % SEC_PER_MIN;//剩下的秒数=秒数除以常量60
        printf("%d seconds is %d minutes,%d seconds.\n", sec, min, left);
        printf("Enter next value(<=0 to quit);\n");
        scanf_s("%d", &sec);
    }
    printf("Done!\n");

    return 0;
}

运行结果如图,输入几个数值后的结果。


### C语言实现与秒数相关的功能 在C语言中,涉及秒数的功能开发主要包括以下几个方面:秒数输入输出秒数与其他时间单位之间的转换以及基于秒数时间计算。以下是详细的说明代码示例。 --- #### 1. **秒数的基本操作** ##### 输入并打印秒数 用户可以通过标准输入提供一个整数表示秒数,并将其解为小时、剩余秒数的形式进行输出。 ```c #include <stdio.h> int main() { int sec, h, m, s; printf("请输入秒数: "); scanf("%d", &sec); h = sec / 3600; // 计算小时部 m = (sec % 3600) / 60; // 计算钟部 s = sec % 60; // 计算剩余秒数 printf("转换结果: %d 小时 %d 钟 %d \n", h, m, s); return 0; } ``` 这段程序实现了将秒数转化为易于理解的时间格式[^1]。 --- #### 2. **秒数与具体时间的相互转换** ##### 获取当前时间秒数 通过 `time()` 函可以获取自1970年1月1日以来经过的秒数(即Unix时间戳)。这是许多时间日期计算的基础。 ```c #include <stdio.h> #include <time.h> int get_current_time_in_seconds() { time_t current_time; time(&current_time); // 获取当前时间 return (int)current_time; } int main() { int seconds_since_epoch = get_current_time_in_seconds(); printf("当前时间距离1970年1月1日已经过了%d。\n", seconds_since_epoch); return 0; } ``` 此代码片段展示了如何使用`time()`函来获取当前时间对应的秒数[^4]。 --- #### 3. **基于秒数时间加减运算** 如果需要对某一时刻增加或减少若干秒数,可以借助 `localtime()` `mktime()` 函完成。这些函允许我们方便地处理复杂的日期时间逻辑。 ```c #include <stdio.h> #include <time.h> void add_or_subtract_seconds(int base_seconds, int delta_seconds) { struct tm *time_info; time_t result; time_info = localtime((const time_t *)&base_seconds); // 转换为基础时间结构体 time_info->tm_sec += delta_seconds; // 修改字段 result = mktime(time_info); // 转回时间秒数 printf("调整后的时间:%s", asctime(localtime(&result))); } int main() { int base_seconds = time(NULL); // 当前时间作为基础 add_or_subtract_seconds(base_seconds, 3600); // 加一小时 add_or_subtract_seconds(base_seconds, -3600); // 减一小时 return 0; } ``` 上述例子演示了如何通过对给定时间加上或者减去一定量的来进行未来或过去时间点的推算[^3]。 --- #### 4. **复杂场景下的应用——统计被点燃的秒数** 假设我们需要在一个范围内找出哪些会被至少一个事件触发(比如鞭炮声),则可以用如下方法实现: ```c #include <stdio.h> #define MAX_EVENTS 5 // 定义事件区间组 typedef struct { int start; int end; } EventInterval; int count_ignited_seconds(EventInterval events[], int num_events, int total_duration) { char ignited[total_duration + 1]; // 标记某是否被点燃 memset(ignited, 0, sizeof(char) * (total_duration + 1)); for (int i = 0; i < num_events; ++i) { if (events[i].start >= 0 && events[i].end <= total_duration) { for (int j = events[i].start; j <= events[i].end; ++j) { ignited[j] = 1; // 设置对应位置为已点燃状态 } } } int count = 0; for (int k = 0; k <= total_duration; ++k) { if (ignited[k]) { count++; } } return count; } int main() { EventInterval events[MAX_EVENTS]; int durations[] = {10, 20}; int starts[] = {5, 15}; for (int i = 0; i < MAX_EVENTS; ++i) { events[i].start = starts[i]; events[i].end = durations[i]; } int total_duration = 30; int result = count_ignited_seconds(events, MAX_EVENTS, total_duration); printf("在%d内有%d被至少一个鞭炮点燃。\n", total_duration, result); return 0; } ``` 本段代码解决了多个不连续时间段内的重叠覆盖问题,并准确统计出符合条件的具体秒数[^2]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值