题目:http://codeup.cn/problem.php?cid=100000578&pid=1
注意:字符串使用char month[13][13]={“Janury”,"","",……}中间使用双引号和逗号。(单引号不可以)
if–while使用错误,
num没有清零,
d0与num位置写错。
问题二,时间超限,答案错误。
问题代码:
if(d0==ym[isleap(y)][m0]+1){//满1个月
d0=1;
1月31号会被写成2月0号所以需要改正
注:(我的年月日算天数速度快)
正确代码:
#include<stdio.h>
#include<string.h>
//2021.02.10周三
char m[13][13]={"None",{"January"},{"February"},{"March"},"April","May","June","July","August","September","October","November","December"};
char w[8][13]={"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"};
int ym[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int isleap(int y){
if(y%4==0&&(y%100!=0)||(y%400==0)) return 1;
else return 0;
}
int subt(int y,int m,int d){
int i,sum=0;
int m0=1,d0=1,num=0;
for(i=1000;i<y;i++){//从1000年开始累加算年份
if(isleap(i)) sum+=366;
else sum+=365;
}
while((m0<m)||(d0<d)){//剩下差距不足一年
num++;
d0++;//天数
if(d0==ym[isleap(y)][m0]+1){//满1个月
d0=1;
m0++;
}
}
sum+=num;
return sum;
}
int main(){
int day,year;
int i;
char month[12];
while(scanf("%d %s %d",&day,month,&year)!=EOF){
for(i=0;i<12;i++){
int temp=strcmp(month,m[i]);
if(!temp) break;
}
int sub=subt(year,i,day);
printf("%s\n",w[sub%7]);
}
return 0;
}
原错误代码:
#include<stdio.h>
#include<string.h>
//2021.02.10周三
char m[13][13]={"None",{"January"},{"February"},{"March"},"April","May","June","July","August","September","October","November","December"};
char w[13][13]={"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"};
int ym[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int isleap(int y){
if(y%4==0&&(y%100!=0)||(y%400==0)) return 1;
else return 0;
}
int subt(int y,int m,int d){
int i,sum=0;
int m0=1,d0=1,num=0;
for(i=1000;i<y;i++){
if(isleap(i)) sum+=366;
else sum+=365;
}
while((m0<m)||(d0<d)){
num++;
d0++;
if(d0==ym[isleap(y)][m0]){
d0=0;
m0++;
}
}
sum+=num;
return sum;
}
int main(){
int day,year;
int i;
char month[12];
while(scanf("%d %s %d",&day,month,&year)!=EOF){
for(i=0;i<12;i++){
int temp=strcmp(month,m[i]);
if(!temp) break;
}
int sub=subt(year,i,day);
printf("%s",w[sub%7]);
}
return 0;
}
正确代码memcpy0
#include <cstdio>
#include <cstring>
struct Date {
int y, m, d;
};
char MonthName[13][12]= {"None", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
char Weekday[8][12] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
int month[13][2] = { //平年闰年的每个月天数(月/日), 第一行平年, 第二行闰年
{0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31},
{31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31} // {0, 0}没有意义
};
int mapMonth(char s[]) {
for (int i = 1; i < 13; i++) {
if (strcmp(s, MonthName[i]) == 0)
return i;
}
return 0;
}
bool isLeap(int year) { //判断是否是闰年
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
int sumDays(struct Date tmp) {
int day = 0;
for (int i = 0; i < tmp.y; i++) {
day += (isLeap(i) ? 366 : 365);
}
for (int i = 1; i < tmp.m; i++) {
day += month[i][isLeap(tmp.y)];
}
for (int i = 1; i < tmp.d; i++) {
day++;
}
return day; // 737634
}
int main() {
struct Date MyDay; // now = {2019, 7, 29}; 距离公元元年737634天
char mEnglish[14];
while (scanf("%d%s%d", &MyDay.d, mEnglish, &MyDay.y) != EOF) {
MyDay.m = mapMonth(mEnglish);
int dayCount = sumDays(MyDay) - 737634; //得到日期差值
int offset = ((dayCount % 7) + 7) % 7; //对负数和超出的正数修正
printf("%s\n", Weekday[1 + offset]); //1-7 20190729星期一
}
return 0;
}