fn leap_year(year: u32) -> bool { if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 { return true; } false } fn day_of_week(year: usize, month: usize) -> usize { let mut days_of_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; if leap_year(year.try_into().unwrap()) { days_of_month[2] = 29; } let day = 1; let mut days = 0; for y in 1..year { if leap_year(y.try_into().unwrap()) { days += 366 } else { days += 365 } } for m in 1..month { days += days_of_month[m as usize] } days += day; return days % 7 } fn main() { let days_of_week_name = ["日".to_string(), "月".to_string(),"火".to_string(),"水".to_string(),"木".to_string(),"金".to_string(),"土".to_string()]; let mut days_of_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; let year = 2040; let month = 3; if leap_year(year.try_into().unwrap()) { days_of_month[2] = 29; } let first_day = day_of_week(year, month); println!("{}年 {}月", year, month); println!("日 月 火 水 木 金 土"); let emp = " ".to_string(); print!("{}", emp.repeat(first_day*3)); for day in 1..(1 + days_of_month[month]) { print!("{:<2} ", day); if(day + first_day - 1) % 7 == 6 { print!("\n"); } } print!("\n"); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/basic`
2040年 3月
日 月 火 水 木 金 土
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
これは中々面白い!!! そんなに難しくないけど、中々思いつかない!