杭电acm题目2005第几天总是Wrong answer
我的代码,编译器运行没错,可是在杭电那一直错#include<stdio.h>intmain(){inty,m,d;intnum=0;while(scanf("%d/%d...
我的代码,编译器运行没错,可是在杭电那一直错
#include <stdio.h>
int main(){
int y,m,d;
int num = 0;
while(scanf("%d/%d/%d",&y,&m,&d)!=EOF){
bool leap;
if(y % 4 == 0){
if(y % 100 == 0){
if(y % 400 == 0)
leap = true;
else leap = false;
}
else leap = true;
}
else leap = false;
switch (m){
case 1 :
num = 0;
break;
case 2:
num = 31;
break;
case 3 :
num = 31+28;
break;
case 4:
num = 31*2+28;
break;
case 5 :
num = 31*2+28+30;
break;
case 6 :
num = 31*3+28+30;
break;
case 7 :
num = 31*3+28+30*2;
break;
case 8 :
num = 31*4+28+30*2;
break;
case 9 :
num = 31*5+28+30*2;
break;
case 10 :
num = 31*5+28+30*3;
break;
case 11 :
num = 31*6+28+30*3;
break;
case 12 :
num = 31*6+28+30*4;
break;
}
num = num+d;
if(leap == true)
num = num+1;
printf("%d\n",num);
}
return 0;
} 展开
#include <stdio.h>
int main(){
int y,m,d;
int num = 0;
while(scanf("%d/%d/%d",&y,&m,&d)!=EOF){
bool leap;
if(y % 4 == 0){
if(y % 100 == 0){
if(y % 400 == 0)
leap = true;
else leap = false;
}
else leap = true;
}
else leap = false;
switch (m){
case 1 :
num = 0;
break;
case 2:
num = 31;
break;
case 3 :
num = 31+28;
break;
case 4:
num = 31*2+28;
break;
case 5 :
num = 31*2+28+30;
break;
case 6 :
num = 31*3+28+30;
break;
case 7 :
num = 31*3+28+30*2;
break;
case 8 :
num = 31*4+28+30*2;
break;
case 9 :
num = 31*5+28+30*2;
break;
case 10 :
num = 31*5+28+30*3;
break;
case 11 :
num = 31*6+28+30*3;
break;
case 12 :
num = 31*6+28+30*4;
break;
}
num = num+d;
if(leap == true)
num = num+1;
printf("%d\n",num);
}
return 0;
} 展开
3个回答
展开全部
//******************************************************************************
// 杭电2005 第几天? 代码已AC
// 算法没问题,只是细节方面没考虑周全
// 好好加油噢!!
//******************************************************************************
#include <stdio.h>
int main()
{
int y,m,d,leap;
int num = 0;
while (scanf("%d/%d/%d",&y,&m,&d)!=EOF)
{
// bool leap; // bool 类型是 C++新增的基本数据类型
if (y % 4 == 0) // 用 1 0 来代替
{
if (y % 100 == 0)
{
if (y % 400 == 0)
leap = 1;
else leap = 0;
}
else leap = 1;
}
else leap = 0;
switch (m)
{
case 1 :
num = 0;
break;
case 2:
num = 31;
break;
case 3 :
num = 31+28;
break;
case 4:
num = 31*2+28;
break;
case 5 :
num = 31*2+28+30;
break;
case 6 :
num = 31*3+28+30;
break;
case 7 :
num = 31*3+28+30*2;
break;
case 8 :
num = 31*4+28+30*2;
break;
case 9 :
num = 31*5+28+30*2;
break;
case 10 :
num = 31*5+28+30*3;
break;
case 11 :
num = 31*6+28+30*3;
break;
case 12 :
num = 31*6+28+30*4;
break;
}
num = num+d;
if (leap == 1&&m>2) // 闰年且月份大于2 才需要加一
num = num+1;
printf("%d\n",num);
}
return 0;
}
//******************************************************************************
// ******************下面是我写的,可供参考****************************
//******************************************************************************
#include<stdio.h>
int main()
{
int i, n, year, moth, day, a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while(scanf("%d/%d/%d", &year, &moth, &day) != EOF)
{
if (moth>2&&((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0)))
day+=1;
for(n = i = 0; i < moth-1; i++)
n += a[i];
printf("%d\n", n + day);
}
}
//******************************************************************************
// 祝学习进步,更上一层楼 *(^-^)*
//******************************************************************************
// 杭电2005 第几天? 代码已AC
// 算法没问题,只是细节方面没考虑周全
// 好好加油噢!!
//******************************************************************************
#include <stdio.h>
int main()
{
int y,m,d,leap;
int num = 0;
while (scanf("%d/%d/%d",&y,&m,&d)!=EOF)
{
// bool leap; // bool 类型是 C++新增的基本数据类型
if (y % 4 == 0) // 用 1 0 来代替
{
if (y % 100 == 0)
{
if (y % 400 == 0)
leap = 1;
else leap = 0;
}
else leap = 1;
}
else leap = 0;
switch (m)
{
case 1 :
num = 0;
break;
case 2:
num = 31;
break;
case 3 :
num = 31+28;
break;
case 4:
num = 31*2+28;
break;
case 5 :
num = 31*2+28+30;
break;
case 6 :
num = 31*3+28+30;
break;
case 7 :
num = 31*3+28+30*2;
break;
case 8 :
num = 31*4+28+30*2;
break;
case 9 :
num = 31*5+28+30*2;
break;
case 10 :
num = 31*5+28+30*3;
break;
case 11 :
num = 31*6+28+30*3;
break;
case 12 :
num = 31*6+28+30*4;
break;
}
num = num+d;
if (leap == 1&&m>2) // 闰年且月份大于2 才需要加一
num = num+1;
printf("%d\n",num);
}
return 0;
}
//******************************************************************************
// ******************下面是我写的,可供参考****************************
//******************************************************************************
#include<stdio.h>
int main()
{
int i, n, year, moth, day, a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while(scanf("%d/%d/%d", &year, &moth, &day) != EOF)
{
if (moth>2&&((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0)))
day+=1;
for(n = i = 0; i < moth-1; i++)
n += a[i];
printf("%d\n", n + day);
}
}
//******************************************************************************
// 祝学习进步,更上一层楼 *(^-^)*
//******************************************************************************
展开全部
编译没错不代表程序没错。我写一个供你参考一下。你的代码风格和思路都有待改进。
#include <cstdio>
using namespace std;
char date[10];
int year, month, day;
int days[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,
};
bool is_leap_year(int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100);
}
int main() {
while (scanf("%s", date) == 1) {
sscanf(date, "%d/%d/%d", &year, &month, &day);
int leap = is_leap_year(year) ? 1 : 0;
int sum = 0;
for (int i = 1; i < month; ++i) {
sum += days[leap][i];
}
sum += day;
printf("%d\n", sum);
}
return 0;
}
你的代码中scanf的用法是不行的,应该用sscanf。要有模块化的思想,写函数、分过程。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(leap == true)
num = num+1;
这里少了一个。
if(m>2&&leap == true)
num = num+1;
不然会把1月的也算上,多加1天。
比如 2010/1/20
num = num+1;
这里少了一个。
if(m>2&&leap == true)
num = num+1;
不然会把1月的也算上,多加1天。
比如 2010/1/20
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询