首页 > 试题广场 >

编程题 定义一个结构体变量(包括年、月、日),计算该日

[问答题]

编程题

定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数days,实现上面的计算。由主函数将年月日传递给days函数,计算后将日子传递回主函数输出。

public class Test1 { public static void Day(int year, int month, int day) { int runMonth[] = {31,29,31,30,31,30,31,31,30,31,30,31};  int pinMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};  int currentDay = 0;  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { for (int i = 0; i < month - 1; i++) {
                currentDay += runMonth[i];  }
            currentDay += day;  } else { for (int i = 0; i < month - 1; i++) {
                currentDay += pinMonth[i];  }
            currentDay += day;  }
        System.out.println("今天是" + year + "年的第" + currentDay + "");  } public static void main(String[] args) { Day(2000,3,1);  }
}
发表于 2022-09-07 21:51:54 回复(0)
class Date {
    private int year;
    private int month;
    private int day;

    public Date(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}


public class Test {

    public static int days(Date date){
        int [] arr = {31,28,31,30,31,30,31,31,30,31,30,31};
        int sum = 0;
        if (date != null) {
            int year = date.getYear();
            int month = date.getMonth();
            int day = date.getDay();
            if(((year %4 ==0 ) && (year %100 !=0)) || (year %400 ==0)){
                arr[1] = 29;
            }
            for (int i = 1;i < month ;i++){
                sum += arr[i-1];
            }
            sum += day;
        }
        return sum;
    }

    public static void main(String[] args) {
        Date date = new Date(2020,3,1);
        System.out.println(days(date));
    }

}

发表于 2021-03-01 15:17:12 回复(0)
public Class Test{
    public static int days(int y, int m,int d){
        int[] bm = new int[]{1,3,5,7,8,10,12};//大月份
        int[] sm = new int[]{4,6,9,11};//小月份
        int feb = 28;
        if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0){
            feb = 29;
        }
        Map<Integer,Integer> mothDays = new HashMap<>();//日期的月份-天数
        for(int c:bm){
            mothDays.put(c,31);
        }
        for(int c:sm){
            mothDays.put(c,30);
        }
        mothDays.put(2,feb);
        int sum = 0;
        for(int i = 1; i < m; i++){
            sum += mothDays.get(i);
        }
        sum += d;
        return sum;
    }
    public static int days(String time){
        Date date = new Date(time);//使用Date判断格式是否符合要求
        int y = 0;
        int m = 0;
        int d = 0;
        if (time.contains("/")){
            String[] split = time.split("/");
            y = Integer.parseInt(split[0]);
            m = Integer.parseInt(split[1]);
            d = Integer.parseInt(split[2]);
        }
        return days(y,m,d);
    }
    public static void main(String[] args){
        System.out.println(days("2020/12/31"));
        System.out.println(days(2020,12,31));
    }
}

发表于 2020-10-07 16:07:37 回复(0)
import java.util.*
public class Main{

    public int days(Date date){
        int year = date.getYear() + 1900;
        long time = date.getTime();
        long startTime = new SimpleDateFormat("yyyyMMdd").parse(year + "0101").getTime();
        return (int) (time - startTime)/(1000*60*60*24);
    }
}


发表于 2020-09-13 23:01:53 回复(0)

#include<iostream>
using namespace std;
struct ymd
{
    int day;
    int month;
    int year;
};
int dayof[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int days(struct ymd *p)
{
    int i, d;
    if (p->year % 4 == 0 && p->year % 100 != 0 || p->year % 400 == 0)
        dayof[2] = 29;
    d = p->day;//这个月的天数
    for (i = 1; i<p->month; i++)
        d = d + dayof[i];//加上前面每个月的天数
    return d;
}
void main()
{
    struct ymd date;
    int d;
    while(1)
    {
        cout << "-----------------------------------" << endl;
        cout<<"date(yyyy/mm/dd)=? (yyyy=0--Exit)"<<endl;
        cin>>date.year>>date.month>>date.day;
        if (date.year == 0)//当输入年份==0时退出
            break;
        d = days(&date);
        cout << "The day of the year is " << d << endl;
    }
}




编辑于 2019-09-24 18:16:04 回复(0)