C语言程序:将给定天数转换为年、周和天
给定一定天数,任务是将这些天数转换为年、周和天。
假设一年有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("
weeks = %d", weeks); printf("
days = %d ",days); } int main() { int Total_days = 209; find(Total_days); return 0; }
输出
如果运行以上代码,将生成以下输出
years = 0 weeks = 29 days = 6
广告