Skip to content

Instantly share code, notes, and snippets.

@showyou
Created December 31, 2022 06:23
Show Gist options
  • Save showyou/74ec6db8b6b1992b7cb00a39fc95f8fc to your computer and use it in GitHub Desktop.
Save showyou/74ec6db8b6b1992b7cb00a39fc95f8fc to your computer and use it in GitHub Desktop.
曜日を答えるやつ
use String;
use std::io;
fn calc_day_of_weeks(date: i32) -> i32{
let year = date / 10000;
let month = date % 10000 / 100;
let year_fixed = if month < 3 {year - 1} else {year};
let month_fixed = if month < 3 {month + 12} else {month};
let year_bottom = year_fixed % 100;
let day = date % 100;
//println!("year: {}, month: {}, day: {}", year_bottom, month, day);
let month_calc = 26*(month_fixed+1)/10;
// for Zeller's congruence
let result = (day + month_calc + (year_bottom / 4) - 2*(year_fixed/100) + (year_fixed/400)) % 7;
if result < 0 {result + 7} else {result}
}
fn main() {
let mut date = String::new();
println!("いつの曜日を数えたいですか?例 20230101 : ");
io::stdin().read_line(&mut date).expect("Failed to read line.");
let date_num: i32 = date.trim().parse().unwrap();
let day_of_week = calc_day_of_weeks(date_num) as usize;
let string_of_week = ["日", "月", "火", "水", "木", "金", "土"];
println!("曜日は {}曜 です", string_of_week[day_of_week]);
}
@showyou
Copy link
Author

showyou commented Dec 31, 2022

いつの曜日を数えたいですか?例 20230101 :
20230101
曜日は 日曜 です


いつの曜日を数えたいですか?例 20221231:
20221231
曜日は 土曜 です

@showyou
Copy link
Author

showyou commented Dec 31, 2022

  • pythonのinput()相当が地味にめんどい
  • 配列の数値は usize型にしないといけない
  • ツェラーの公式で、1, 2月は年を-1して月に12足すの見逃してた

@showyou
Copy link
Author

showyou commented Dec 31, 2022

所有権絡みのせいで、変数が増えがちになる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment