谁能告诉我 将 日期(date) 月/日 转换为 一年中的第几天的公式.要求公式尽量要简单. 不要让程序处理开销太大. 最好不要用到 if() 语句

worldbankc 2008-11-02 07:42:01


一年 365天 ,用下标表示 0 到 364

int day=date%100, moon=date/100; // int date; date 中包含月日,例如 1231
例如 1月1日 int (0101) 就是一年的下标第 0天
例如 1月2日 int (0102) 就是一年的下标第 1天
....
例如 1月31日 int (0103) 就是一年的下标第 30天

.....
例如 12月15日 int (1215) 就是一年的下标第 x天

复杂的我有办法. 我想知道简单的转换公式. 就是将int形式的 日期(月/日)转换为一年的下标第几天. 要求公式尽量要简单.
不要让程序处理开销太大. 最好不要用到 if() 语句.
...全文
343 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldbankc 2008-11-03
  • 打赏
  • 举报
回复
漏了一点. struct tm *tm=localtime(&t);

我要不用使用 if(){} 最好用 能一个公式解决. 不要出现逻辑运算. 就要要简单的数学计算这样才快.
worldbankc 2008-11-03
  • 打赏
  • 举报
回复

// 看看我们程序的行利用率多高. 学着点.

#include<windows.h>
#include<tchar.h>
#include<iostream>
//#include<new>
#include<iomanip>
#include<cmath> //#include<typeinfo> typeid(object)
#include<fstream> //#include<cstdio> sprintf()
#include<vector>
//#include<cwctype>
#include<cwchar>
#include<sstream> // <- 包含 #include<string>
//#include<cstdlib> rand()随机
//#include<cstring> strcpy() strlen()
//#include<ctype>
//#include<map>
#include<algorithm>
#include<ctime> //由于使用了时间函数
using namespace std;

#define Begin_Year 1970
#define Begin_Month 1
#define Begin_Day 1
#define Begin_Week 4
const char Weeks[][8]={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
const int Days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int getDays(int y,int m,int d)//返回y-m-d到begin日期的天数
{
if (y<Begin_Year || m<Begin_Month || d<Begin_Day) return -1;

int countday=0;
for (int i=Begin_Year;i<y;i++){if(i%4==0 && i%100!=0 || i%400==0)countday++; /*闰年*/countday=countday+365;}
for (int j=0,end=m-1;j<end;j++){countday=countday+Days[j]; if(j==1)countday=countday+((y%4==0 && y%100!=0|| y%400==0)?1:0);/*第二月*/}
return countday+d-1;
}

int main()

{ int y,m,d; //cout<<"\n 请输入年月日:"; cin>>y>>m>>d; cout<<"这是"<<Weeks[(getDays(y,m,d)+Begin_Week-1)%7]<<endl;

int i,t,idate,iw,igetdays,iweek,s_num=24*3600;
int error_week=0,error_day=0;
char str1[30];i=t=0;

do{ tm=localtime(&t); strftime(str1,29,"%Y%m%d%w",tm); idate=atoi(str1); //读出系统时间
iw=idate%10; idate=idate/10; if(iw==0)iw=7;//这里 iw==0 为星期日 星期一到六 用 1 到 6表示
y=idate/10000; m=idate%10000/100; d=idate%100; iw=iw-1;
igetdays=getDays(y,m,d); iweek=(igetdays+Begin_Week-1)%7;

if(i!=igetdays){error_day++; cout<<"\n 日错误 "<<y<<setw(2)<<m<<setw(2)<<d<<" getdays="<<igetdays<<" i="<<i;}
if(iw!=iweek){error_week++; cout<<" 星期几错误 "<<y<<setw(2)<<m<<setw(2)<<d<<" iweek="<<iweek<<" iw="<<iw;}
if(error_day>=10 || error_week>=10)break;
i++; t=i*s_num;//增一天的秒数

}while(idate<20081201);

if(error_week==0 && error_day==0)cout<<"\n 经过了 "<<i<<" 天的检查发现全对. 他hqin6(独行)的程序至少没错. 但是效率太低,如果要一次要检查上千万上亿个时间,就开销太多了.我原本要不要使用 if(){}的 ";

return 0; // 看看我们程序的行利用率多高. 学着点.
}
worldbankc 2008-11-03
  • 打赏
  • 举报
回复
你的行利用率太低了. 看看我改过后的.
#include <iostream>
#include <string>
using namespace std;

#define Begin_Year 1970
#define Begin_Month 1
#define Begin_Day 1
#define Begin_Week 4

const string weeks[]={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int getDays(int y,int m,int d)//返回y-m-d到begin日期的天数
{
if (y<Begin_Year || m<Begin_Month || d<Begin_Day) return -1;

int countday=0;
for (int i=Begin_Year;i<y;i++)
{
if (i%4==0 && i%100!=0 || i%400==0)countday++; //闰年
countday=countday+365;
}
for (int j=0,end=m-1;j<end;j++)
{
countday=countday+days[j];
if(j==1)countday=countday+((y%4==0 && y%100!=0|| y%400==0)?1:0); //第二月
}
return countday+d-1;
}
void main()
{
int y,m,d; cout<<"请输入年月日:"; cin>>y>>m>>d;
cout<<"这是"<<weeks[(getDays(y,m,d)+Begin_Week-1)%7]<<endl;
}




//下面我将证明.
太乙 2008-11-02
  • 打赏
  • 举报
回复


#include <iostream>
#include <string>
using namespace std;
#define BEGINYEAR 1970
#define BEGINMONTH 1
#define BEGINDAY 1
#define BEGINWEEK 4
const string weeks[]={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int getDays(int y,int m,int d)//返回y-m-d到begin日期的天数
{
if (y<BEGINYEAR || m<BEGINMONTH || d<BEGINDAY)
{
return -1;
}
int countday=0;
for (int i=BEGINYEAR;i<y;i++)
{
if (i%4==0 && i%100!=0 || i%400==0)//闰年
{
countday+=1;
}
countday+=365;
}
for (int j=0;j<m-1;j++)
{
countday+=days[j];
if (j==1)//第二月
{
countday+=(y%4==0&&y%100!=0||y%400==0)?1:0;
}
}
return countday+d-1;
}
void main()
{
int y,m,d;
cout<<"请输入年月日:";
cin>>y>>m>>d;
cout<<"这是"<<weeks[(getDays(y,m,d)+BEGINWEEK-1)%7]<<endl;
}
tangshuiling 2008-11-02
  • 打赏
  • 举报
回复
谁说的if语句开销大来着~~~楼主的题意岂能不用到逻辑判断?
帅得不敢出门 2008-11-02
  • 打赏
  • 举报
回复
不用if那用switch
太乙 2008-11-02
  • 打赏
  • 举报
回复
晕,如果是闰年呢??

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧