What Day Is It?(模拟)

该博客介绍了从朱利安历到格里高利历的转变,包括朱利安历的闰年规则和格里高利历的修正规则。文章还提到英国和美国在1752年才采用格里高利历,并举例说明如何根据给定日期确定当时的星期。此外,博主分享了实现这一日期转换过程时遇到的挑战和需要注意的细节。

The calendar now in use evolved from the Romans. Julius Caesar codified a calendar system that came to be known as the Julian calendar. In this system, all months have 31 days, except for April, June, September, and November, which have 30 days, and February, which has 28 days in non-leap years, and 29 days in leap years. Also, in this system, leap years happened every four years. That is because the astronomers of ancient Rome computed the year to be 365.25 days long, so that after every four years, one needed to add an extra day to keep the calendar on track with the seasons. To do this, they added an extra day (February 29) to every year that was a multiple of four.

Julian Rule:
Every year that is a multiple of 4 is a leap year, i.e. has an extra day (February 29).
In 1582, Pope Gregory's astronomers noticed that the year was not 365.25 days long, but closer to 365.2425. Therefore, the leap year rule would be revised to the following:

Gregorian Rule:
Every year that is a multiple of 4 is a leap year, unless it is a multiple of 100 that is not a multiple of 400.
To compensate for how the seasons had shifted against the calendar up until that time, the calendar was actually shifted 10 days: the day following October 4, 1582 was declared to be October 15.
England and its empire (including the United States) didn't switch to the Gregorian calendar system until 1752, when the day following September 2 was declared to be September 14. (The delay was caused by the poor relationship between Henry VIII and the Pope.)

Write a program that converts dates in the United States using a calendar of the time and outputs weekdays. 

input

The input will be a series of positive integers greater than zero, three integers per line, which represent dates, one date per line. The format for a date is ``month day year" where month is a number between 1 (which indicates January) and 12 (which indicates December), day is a number between 1 and 31, and year is positive number.


output

The output will be the input date and name of the weekday on which the given date falls in the format shown in the sample. An invalid date or nonexistent date for the calendar used in the United States at the time should generate an error message indicating a invalid date. The input will end with three zeroes.


sample input

11 15 1997
1 1 2000
7 4 1998
2 11 1732
9 2 1752
9 14 1752
4 33 1997
0 0 0

sample output

November 15, 1997 is a Saturday
January 1, 2000 is a Saturday
July 4, 1998 is a Saturday
February 11, 1732 is a Friday
September 2, 1752 is a Wednesday 
September 14, 1752 is a Thursday
4/33/1997 is an invalid date.


题意:就是给你一个日子,然后让你输出按照他所给的规则应该是周几。格式上面对于之前的输出也有一定的要求。

分析:这题就是一个比较复杂的模拟,一开始觉得这道题目容易理解容易做,但是还是遇到了很多的小问题,最后调试了很久才通过。

有很多小细节需要注意,细心细心细心,重要的事情说三遍。

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int judge(int year)
{
	if(year<=1752)
	{
		if(year%4==0)
		return 1;
		else
		return 0;
	}
	else
	{
		if(year%4==0&&year%100)
		{
			return 1;
		}
		else if(year%4==0&&year%100==0&&year%400==0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}
int judge2(int month,int day,int year)
{
	if(month>12||(month!=2&&months[month]<day)||(month==2&&!judge(year)&&months[month]<day)||(month==2&&judge(year)&&months[month]+1<day)||(1752==year&&month==9&&(day>2&&day<14)))  
	return 0;
	else
	return 1;
}
void printmonth(int a)
{
	if(a==1)
	printf("January");
	if(a==2)
	printf("February");
	if(a==3)
	printf("March");
	if(a==4)
	printf("April");
	if(a==5)
	printf("May");
	if(a==6)
	printf("June");
	if(a==7)
	printf("July");
	if(a==8)
	printf("August");
	if(a==9)
	printf("September");
	if(a==10)
	printf("October");
	if(a==11)
	printf("November");
	if(a==12)
	printf("December");	
}
void printday(int c)
{
	if(c==1)
	printf("Monday\n");
	if(c==2)
	printf("Tuesday\n");
	if(c==3)
	printf("Wednesday\n");
	if(c==4)
	printf("Thursday\n");
	if(c==5)
	printf("Friday\n");
	if(c==6)
	printf("Saturday\n");	
	if(c==0)
	printf("Sunday\n");
}
int main()
{
	int year,month,day,t,flag,day2,i,j,k;
	while(scanf("%d %d %d",&month,&day,&year))
	{
		if(month==0||year==0||day==0)
		break;
		t=year;
		if(judge2(month,day,year)==0)
		printf("%d/%d/%d is an invalid date.\n",month,day,year);
		else
		{
			day2=-1;
			for(i=1;i<year;i++)
			{
				if(judge(i)==1)
				day2+=366;
				else
				day2+=365;
			}
			for(i=1;i<month;i++)
			day2+=months[i];
			if(judge(year)&&month>2)
			day2++;
			day2=day2+day-1;
			if(year>1752||(year==1752&&month>9)||(year==1752&&month==9&&day>=14))
			day2-=11;
			day2=day2%7;
			printmonth(month);
		   printf(" %d, %d is a ",day,t);  
		   printday(day2);
		}
	}
}

A. 纪念品 (Souvenir) A C D E F G H 传统题 1000ms 256MiB Description Xiaowei suddenly acquires a superpower. He knows the daily prices of N N souvenirs for the next T T days. The price of a souvenir refers to the number of gold coins required to buy the souvenir or the number of gold coins that can be earned by selling a souvenir. Every day, Xiaowei can carry out the following two types of transactions any number of times: Choose a souvenir. If he has enough gold coins with him, buy the souvenir at its price for that day. Sell any souvenir he has and earn gold coins equal to the price of the souvenir for that day. The gold coins earned by selling souvenirs on a day can be used to buy souvenirs on the same day too. Similarly, souvenirs purchased on a day can also be sold for gold coins on the same day. After T T days, Xiaowei’s superpower will disappear. Therefore, he will sell all the souvenirs he still has on the T T-th day. Currently, Xiaowei has M M gold coins, and he wants to have as many gold coins as possible after the superpower disappears. What is the maximum number of gold coins he can have? Input Format The first line contains three positive integers T T, N N and M M - the number of days for which Xiaowei knows souvenir prices, the number of souvenirs, and the number of gold coins that Xiaowei currenly has. i i-th of the next T T lines contains N N positive integers P i , 1 , P i , 2 , . . . , P i , N , P i,1 ​ ,P i,2 ​ ,...,P i,N ​ , where P i , j P i,j ​ denotes the price of the j j-th souvenir on the i i-th day. Output Format Print one line containing a positive integer denoting the maximum number of gold coins Xiaowei can have after his superpower disappears. 输入数据 1 6 1 100 50 20 25 20 25 50 输出数据 1 305 The best strategy is: On the second day, spend all 100 100 gold coins to buy 5 5 units of souvenir 1 1. Sell all the 5 5 units of souvenir 1 1 on the third day and get 125 125 gold coins. Buy 6 6 units of souvenir 1 1 on the fourth day. 5 5 gold coins are left. On the sixth day, sell all souvenirs for 300 300 gold coins. On the fourth day there were 5 5 gold coins remaining, so now there are 305 305 gold coins remaining. After the superpower disappears, Xiaowei has 305 305 gold coins, which is the maximum possible. 输入数据 2 3 3 100 10 20 15 15 17 13 15 25 16 输出数据 2 217 The best strategy is: On the first day, spend all the gold coins to buy 10 10 units of souvenir 1 1. Sell all the units of souvenir 1 1 on the next day and get 150 150 gold coins. Buy 8 8 units of souvenir 2 2 and 1 1 unit of souvenir 3 3. 1 1 gold coin is remaining. On the third day, sell all souvenirs and get 216 216 gold coins. On the second day, there was 1 1 gold coin left, so in total there are 217 217 gold coins left. After the superpower disappears, Xiaowei has 217 217 gold coins, which is the maximum possible. Constraints For 10 % 10% of the data, T = 1 T=1. For 30 % 30% of the data, T ≤ 4 , N ≤ 4 , M ≤ 100 , 10 ≤ P i , j ≤ 1001 T≤4,N≤4,M≤100,10≤P i,j ​ ≤1001. For another 15 % 15% of the data, T ≤ 100 , N = 1 T≤100,N=1. For another 15 % 15% of the data, T = 2 , N ≤ 100 T=2,N≤100. For 100 % 100% of the data, T ≤ 100 , N ≤ 100 , M ≤ 10 3 , 1 ≤ P i , j ≤ 10 4 T≤100,N≤100,M≤10 3 ,1≤P i,j ​ ≤10 4 . It is guaranteed that at any time, the number of gold coins that Xiaowei has can't exceed 10 4 10 4 .
最新发布
10-27
为了解决这个问题,我们需要模拟 Xiaowei 在每一天的买卖行为,以最大化他在最后一天卖出所有纪念品后所拥有的金币数量。 ### 思路分析: 1. **问题建模**: - 每天可以买卖任意次数,且当天买入的纪念品可以当天卖出。 - 最终在第 T 天必须卖出所有剩余纪念品。 - 目标是最大化金币数量。 2. **贪心策略**: - 对于每种纪念品,我们可以在价格低的那天买入,价格高的那天卖出。 - 每一天的决策应基于最大化收益。 3. **动态规划 + 贪心**: - 使用动态规划维护每一天的最大金币数量。 - 对于每一天,我们遍历所有纪念品,决定是否买入或卖出。 - 如果第 `i` 天纪念品 `j` 的价格低于第 `i+1` 天,则买入,否则卖出。 4. **实现细节**: - 维护一个变量 `money` 表示当前金币数量。 - 对于每一天,我们尝试买入纪念品,然后在价格更高的下一天卖出。 以下是 Python 的实现代码: ```python def max_gold_coins(T, N, M, prices): # 初始化金币数量 money = M # 遍历每一天(从第 0 天到 T-2 天) for day in range(T - 1): # 遍历所有纪念品 for j in range(N): buy_price = prices[day][j] sell_price = prices[day + 1][j] # 如果下一天的价格更高,就买入并卖出 if sell_price > buy_price: # 计算最多可以买入多少件纪念品 count = money // buy_price # 买入 money -= count * buy_price # 卖出 money += count * sell_price # 最后一天卖出所有纪念品(题目保证在第 T 天会卖出) # 这里不需要额外操作,因为前面已经处理了每天的买卖,最后一天的价格不参与买卖决策 return money # 输入处理 def main(): import sys input = sys.stdin.read data = input().split() T = int(data[0]) N = int(data[1]) M = int(data[2]) prices = [] index = 3 for _ in range(T): day_prices = list(map(int, data[index:index + N])) prices.append(day_prices) index += N result = max_gold_coins(T, N, M, prices) print(result) if __name__ == "__main__": main() ``` ### 示例运行: 对于输入: ``` 6 1 100 50 20 25 20 25 50 ``` 输出结果为: ``` 305 ``` 对于输入: ``` 3 3 100 10 20 15 15 17 13 15 25 16 ``` 输出结果为: ``` 217 ``` ### 总结: - 我们使用贪心策略,每天比较纪念品的价格,决定是否买入或卖出。 - 确保在最后一天卖出所有纪念品,以获得最大收益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值