|
发表于 2016-11-30 17:57:26
|
显示全部楼层
- /*1、年份能被4整除;
- 2、年份若是100的整数倍的话,需被400整除,否则是平年
- */
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int a[2017];
- int b[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/* 平年月历表 */
- int year1, mouth1, day1; /* 起始日期 */
- int year2, mouth2, day2; /* 结束日期 */
- int count1 = 0; /* 计数器 */
- int count2 = 0;
-
- //建立一个年历表
- for(int i = 1; i <=2016; i++){
- if(i%100 == 0){
- if(i%400 == 0){
- a[i] = 366;
- }
- else{
- a[i] = 365;
- }
- }
- else{
- if(i%4 == 0){
- a[i] = 366;
- }
- else{
- a[i] = 365;
- }
- }
- }
-
- printf("请输入开始日期(xxxx-xx-xx):");
- scanf("%d-%d-%d", &year1, &mouth1, &day1);
-
- if(a[year1] == 366){
- b[2] = 29; /* 根据平年还是闰年修改月历表 */
- }
- else{
- b[2] = 28;
- }
-
- if(day1 > b[mouth1]){
- printf("该日期不存在");
- exit(-1); /* 结束 */
- }
-
- for(int i = 1; i < mouth1; i++){ /* 从开始年份的第一天到开始日期的天数 */
- count1 += b[i];
- }
- count1 += day1;
-
- printf("请输入结束日期(xxxx-xx-xx):");
- scanf("%d-%d-%d", &year2, &mouth2, &day2);
-
- if(a[year2] == 366){
- b[2] = 29;
- }
- else{
- b[2] = 28;
- }
-
- if(day2 > b[mouth2]){
- printf("该日期不存在");
- exit(0);
- }
-
- for(int i = year1; i < year2; i++){ /* 从开始年份的第一天到结束日期的天数 */
- count2 += a[i];
- }
-
- for(int i = 1; i < mouth2; i++){
- count2 += b[i];
- }
- count2 += day2;
-
- printf("中间间隔的天数:%d", count2 - count1);
-
-
- return 0;
- }
复制代码
原来有人4年过一轮生日........ |
|