如何将整数值转换为年

如何将整数值转换为年

本文介绍了如何将整数值转换为年,月和日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #include < stdio.h > #include < conio.h > #define DAYSINYEAR 365 #define DAYSINMONTH 30 #define DAYSINWEEK 7 void main() { int year = 0 ,month = 0 ,week = 0 ,day = 0 ; clrscr(); printf( 输入天数:); scanf( %d,& day); while (day> = DAYSINYEAR) { day = day-DAYSINYEAR; 年++; } while (day> = DAYSINMONTH) { day = day-DAYSINMONTH; month ++; } 而(day> = DAYSINWEEK) { day = day-DAYSINWEEK; 周++; } printf( \ n%d年,%d月,%d周和%d天,年,月,周,日); // return 0; getch(); } 我怎样才能将简单的整数值转换为年,月和日照顾闰年,以及31个月的月份。 就像我已经输入364然后回答将是11个月和30天.. 或如果输入366那么它应该是1年,0个月和1天.. 请帮帮我。我已经有了简单的代码,但它并没有给我正确的答案。 如果我在12个月和4天内输入364就会给出答案,所以它错了,但似乎有很大的价值。所以我需要确切的代码.. 代码如上所示。 Thanx 解决方案 没有直接的解决方案:如果不知道开始日期(包括年份),就不能计算月数 - 因为2月的长度每四年变化一次。 但是......如果你想接受不准确的答案,假设你的代码中有30个月的月份,那么它比你想象的更容易 - years = days / 365 ; 天=天% 365 ; 个月=天/ 30 ; 天=天% 30 ; 由于你在整个过程中使用整数,因此除去任何分数,然后模数就会丢失你计算的那一点! 但请注意:它会在现实世界中产生垃圾结果:例如1年12个月,如果输入729则为4天! #include<stdio.h>#include<conio.h>#define DAYSINYEAR 365#define DAYSINMONTH 30#define DAYSINWEEK 7void main(){ int year=0, month=0, week=0, day=0; clrscr(); printf("Enter the number of days:"); scanf("%d",&day); while(day>=DAYSINYEAR) { day = day-DAYSINYEAR; year++; } while(day>=DAYSINMONTH) { day = day-DAYSINMONTH; month++; } while(day>=DAYSINWEEK) { day = day-DAYSINWEEK; week++; } printf("\n%d year, %d Month, %d Weeks and %d Days", year,month,week,day);// return 0; getch();}how can i convert simple integer value to years, months and days with taking care of leap year, and also 31 day of months.like i have entered 364 then answer will be 11 months and 30 days..or if entered 366 then it should be 1 year, 0 month and 1 day..please help me. i have already the simple code but it is not giving me correct answer.it is giving the answer if i entered 364 the 12 months and 4 days so its wrong but it seems right for big amount of value. so i need the exact code..the code is given above.Thanx 解决方案 There is no "direct" solution: you can't have a "months" count without knowing the start date, including the year - as the length of February changes every four years.But... if you want to accept inaccurate answers which assume a 30 day month as in your code, then it's easier than your think - sort of.years = days / 365;days = days % 365;months = days / 30;days = days % 30;Since you are using integers throughout, the divide throws away any fractions, then the modulus throws away the bit you just calculated!But do be aware: it produces rubbish results in the real world: such as 1 year, 12 months, 4 days if you enter 729! 这篇关于如何将整数值转换为年,月和日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 00:40