給定了天數,任務是將給定的天數轉換為年、週和天。
讓我們假設一年中的天數=365
年數=(天數)/365
解釋-:年數將是除以給定天數得到的商與365
週數= (天數% 365) / 7
解釋-:週數將透過收集餘數獲得將天數除以365 ,再除以一週的天數7。
天數= (天數% 365) % 7
說明-:天數是用天數除以365所得的餘數再除以一週的天數7所得的餘數。
Input-:days = 209 Output-: years = 0 weeks = 29 days = 6 Input-: days = 1000 Output-: years = 2 weeks = 38 days = 4
Start Step 1-> declare macro for number of days as const int n=7 Step 2-> Declare function to convert number of days in terms of Years, Weeks and Days void find(int total_days) declare variables as int year, weeks, days Set year = total_days / 365 Set weeks = (total_days % 365) / n Set days = (total_days % 365) % n Print year, weeks and days Step 3-> in main() Declare int Total_days = 209 Call find(Total_days) Stop
現場示範
#include <stdio.h> const int n=7 ; //find year, week, days void find(int total_days) { int year, weeks, days; // assuming its not a leap year year = total_days / 365; weeks = (total_days % 365) / n; days = (total_days % 365) % n; printf("years = %d",year); printf("</p><p>weeks = %d", weeks); printf("</p><p>days = %d ",days); } int main() { int Total_days = 209; find(Total_days); return 0; }
如果我們執行上述程式碼,它將產生以下輸出
years = 0 weeks = 29 days = 6
以上是寫一個C程序,將給定的天數轉換為年、週和天的詳細內容。更多資訊請關注PHP中文網其他相關文章!