﻿ 给出年、月、日，计算该日是该年的第几天。 - Paren's Blog

377 浏览 | 2020-05-18 | 阅读时间: 约 1 分钟 | 分类: 开发记录 | 标签:

# 代码

## 1.直接嵌套

``````/*19通信 田博

#include<stdio.h>
int main(){
int year,month,day;
int res=0,i;
int r[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d[12];
scanf("%d%d%d",&year,&month,&day);
if(year%4==0&&year%100!=0||year%400==0){
for(i=0;i<12;i++){
d[i]=r[i];
}
}
else{
for(i=0;i<12;i++){
d[i]=p[i];
}
}
if (month>=1&&month<=12){
if(day>=1&&day<=d[month-1]){
for(i=0;i<month-1;i++){
res=res+d[i];
}
res=res+day;
printf("%d\n",res);
}else{
printf("输入的时间不正确\n");
}
}else{
printf("输入的时间不正确\n");
}
return 0;
}
``````

## 2.参数传递

``````/*19通信 田博

#include<stdio.h>
int main(){
int year,month,day;
int res=0;
int r[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int *d;
int get(int year,int month ,int day,int d[]);
scanf("%d%d%d",&year,&month,&day);
if(year%4==0&&year%100!=0||year%400==0){
d=r;
}
else{
d=p;
}
res=get(year,month,day,p);
if(res==0){
printf("日期错误");
}else{
printf("%d",res);
}
}
int get(int year,int month ,int day,int d[]){
int res,i;
if (month>=1&&month<=12){
if(day>=1&&day<=d[month-1]){
for(i=0;i<month-1;i++){
res=res+d[i];
}
res=res+day;
return res;
}else{
return (0);
}
}else{
return (0);
}
}``````

## 3.不用指针传递数组

``````#include<stdio.h>
int main(){
int year,month,day;
int res;
int r[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int get(int year,int month ,int day,int d[]);
scanf("%d%d%d",&year,&month,&day);
if(year%4==0&&year%100!=0||year%400==0){
res=get(year,month,day,r);
}
else{
res=get(year,month,day,p);

}
if(res==0){
printf("日期错误");
}else{
printf("%d",res);
}
}
int get(int year,int month ,int day,int d[]){
int res=0,i;
if (month>=1&&month<=12){
if(day>=1&&day<=d[month-1]){
for(i=0;i<month-1;i++){
res=res+d[i];
}
res=res+day;
return res;
}else{
return (0);
}
}else{
return (0);
}
}``````

email
web

### 全部评论 (暂无评论)

info 还没有任何评论，你来说两句呐!