1.第1题解答
程序是线性分段函数,可用二分法定位对应的分段函数。
2.第4题解答
#include<iostream> #include<string> #include<vector> using namespace std; int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 // 1 2 3 4 5 6 7 8 9 10 11 12 }; class D{ public: int year,mon,day;// 1900 <= year, 1 <= mon <= 12, // 1 <= day <= 31 D(){} D(int y, int m, int d): year(y),mon(m),day(d){} int yearday(void){//返回这一天是这一年的第几天 int sum = day; for( int i = 1; i < mon; i++ ) sum += month[i]; if( isrun() && mon > 2 ) sum ++; return sum; } bool isrun( void ){//是否是闰年 return (year%4==0&&year%100!=0)||(year%400==0); } }; int dist( D d1, D d2 ){//两个日期相差的天数 int sum = -(d1.yearday()); for( ; d1.year < d2.year ; d1.year++ ) sum += d1.isrun()?366:365; return sum + d2.yearday(); } int xingqiji( D d ){//某一天是星期几 D temp(1900,1,1); return dist( temp, d )%7+1; } int print(int year, int mon ){//输出某月日历 D d(year, mon, 1 ); int week = xingqiji(d); int sum = month[ mon ]; for( int i = 1; i < week; i++ ) cout << " "; for( int i = 1; i <= sum; i++){ cout << i << " "; if( week == 7 ){ week = 1; cout << endl; } else week++; } cout << endl; } int main(void){ D a(2015,3,5); cout << xingqiji(a) << endl; print(2015,3); return 0; }
3.第8题解答
(待补充...)