2018年5月12日 星期六

C++計算星期幾(蔡勒公式)

#include<iostream>
using namespace std;

int main()
{
int n,y,m,d,w,a,b,c;
w = 0;
a = 0;
b = 0;
c = 0;
cin >> n;
for (int i = 0;i < n; i++)
{
w = 0;
a = 0;
b = 0;
c = 0;
cin >> y >> m >> d;
if (m == 1 || m == 2)
{
a = (y - 1) % 100;
c = (y - 1) / 100;
m += 12;
}
else
{
a = y % 100;
c = y / 100;
}
b = a + (a / 4) + (c / 4) - (2 * c) + (26 * (m + 1) / 10) + d - 1;
if (b < 0)
w = (b % 7 + 7) % 7;
else
w = b % 7;
cout << w << endl;
}
return 0;
}

沒有留言:

張貼留言