日期:2014-05-20  浏览次数:20670 次

大家看看我写的这段代码算法上的问题,验证和实际不符啊
Java code
//这个测试类是得到某年某月一号星期几,从1990年一月一号开始算起的(1990年一月一号星期一)

import java.util.GregorianCalendar;

public class TestgetFirstDayOfMonth{
    public static void main(String[] args)
    {
        int m=getFirstDay(2012,2);  //这个是测试
        System.out.println("m="+m);
    }
    public static  int getFirstDay(int year,int month)
    {
        int totalDay=0,totalYearDay=0,totalMonthDay=0,yearDay=0,monthDay=0;
    
        if(year<1900)
        {
            return(-1);
            
        }
        if((month<1)||(month>12))
        {
            return(-1);
        }
       
            if (year % 4 == 0 && !(year % 100 == 0) || year % 400 == 0)   //判断是不是闰年,闰年加366,平年加365
            {
                yearDay=366;
            }
            else
            {
                yearDay=365;
            }
            for(int i=1900;i<year;i++)
                
            {
                totalYearDay = totalYearDay + yearDay;
            }
        
        for (int i = 1; i <= month; i++) {
            switch (i) {
              case 1:
              case 3:
              case 5:
              case 7:
              case 8:
              case 10:
              case 12:
                monthDay = 31;
                break;
              case 2:
                if (year % 4 == 0 && !(year % 100 == 0) || year % 400 == 0) {
                    monthDay = 29;
                } else {
                    monthDay = 28;
                }
                break;
              default:
                monthDay = 30;
                break;
            }
            
            if (i < month) {
                totalMonthDay = totalMonthDay + monthDay;
            }
        
        
    }
        for(int i=1;i<month;i++)
                
            {
                totalMonthDay = totalMonthDay + monthDay;
            }
        
        totalDay=totalYearDay+totalMonthDay;
        return((totalDay+1)%7);
    }
}
        



------解决方案--------------------
Java code

import java.util.GregorianCalendar;

public class TestgetFirstDayOfMonth{
    public static void main(String[] args)
    {
        int m=getFirstDay(2012,2);  //这个是测试
        System.out.println("m="+m);
    }
    public static  int getFirstDay(int year,int month)
    {
        int totalDay=0,totalYearDay=0,totalMonthDay=0,yearDay=0,monthDay=0;
    
        if(year<1900)
        {
            return(-1);
            
        }
        if((month<1)||(month>12))
        {
            return(-1);
        }
       
        for(int i=1900;i<year;i++)            
        {
            //判断闰年的代码应该放在这儿,而且应该是用i来判断的
            if (i % 4 == 0 && !(i % 100 == 0) || i % 400 == 0)   //判断是不是闰年,闰年加366,平年加365
            {
                yearDay=366;
            }
            else
            {
                yearDay=365;
            }
            totalYearDay = totalYearDay + yearDay;
        }
        
        for (int i = 1; i < month; i++) {    //我把这里的等号去掉了
            switch (i) {
              case 1:
              case 3:
              case 5:
              case 7:
              case 8:
              case 10:
              case 12:
                monthDay = 31;
                break;
              case 2:
                if (year % 4 == 0 && !(year % 100 == 0) || year % 400 == 0) {
                    monthDay = 29;
                } else {
                    monthDay = 28;
                }
                break;
              default:
                monthDay = 30;
                break;
            }
            
            //if (i < month) {        //这个判断多此一举
            totalMonthDay = totalMonthDay + monthDay;
            //}        
        }
       /* for(int i=1;i<month;i++)    //这些都不需要了
                
            {
                totalMonthDay = totalMonthDay + monthDay;
            }*/
        
        totalDay=totalYearDay+totalMonthDay;
        return((totalDay+1)%7);
    }
}