[英]program to get a month the bth c day of the week
例如,獲取從 2000 年到 2015 年 10 月的第三個星期五的日期。我知道 1850-1-1 是星期二。
我在 OJ 上得了 80 分(低於最高分)。 我不知道錯誤在哪里。 請幫我找出問題所在。 我已經嘗試了很長時間,但無法解決問題。 下面是我的代碼:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int daysFrom[250];
bool year_check(int year);
string int2String(int a);
int main(){
unsigned int month2days[13];
month2days[0]=0;
month2days[1]=31;
month2days[2]=0;
month2days[3]=31;
month2days[4]=30;
month2days[5]=31;
month2days[6]=30;
month2days[7]=31;
month2days[8]=31;
month2days[9]=30;
month2days[10]=31;
month2days[11]=30;
month2days[12]=31;
int a;
cin>>a;
int b;
cin>>b;
int c;
cin>>c;
int y1;
cin>>y1;
int y2;
cin>>y2;
daysFrom[0]=0;
for(int i=1;i<250;i++){
int year=1850+i;
if(year_check(year-1)){
daysFrom[i]=daysFrom[i-1]+366;
}else{
daysFrom[i]=daysFrom[i-1]+365;
}
}
for(int yearP=y1;yearP<=y2;yearP++){
unsigned int days=daysFrom[yearP-1850];
for(int monthP=1;monthP<a;monthP++){
if(monthP==2){
if(year_check(yearP)){
days+=29;
}else{
days+=28;
}
}else{
days+=month2days[monthP];
}
}
int weekPos=days%7;
weekPos=(weekPos+2)%7;
int dis=0;
if(c<weekPos){
dis=7-weekPos+c+1+(b-1)*7;
}else{
dis=c-weekPos+1+(b-1)*7;
}
if(a==2){
if(year_check(yearP)){
if(dis>29){
cout<<"none"<<endl;
}else{
cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
}
}else{
if(dis>28){
cout<<"none"<<endl;
}else{
cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
}
}
}else{
if(dis>month2days[a]){
cout<<"none"<<endl;
}else{
cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
}
}
}
return 0;
}
bool year_check(int year){
if(year%400==0){
return true;
}else{
if(year%4==0&&year%100!=0){
return true;
}
}
return false;
}
string int2String(int a){
switch(a){
case 1:
return "01";
case 2:
return "02";
case 3:
return "03";
case 4:
return "04";
case 5:
return "05";
case 6:
return "06";
case 7:
return "07";
case 8:
return "08";
case 9:
return "09";
default:
stringstream str;
str<<a;
return str.str();
}
}
我知道了。 問題是
weekPos=(weekPos+2)%7;
星期天是 0。實際上是 7。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.