算法-日期相关

日期相关

求n天之后的日期

1.java实现

import java.util.Scanner;
class MyDate 
{
	 private int day ; 
	 private int month ;     
	 private int year ;    
	 public void print()	//打印时间
	 {  
		 System.out.printf("%04d-%02d-%02d", year, month, day);//以标准化格式打印日期
	 }     
	 public MyDate(int year,int month,int day) 	//参数为具体年月日
	 { 
		 this.day = day;
		 this.month = month;         
		 this.year = year;
	 }
	 public MyDate(MyDate date)		//参数为对象
	 {	
		 this.day = date.day;
		 this.month = date.month;
		 this.year = date.year;
	 }
	 public MyDate addDays(int add_days,MyDate new_date) //计算日期,参数为需要增加的天数和现在的日期
	 {
		 MyDate temp_date = new MyDate(new_date);	//临时对象,不改变现在的日期
		 while(true)	//死循环
		 {
			 temp_date = nextDay(temp_date);	//计算下一天
			 add_days--;						//要增加的天数--
			 if(add_days == 0)					//当要增加的天数等于0时,停止循环
				 break;
		 }
		 return temp_date;
	 } 
	 public MyDate nextDay(MyDate date)		//计算下一天
	 {
		 if(date.day == isLastDays(date.year,date.month))	//如果是月底
		 {
			 date.day = 1;									//那么下一天则置天数为1
			 date.month++;									//月份++
			 if(date.month == 13)							//如果是年底
			 {
				 date.month = 1;							//那么月份置为1
				 date.year++;								//年份加1
			 }
		 }
		 else												//如果不是月底,天数加一即可
			 date.day++;
		 return date;
	 }
	 public int isLastDays(int year,int month)	//返回对应月份的天数
	 {
		 if(isRun(year) && month == 2)
			 return 29;
		 if(month == 2)
			 return 28;
		 if(month == 4 || month == 6 || month == 9 || month == 11)
			 return 30;
		 return 31;
	 }
	 public boolean isRun(int year)		//判断是否为闰年
	 {
		 if(year%400 == 0 || (year %4 == 0 && year %100 != 0))
			 return true;
		 else
			 return false;
	 }
}


public class Main {
	public static void main(String[] args)      
	{
		Scanner input = new Scanner(System.in);
		String[] sa=input.nextLine().split("-");
		int nowtime[] = new int[3];	
		for(int i = 0; i < 3;i++)
			nowtime[i] = Integer.parseInt(sa[i]);	//读取现在的时间
		int days = input.nextInt();			//读取要增加的天数
		MyDate nowdays = new MyDate(nowtime[0],nowtime[1],nowtime[2]);	//实例化对象-现在的时间
		MyDate nextdays = nowdays.addDays(days,nowdays);				//计算增加天数后的日期
	
		nextdays.print();
		input.close();
	}
}

2.c++实现

#include <cstdio>

int dayOfMonth[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 isLeapYear(int year) {
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

void addOneDay(int &year, int &month, int &day) {
    day++;
    if (day > dayOfMonth[isLeapYear(year)][month]) {
        month++;
        day = 1;
    }
    if (month > 12) {
        year++;
        month = 1;
    }
}

int main() {
    int year, month, day, n;
    scanf("%d-%d-%d", &year, &month, &day);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        addOneDay(year, month, day);
    }
    printf("%04d-%02d-%02d", year, month, day);
    return 0;
}

es

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的贪心算法截止日期问题的C++代码示例: ```c++ #include <iostream> #include <algorithm> #include <vector> using namespace std; struct Task { int id, deadline, profit; }; bool cmp(Task a, Task b) { return a.profit > b.profit; // 按照利润从大到小排序 } void scheduleTasks(vector<Task>& tasks) { sort(tasks.begin(), tasks.end(), cmp); int n = tasks.size(); vector<int> slot(n, -1); int profit = 0; for(int i=0; i<n; i++) { for(int j=min(n, tasks[i].deadline)-1; j>=0; j--) { if(slot[j] == -1) { // 如果该时间点可用 slot[j] = tasks[i].id; // 安排该任务 profit += tasks[i].profit; // 计算利润 break; } } } cout << "最大利润:" << profit << endl; cout << "安排的任务:"; for(int i=0; i<n; i++) { if(slot[i] != -1) { cout << slot[i] << " "; } } cout << endl; } int main() { int n; cout << "输入任务个数:" << endl; cin >> n; vector<Task> tasks(n); cout << "输入每个任务的截止日期和利润:" << endl; for(int i=0; i<n; i++) { tasks[i].id = i+1; cin >> tasks[i].deadline >> tasks[i].profit; } scheduleTasks(tasks); return 0; } ``` 该代码使用了一个结构体 `Task` 来表示每个任务的属性,其中包括任务编号、截止日期和利润。通过自定义比较函数 `cmp` 将任务按照利润从大到小排序。然后,使用一个 `vector` 来表示每个时间点是否被占用,初始化为 -1 表示该时间点可用。接着,从前往后遍历任务列表,对于每个任务,从该任务的截止日期开始往前找,找到第一个可用时间点,并将该任务安排在该时间点。最后,计算总利润和输出安排的任务列表。 注意,该代码中贪心策略是按照利润从大到小排序,优先安排利润高的任务。如果有多个任务的利润相同,则安排截止日期早的任务。这种贪心策略可能不是最优的,但可以得到一个近似最优解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值