文章仅为本人学习笔记,不是最优解,欢迎指出不足
#include <iostream>
using namespace std;
int main()
{
//-----------------------------thirteen中元素为12个月中每个月的13号所在365天中的天数序号,数组的元素下标+1即为所在月份
int sum=13;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int thirteen[12]={13};
for (int i=0;i<12;i++)
{
sum+=month[i];
thirteen[i+1]=sum;
}
//-----------------------------
/*
上述为累加计算thirteen数组
由于只有十二个月,懒得想也可以手动输入:
thirteen[12]= {13,13+31,13+31+28,13+31+28+31,13+31+28+31+30,13+31+28+31+30+31,13+31+28+31+30+31+30,13+31+28+31+30+31+30+31,13+31+28+31+30+31+30+31+31,13+31+28+31+30+31+30+31+31+30,13+31+28+31+30+31+30+31+31+30+31,13+31+28+31+30+31+30+31+31+30+31+30,13+31+28+31+30+31+30+31+31+30+31+30+31};
*/
int w;
cin>>w;
//观察到要分两种情况,w小于6、大于6。之后得到每个周五所在365天中的天数序号,遍历thirteen进行比较,若一样则输出所在月份
if (w<6)
{
for (int i=6-w;i<365;i+=7)//i为每个周五所在的天数序号
{
for (int j=0;j<12;j++)//遍历thirteen进行比较
{
if (i==thirteen[j])
{
cout<<j+1<<endl;//j+1为所在月份
}
}
}
}
if (w>=6)
{
for (int i=13-w;i<365;i+=7)//i有点区别,其他都一样
{
for (int j=0;j<12;j++)
{
if (i==thirteen[j])
{
cout<<j+1<<endl;
}
}
}
}
return 0;
}
输入:
7
输出:
1
10