日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

用C語言根據天數輸出對應的年、月、日

作者:Sunday王-YH 更新時間: 2022-07-22 編程語言

題目描述:輸入年份和這一年的第幾天,輸出具體的年、月、日的信息。(注意閏年的判斷!)

輸入要求:輸入兩個整數分別代表年份和這一年的第幾天。(假設數據都在有效范圍內)

輸出要求:輸出對應的年、月、日。輸出的數字之間以一條橫線間隔,輸出完畢換行。

代碼如下:

#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

欄目分類
最近更新