Last active
October 17, 2019 15:57
-
-
Save orange-in-space/b3ba7c5161f2f0089b922b57ddb9be51 to your computer and use it in GitHub Desktop.
閏年を論理演算じゃなく判定するやつ!><
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// | |
// 自信ない><; | |
// | |
namespace uruudoshi | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
System.Console.WriteLine("年を入力してEnterを押すと閏年かわかるよ!!!><"); | |
while (true) | |
{ | |
TestOne(); | |
} | |
} | |
private static void TestOne() | |
{ | |
string str = System.Console.ReadLine(); | |
int year = 0; | |
if (int.TryParse(str, out year)) | |
{ | |
int result = Uruudoshi(year); | |
System.Console.WriteLine($""); | |
System.Console.WriteLine($"{year}年は"); | |
if (result != 0) | |
{ | |
System.Console.WriteLine("閏年!!!><"); | |
} | |
else | |
{ | |
System.Console.WriteLine("ふつうの年"); | |
} | |
System.Console.WriteLine("result: " + result); | |
} | |
else | |
{ | |
System.Console.WriteLine("整数を入力してね!><"); | |
} | |
} | |
/// <summary> | |
/// 閏年の時はゼロ以外、閏年じゃない時にはゼロを返すやつ>< | |
/// </summary> | |
/// <param name="year"></param> | |
/// <returns>0 is common year, not 0 is leap year</returns> | |
public static int Uruudoshi(int year) | |
{ | |
return (((year + 1) % 4) * ((year + 2) % 4) * ((year + 3) % 4) * (year % 100)) | |
+ (((year + 1) % 4) * ((year + 2) % 4) * ((year + 3) % 4)) * | |
((year - (year % 100)) * | |
((((year - (year % 100)) + 100) / 100) % 4) * | |
((((year - (year % 100)) + 200) / 100) % 4) * | |
((((year - (year % 100)) + 300) / 100) % 4)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment