A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 万里天 中级黑马   /  2015-3-2 15:59  /  1132 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

10、 小明从2006年1月1日开始,每三天结识一个美女两天结识一个帅哥,编程实现当输入2006年1月1日之后的任意一天,输出小明那天是结识美女还是帅哥(注意润年问题)(C

语言)
思路分析
         1,3,5,7,8,10 12 月份有31天
         4,6,9,11 月份有 30 天
         2 月份有平年 28天,闰年 29 天
        1.计算 输入的那天,距离2006年1月1日之间有多少天
        2.天数与5进行模运算,如果结果是1或2或3那么那天小明结识的是美女,如果是0或4结识的是帅哥#


#include <studio.h>
void printGorB(int year,int month,int day);//定义方法
int main()
{
        int year;//记录年
        int month;//记录月
        int day;//记录你
        printf("输入年:");
        scanf("%d\n",&year);//输入年
        printf("输入月:");
        scanf("%d\n",&month);//输入月
        printf("输入日:");
        scanf("%d\n",&day);//输入日
        printf("您输入的是:%d年%d月%d日\n",year,month,day);
        printGorB(year,month,day);//调用方法输出结果
        return 0;
}
void printGorB(int year,int month,int day)
{
    int a=year-2006;//判断是否为同一年
    int count=0;//总共天数
    if(a>0)//如果不是同一年
    {
        //判断2006年起之间是否有闰年
        for(int i=2006;i<year;i++)
        {
          if(i%4==0&&i%400==0)
          {
               count+=366;//如果是闰年,那么一年有366天
             }
          else
             count+=365;//如果不是,一年有365天
        }
        //计算最后一年过去了多少天
        //判断这一年是否为闰年,闰年2月有29天
         if(year%4==0&&year%100!=0||year%400==0)//如果是如年
         {
           if(month==1)//如果是1月,count直接加1月过去的天数day
              count+=day;
            if(month==2)//如果是2月,count加上1月的天数再加上2月过去的天数
              count+=31+day;
            if(month<=7&&month>2)如果是3月和7月之间
              count+=(month-1)*30+month/2-1+day;
            if(month>7&&month<=12)如果是8月和12月之间
              count+=(month-1)*31-month/2+day;
            }
    else
        {
            if(month==1)
              count+=day;
            if(month==2)
              count+=31+day;
            if(month<=7&&month>2)
             count+=(month-1)*30+month/2-2+day;//由于平年2月比闰年少一天,所以多减1
            if(month>7&&month<=12)
             count+=(month-1)*31-month/2-1+day;//同样多减去一天
        }
   if(count%5<=3&&count%5>0)//如果总共过去的天数与5模运算结果小于3大于0,即1,2,3那么当天小明结识的定时美女
    {
        printf("gril");
    }
    else//如果不是,小明结识的就是帅哥
        printf("boy");
}

1 个回复

倒序浏览
#include<stdio.h>

int main()
{       
        int year=2006, month=1, day=1, subdays=0;//初始化 年=2006、月=1、day=1、与2006-1-1相隔的天数=0
        int monthLeap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年时的各月份天数
        int monthCommon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//平年时的各月份天数
       
        //输入2006-1-1之后的任意一天
        printf("请输入2006-1-1之后的任意一天:");
        scanf("%d-%d-%d", &year, &month, &day);
       
        //如果不是当前年的情况下
        if(year - 2006 > 0) {
                //for循环遍历经过的年费
                for(int i=2006; i<year; i++) {
                        //闰年的天数为366
                        if((i%4==0&&i%100!=0)||(i%400==0)) {
                                subdays = subdays+366;//相隔天数+闰年的天数
                        }else {
                                subdays = subdays+365;//相隔天数+平年的天数
                        }
                }
        }
       
        //如果不是1月
        if(month - 1 > 0) {
                //循环每个月份
                for(int i=1; i<month; i++) {
                        //判断当前年是否是闰年
                        if((year%4==0&&year%100!=0)||(year%400==0)) {
                                subdays = monthLeap[i];//相隔天数+闰年月份天数
                        }else {
                                subdays = monthCommon[i];//相隔天数+平年月份天数
                        }
                }
        }
       
        //相隔天数+天数
        subdays = subdays + day;
       
        //输出
        if(subdays%3 == 0) {
                printf("认识了一个美女");
        }
        if(subdays%2 == 0){
                printf("认识了一个帅哥");
        }
        return 0;
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马