4.1.1 A - Calendar(简单线性表)(日期查找)(数组应用)
来源:互联网 发布:微商城 淘宝客 编辑:程序博客网 时间:2024/05/16 06:57
Description
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week.
Input
You may assume that the resulting date won’t be after the year 9999.
Output
Sample Input
1730174017501751-1
Sample Output
2004-09-26 Sunday2004-10-06 Wednesday2004-10-16 Saturday2004-10-17 Sunday
这道题做的太坑爹了,竟然每一种情况都得考虑进来,所以出错率特别高,因此这样的方法是不好的,而应该建立一种比较好的范式
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int wee=0,n,year=2000,month=1,day=1,x;
char week[][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"};
bool isleap()
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
return 0;
}
void whichday()
{
while(n>=31)
{
//cout<<;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
n=n-31;
else
if(month==4||month==6||month==9||month==11)
n=n-30;
else
if(month==2&&x)
n=n-29;
else
n=n-28;
month++;
}
//cout<<month<<' '<<x<<' '<<n<<' ';
if(n==30)
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
else
{
if(month==2&&x)
day=2;
else
if(month==2&&!x)
day=3;
else
day=1;
month++;
}
else
if(month==2&&(n==29||n==28))
if((n==29&&x)||(n==28&&!x))
{
day=1;
month++;
}
else
if(!x&&n==29)
{
month++;
day=2;
//cout<<day<<"MMMMMMMMMMMM";
}
else
day=n+1;
else
day=n+1;
//cout<<n<<endl;
}
int main ()
{
cin>>n;
while(n!=-1)
{
year=2000,month=1,day=1;
int wee=(n%7+6)%7;
x=isleap();
while(1)
{
x=isleap();
if(n>=365)
{
if(x)
if(n>=366)
{
n=n-366;
year++;
}
else
break;
else
if(n>=365)
{
n=n-365;
year++;
}
}
else
break;
}
whichday();
cout<<year<<'-';
if(month<10)
cout<<0;
cout<<month<<'-';
if(day<10)
cout<<0;
cout<<day<<' '<<week[wee]<<endl;
cin>>n;
}
return 0;
}
简便,在找的同时查询时哪一天,思路简单!!!!!!
#include <iostream>
#include <string> //string类型
#include<cmath>
#include <iomanip>
#include <cstring> //memset
#include<algorithm> //sort memset
using namespace std;
char week[][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"};
int Year(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 366;
return 365;
}
int Month(int month ,int year)
{
if(month==2)
return Year(year)==366?29:28;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
else
return 30;
}
int main()
{
int n;
int year,month,day,wee;
cin>>n;
while(n>=0)
{
year=2000,month=1,day=1;
wee=(n%7+6)%7;
while(n)
{
if(n>=Year(year))
{
n=n-Year(year);
year++;
}
else
if(n>=Month(month,year))
{
n=n-Month(month,year);
month++;
}
else
{
day=day+n;
n=0;
}
}
cout<<year<<'-'<<(month<10?"0":"")<<month<<'-'<<(day<10?"0":"")<<day<<' '<<week[wee]<<endl;//这样的输出格式必须是双引号!!!!!!
cin>>n;
}
return 0;
}
- 4.1.1 A - Calendar(简单线性表)(日期查找)(数组应用)
- (应用直接存取类线性表4.1.1)POJ 2080 Calendar(日期计算)
- A - Calendar(4.1.1)
- java中Calendar日期对象的一些简单应用
- 线性表的应用1(顺序表的遍历和查找)
- Calendar(日历)--选择日期
- (直接存取类线性表4.1.2)UVA 602 - What Day Is It?(数组的应用---日期系统的转换)
- #368 –在Calendar 控件中指定不可选择日期(Specifying Blackout Dates in a Calendar Control)
- 线性表的查找操作(线性表的顺序查找、二分查找、分块查找)
- 5.2 calendar--通用日期的相关函数(1)
- 大二 第一次数据结构作业 (数组的排序,删除,查找,插入,合并)(线性表)
- 数组应用--查找(课时3)
- java -日期-Calendar 日期,天数,简单处理
- 算法-查找-线性表的查找(顺序查找,二分查找,分块查找)
- 线性表建立、插入、删除、查找应用
- 日期操作类(Date、Calendar)
- java日期处理一(Calendar,Date)
- POJ 1008 Maya Calendar(日期模拟)
- 大话设计の设计原则汇总(一)
- 南北棋牌游戏大不同,我就爱玩南方棋牌
- JS 简易控制台插件
- USB枚举过程详细分析
- aul 学习测试(测)
- 4.1.1 A - Calendar(简单线性表)(日期查找)(数组应用)
- 编译的静态性
- CF#256(Div.2) A. Rewards
- PL/SQL 之 sql语句的编写
- Android_app项目开发步骤总结
- eclipse package,source folder,folder区别及相互转换
- crm查询记录共享给了哪些人
- [c++11] 判断某类是否有某个原型及名称的成员函数
- STM32F10x的LCD(ILI9320)显示