
让我们假设一年中的天数 =365
年数=(天数)/365
解释-:年数将是除以给定天数得到的商与 365
周数 = (天数 % 365) / 7
解释-:周数将通过收集余数获得将天数除以 365,再除以一周的天数 7。
天数 = (天数 % 365) % 7
说明-:天数是用天数除以365所得的余数再除以一周的天数7得到的余数。
示例
Input-:days = 209Output-: years = 0 weeks = 29 days = 6Input-: days = 1000Output-: years = 2 weeks = 38 days = 4
算法
StartStep 1-> declare macro for number of days as const int n=7Step 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 daysStep 3-> in main() Declare int Total_days = 209 Call find(Total_days)Stop
Example
现场演示
#include const int n=7 ;//find year, week, daysvoid 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 = 0weeks = 29days = 6
以上就是编写一个C程序,将给定的天数转换为年、周和天的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444269.html
微信扫一扫
支付宝扫一扫