Description

A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years and centuries. The terms of hour, day, month, year and century are all units of time measurements of a calender system.

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

The input consists of lines each containing a positive integer, which is the number of days that have elapsed since January 1, 2000 A.D. The last line contains an integer −1, which should not be processed.

You may assume that the resulting date won’t be after the year 9999.

Output

For each test case, output one line containing the date and the day of the week in the format of "YYYY-MM-DD DayOfWeek", where "DayOfWeek" must be one of "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".

Sample Input

1730
1740
1750
1751
-1

Sample Output

2004-09-26 Sunday
2004-10-06 Wednesday
2004-10-16 Saturday
2004-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(简单线性表)(日期查找)(数组应用)的更多相关文章

  1. 线性表(存储结构数组)--Java 实现

    /*线性表的数组实现 *特点:插入删除慢需要平均移动一半的数据,查找较快 *注意:有重复和无重复的数据对应的操作会有些不同 *注意数组一旦创建其大小就固定了 *Java集合长度可变是由于创建新的数组将 ...

  2. "《算法导论》之‘线性表’":基于数组实现的单链表

    对于单链表,我们大多时候会用指针来实现(可参考基于指针实现的单链表).现在我们就来看看怎么用数组来实现单链表. 1. 定义单链表中结点的数据结构 typedef int ElementType; cl ...

  3. 通过Calendar简单解析Date日期,获取年、月、日、星期的数值

    有时候项目中需要用到Date的年.月.日.星期的数值.那么解析方法如下: /**解析日期,获取年月日星期*/ private void parseDateToYearMonthDayWeek(Date ...

  4. 有序线性表(存储结构数组)--Java实现

    /*有序数组:主要是为了提高查找的效率 *查找:无序数组--顺序查找,有序数组--折半查找 *其中插入比无序数组慢 * */ public class MyOrderedArray { private ...

  5. Day05:集合操作——线性表(二) / 查找表 / 文件操作——File(一)

    文件操作:https://www.cnblogs.com/wsnb8/p/11403626.html

  6. 线性表&顺序线性表

    第二章 线性表 参考文献:[数据结构(C语言版)].严蔚敏 本篇章仅为个人学习数据结构的笔记,不做任何用途. 2.1 线性结构的特点 (1). 存在唯一的一个被称为"第一个"的数据 ...

  7. 使用C语言实现线性表

    线性表是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列,序列中的每个数据元素,可以是一个数字,可以是一个字符,也可以是复杂的结构体或对象.例如:1,2,3,4,5是一个线性表,A,B ...

  8. 《数据结构》C++代码 线性表

    线性表,分数组和链表两种(官方名称记得是叫顺序存储和链式存储).代码里天天用,简单写写. 首先是数组,分静态.动态两种,没什么可说的,注意动态的要手动释放内存就好了. 其次是链表,依旧分静态.动态.课 ...

  9. 学习使用C语言实现线性表

    线性表是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列,序列中的每个数据元素,可以是一个数字,可以是一个字符,也可以是复杂的结 构体或对象.例如:1,2,3,4,5是一个线性表,A, ...

随机推荐

  1. Innodb 存储引擎(转)

    第一部分:线程 Innodb是一个多线程的,各个线程负责不同的任务.主要的线程有:Master Thread.IO Thread.Purge Thread.Page Cleaner Thread. 一 ...

  2. [linux]BASH 的基本语法

    最简单的例子 -- Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1     最简单的例子 -- ...

  3. C++日志操作开源函数库之Google-glog

    今天想给我的C++项目找一个开源的日志类,用于记录系统日志,结果浪费了半个下午的时间.从网上搜索相关资料,找到以下几个备选方案: 1.log4cplus 下载地址:http://sourceforge ...

  4. 怎么优化JAVA程序的执行效率和性能?

    现在java程序已经够快的了,不过有时写出了的程序效率就不怎么样,很多细节值得我们注意,比如使用StringBuffer或者StringBuilder来拼接或者操作字符串就比直接使用String效率高 ...

  5. Async 和 Await的性能(.NET4.5新异步编程模型)

    异步编程长时间以来一直都是那些技能高超.喜欢挑战自我的开发人员涉足的领域 — 这些人愿意花费时间,充满热情并拥有心理承受能力,能够在非线性的控制流程中不断地琢磨回调,之后再回调. 随着 Microso ...

  6. 关于iis站点无法读取 服务器共享目录的问题

    也是权限问题,具体解决方法如下: 1. 找到该站点所在的应用程序池>>高级设置>>进程模型>>设置标志账号,如下图所示 2. 设置账号之后,找到该共享目录>& ...

  7. Spring实战1:Spring初探

    主要内容 Spring的使命--简化Java开发 Spring容器 Spring的整体架构 Spring的新发展 现在的Java程序员赶上了好时候.在将近20年的历史中,Java的发展历经沉浮.尽管有 ...

  8. java-多态性

    1 多态性 主要表现在上转型对象 2 强制类型转换 2.1 基本类型的强制类型转换 转换只能在数值间进行.包括整数型.字符型.浮点型.数值类型和布尔类型间不能转换. 2.2 引用类型变量转换成其子类型 ...

  9. 287. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  10. 黄聪:Wordpress、PHP使用POST数据过大导致MySQL server has gone away报错原因分析

    错误原因: 当POST的数据超过 max_allowed_packet 就会报 MySQL server has gone away 的错误. 1.查看当前Mysql的 max_allowed_pac ...