網站首頁 編程語言 正文
題目描述:輸入年份和這一年的第幾天,輸出具體的年、月、日的信息。(注意閏年的判斷!)
輸入要求:輸入兩個整數分別代表年份和這一年的第幾天。(假設數據都在有效范圍內)
輸出要求:輸出對應的年、月、日。輸出的數字之間以一條橫線間隔,輸出完畢換行。
代碼如下:
#include<stdio.h>
//接口定義,year為年,date為天數,*nmonth是計算得出的月,*nday是計算得出的日
void month_day(int year, int date, int* nmonth, int* nday) {
?? ?int yu, hao;
?? ?int wang[2][13] = {
?? ??? ?{0,31,28,31,30,31,30,31,31,30,31,30,31},
?? ??? ?{0,31,29,31,30,31,30,31,31,30,31,30,31},?? ??? ??? ??? ??? ??? ?//定義數組存放閏年和平年的天數
?? ?};
?? ?hao = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;?? ??? ?//閏年判別條件
?? ?for (yu = 1; date > wang[hao][yu]; yu++)
?? ??? ?date -= wang[hao][yu];
?? ?*nmonth = yu;
?? ?*nday = date;
}
int main() {
?? ?int day, month, year, date;?? ??? ??? ??? ??? ??? ??? ??? ?//定義日、月、年和天數的變量
?? ?printf("請輸入年份和第幾天:\n");
?? ?scanf_s("%d%d", &year, &date);
?? ?if (date >= 1 && year >= 1) {
?? ??? ?if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 && date < 367) {
?? ??? ??? ?month_day(year, date, &month, &day);
?? ??? ??? ?printf("具體年、月、日如下:\n%d-%d-%d", year, month, day);
?? ??? ?}
?? ??? ?if (year % 4 != 0 && date < 366) {
?? ??? ??? ?month_day(year, date, &month, &day);
?? ??? ??? ?printf("具體年、月、日如下:\n%d-%d-%d", year, month, day);
?? ??? ?}
?? ?}
?? ?else {
?? ??? ?printf("搞事情是吧?這邊給你強行退出!\n");
?? ?}
?? ?return 0;
}
原文鏈接:https://blog.csdn.net/weixin_67793168/article/details/125919329
相關推薦
- 2022-06-08 FreeRTOS實時操作系統支持時間片示例詳解_操作系統
- 2022-10-07 Rust中的Struct使用示例詳解_相關技巧
- 2022-04-08 iOS實現簡單計算器功能_IOS
- 2022-09-10 Python實現自定義異常堆棧信息的示例代碼_python
- 2023-10-16 forEach方法如何跳出循環和for方法跳出循環
- 2022-07-10 手動實現function isInstanceOf(child,Parent)
- 2023-03-26 C#?cefSharep控件的使用詳情_C#教程
- 2023-01-01 C++?Boost.Signals2信號/槽概念_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支