NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
现在给你一段时间区间,请你帮他计算总收益有多少。
输入包含多组数据。
每组数据包含两个日期from和to (2000-01-01 ≤ from ≤ to ≤ 2999-12-31)。
日期用三个正整数表示,用空格隔开:year month day。
对应每一组数据,输出在给定的日期范围(包含开始和结束日期)内能赚多少钱。
2000 1 1 2000 1 31 2000 2 1 2000 2 29
62 29
#include <iostream>
using namespace std;
int main()
{
int arr[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year1,month1,day1,year2,month2,day2;
while(cin>>year1>>month1>>day1>>year2>>month2>>day2)
{
int ret=0;
day1--;//1号到1号也会盈利一天,所以提前将day1减一天方便直接做减法计算能盈利的天数
while(year1<year2||month1<month2)
{
//如果前者的日期和后者的日期还不在同一个年月,则ret累加day1到月底的盈利
//如果是闰年的2月则一共29天,否则直接使用arr数组,另外素数月盈利与非素数月盈利不同,用三目运算符来表示
ret+=(((month1==2&&((year1%4==0&&year1%100!=0)||year1%400==0))?29:arr[month1])-day1)*((month1==2||month1==3||month1==5||month1==7||month1==11)?1:2);
day1=0;//day1来到下一个月的第一天,与上方day1--同理,这里不设置为1而是0
if(++month1>12)//月份也要向后走
{
month1=1;
year1++;
}
}
//如果day1来到了day2所在的年月,那么用day2-day1就可以算出盈利了多少天
cout<<ret+(day2-day1)*((month1==2||month1==3||month1==5||month1==7||month1==11)?1:2)<<endl;
}
return 0;
} #include <iostream>
using namespace std;
//判断闰年
bool leap_year(int year){
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
//足年天数
inline int profit_of_year(int year){
return 2*31+28+31+2*30+31+2*30+31+2*31+2*30+2*31+30+2*31+leap_year(year);
}
//求前半段
int pro_sum(int year, int month, int day){
int res = 0;
int i = 0;
if(leap_year(year) && month > 2){
i = 1;
}
if(month == 1){
res = 2*day;
}
else if(month == 2){
res = 2*31+day;
}
else if(month == 3){
res = 2*31+28+day;
}
else if(month == 4){
res = 2*31+28+31+2*day;
}
else if(month == 5){
res = 2*31+28+31+2*30+day;
}
else if(month == 6){
res = 2*31+28+31+2*30+31+2*day;
}
else if(month == 7){
res = 2*31+28+31+2*30+31+2*30+day;
}
else if(month == 8){
res = 2*31+28+31+2*30+31+2*30+31+2*day;
}
else if(month == 9){
res = 2*31+28+31+2*30+31+2*30+31+2*31+2*day;
}
else if(month == 10){
res = 2*31+28+31+2*30+31+2*30+31+2*31+2*30+2*day;
}
else if(month == 11){
res = 2*31+28+31+2*30+31+2*30+31+2*31+2*30+2*31+day;
}
else if(month == 12){
res = 2*31+28+31+2*30+31+2*30+31+2*31+2*30+2*31+30+2*day;
}
return res + i;
}
int main(){
int year1, month1, day1;
int year2, month2, day2;
int sum = 0;
while(cin >> year1 >> month1 >> day1 >> year2 >> month2 >> day2){
sum = 0;
day1 -= 1;
if(year1 == year2){
sum = pro_sum(year2, month2, day2) - pro_sum(year1, month1, day1);
}
else{
sum += profit_of_year(year1) - pro_sum(year1, month1, day1);
sum += pro_sum(year2, month2, day2);
++year1;
while(year1 != year2){
sum += profit_of_year(year1);
++year1;
}
}
cout << sum << endl;
}
return 0;
}
// write your code here cpp
#include<iostream>
using namespace std;
//非闰年为标准
int totalday[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//非闰年 - 每个月的总收益
int totalMoney[12] = {62, 28, 31, 60, 31, 60, 31, 62, 60, 62, 30, 62};
//判断该月份是否为素数
bool Judge_IsPriNum(int month){
if(month == 2 || month == 3 || month == 5 || month == 7 ||
month == 11){
return true;
}
return false;
}
//判断该年份是否为闰年
bool Judge_IsLeaYear(int year){
if( (year % 4 == 0 && year % 100 != 0) ||
year % 400 == 0){
return true;
}
return false;
}
//计算相差day2 - day1天数的收益
int Caclue_Day(int day1, int day2, int month){
//1 - 31, 是31天 31 - 1 = 30 -- 得加1
int sum = day2 - day1 + 1;
if(!Judge_IsPriNum(month)){
//非素数一天赚2块
sum += sum;
}
return sum;
}
//计算相差month2 - month1 整月数的收益
int Caclue_Month(int month1, int month2, int year){
int sum = 0;
for(int i = month1; i <= month2; ++i){
//下标-1
sum += totalMoney[i - 1];
//2月 - 闰年 - 多加1块
if(i == 2 && Judge_IsLeaYear(year)){
sum += 1;
}
}
return sum;
}
//计算相差year2 - year1 整年数的收益
int Caclue_Year(int year1, int year2){
int sum = 0;
for(int i = year1; i <= year2; ++i){
sum += Caclue_Month(1, 12, i);
}
return sum;
}
int main(){
//定义接受的年月日
int year1, month1, day1;
int year2, month2, day2;
while(cin >> year1 >> month1 >> day1
>> year2 >> month2 >> day2){
//由于是整数 还是 是空格分隔, 采用cin就可以
int total_num = 0;
if(year1 == year2){
//同一年份
if(month1 == month2){
//同一月份
total_num = Caclue_Day(day1, day2, month1);
}
else{
//不同月份
//1、先计算两者相差整月份的收益
total_num += Caclue_Month(month1, month2, year1);
//2、month1首月不足一月的天数收益
total_num += Caclue_Day(day1, totalday[month1 - 1], month1);
//3、month2尾月不足一月的天数收益
total_num += Caclue_Day(1, day2, month2);
}
}
else{
//不同年份
//1、先计算两者相差整年份的收益
total_num += Caclue_Year(year1 + 1, year2 - 1);
//2、year1年份不足一年整月份的收益
total_num += Caclue_Month(month1 + 1, 12, year1);
//3、year2年份不足一年整月份的收益
total_num += Caclue_Month(1, month2 - 1, year2);
//4、year1中首月不足一月的收益
total_num += Caclue_Day(day1, totalday[month1 - 1], month1);
//5、year2中尾月不足一月的收益
total_num += Caclue_Day(1, day2, month2);
}
//输出
cout << total_num << endl;
}
} #include <cstdio> using namespace std; bool isLeapYear(int year)
{
return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
} int main()
{
const int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const int money_day[12] = { 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2 };
const int money_month[12] = { 31 * 2, 28, 31, 30 * 2, 31, 30 * 2, 31, 31 * 2, 30 * 2, 31 * 2, 30, 31 * 2 };
const int money_year = 31 * 2 + 28 + 31 + 30 * 2 + 31 + 30 * 2 + 31 + 31 * 2 + 30 * 2 + 31 * 2 + 30 + 31 * 2;
//freopen("in.txt", "r", stdin);
int start_year, start_month, start_day, end_year, end_month, end_day;
int profit;
while (scanf("%d%d%d%d%d%d",&start_year,&start_month,&start_day,&end_year,&end_month,&end_day) != EOF)
{
profit = 0;
if (end_year - start_year >= 2)
{
profit += (end_year - start_year - 1) * money_year;
for (int i = start_year + 1; i <= end_year - 1; ++i)
if (isLeapYear(i)) ++profit;
}
if (end_year > start_year)
{
profit += (money_day[start_month - 1]) * (month_days[start_month - 1] - start_day + 1);
for (int i = start_month; i < 12; ++i) profit += money_month[i];
if (isLeapYear(start_year) && start_month <= 1) ++profit;
profit += (money_day[end_month - 1]) * end_day;
for (int i = 0; i < end_month - 1; ++i) profit += money_month[i];
}
else
{
if (end_month - start_month >= 2)
{
for (int i = start_year + 1; i <= end_year - 1; ++i)
profit += money_month[i];
}
if (end_month > start_month)
{
profit += (money_day[start_month - 1]) * (month_days[start_month - 1] - start_day + 1);
if (isLeapYear(start_year) && start_month <= 1) ++profit;
profit += (money_day[end_month - 1]) * end_day;
}
else
{
profit += (end_day - start_day + 1) * money_day[end_month - 1];
}
} printf("%d\n", profit);
} return 0;
} #include <iostream>
(720)#include <string>
using namespace std;
bool leapYear(int year)
{
if (year % 100 == 0 || ((year % 4 == 0) && (year % 400 != 0)))
{
return true;
}
return false;
}
bool Prime(int month)
{
if (month == 2 || month == 3 || month == 5 || month == 7 || month == 11)
return true;
return false;
}
int day_money(int month)
{
if (month == 2 || month == 3 || month == 5 || month == 7 || month == 11)
return 1;
return 2;
}
int main()
{
int year1 = 0;
int month1 = 0;
int day1 = 0;
int year2 = 0;
int month2 = 0;
int day2 = 0;
static int Moneys[] = { 0, 62, 28, 31, 60, 31, 60, 31, 62, 60, 62, 30, 62 };// 1 3 5 7 11
while (cin >> year1 >> month1 >> day1 >> year2 >> month2 >> day2)
{
int to_money = 0;
int from_money = 0;
for (int i = 0; i < month2; i++)
{
to_money += Moneys[i];
}
if (Prime(month2))
to_money += day2;
else
to_money += day2 * 2;
if (leapYear(year2) && month2 >= 3)
to_money += 2;
for (int i = 0; i < month1; i++)
{
from_money += Moneys[i];
}
if (leapYear(year1) && month1 >= 3)
from_money += 2;
if (Prime(month1))
from_money += day1;
else
from_money += day1 * 2;
if (year2 == year1)
{
cout << to_money - from_money + day_money(month1) << endl;
}
else
{
int dif_money = 0;
if (leapYear(year1))
{
dif_money += (581 - from_money + day_money(month1));
}
else
{
dif_money += (579 - from_money + day_money(month1));
}
year1 += 1;
for (int i = year1; i < year2; i++)
{
if (leapYear(i))
dif_money += 581;
else
dif_money += 579;
}
dif_money += to_money;
cout << dif_money << endl;
}
}
}
#include<iostream>
using namespace std;
int nor[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool Judge_leap(int y)
{ if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) return true; else return false;
}
int Earn(int y1, int y2, int m1, int m2, int d1, int d2)
{ int money = 0; if (m1 == 2 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 11) money = 1; else money = 2; while ((y1 != y2) || (m1 != m2) || (d1 != d2)) { if (Judge_leap(y1)) nor[2] = 29; else nor[2] = 28; if (d1 <= nor[m1]) { if (m1 == 2 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 11) { money += 1; d1++; } else { money += 2; d1++; } } else { if (m1 == 12) { y1++; m1 = 1; d1 = 1; } else { m1++; d1 = 1; } } } return money;
}
int main()
{ int y1, m1, d1; int y2, m2, d2; while (cin >> y1 >> m1 >> d1) { cin >> y2 >> m2 >> d2; cout << Earn(y1, y2, m1, m2, d1, d2) << endl; } return 0;
}
import java.util.Scanner;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
LocalDate start = LocalDate.of(in.nextInt(),in.nextInt(),in.nextInt());
LocalDate end = LocalDate.of(in.nextInt(),in.nextInt(),in.nextInt());
int money = 0;
while(start.compareTo(end)<=0){
int month = start.getMonthValue();
if(month==2||month==3||month==5||month==7||month==11){
money+=1;
}else{
money+=2;
}
start=start.plusDays(1);
}
System.out.println(money);
}
}
} import java.util.Scanner;
import java.time.LocalDate;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
int year1 = in.nextInt();
int month1 = in.nextInt();
int day1 = in.nextInt();
LocalDate L1 = LocalDate.of(year, month, day);
LocalDate L2 = LocalDate.of(year1, month1, day1);
System.out.println(date(L1, L2));
}
}
//计算获取收益
public static int date(LocalDate L1, LocalDate L2) {
int count = 0;
while (!L1.isAfter(L2)) {
if (isSuShu(L1.getMonthValue())) {
count++;
}else{
count+=2;
}
L1 = L1.plusDays(1);
}
return count;
}
private static boolean isSuShu(int month) {
if (month == 2 || month == 3 || month == 5 || month == 7 || month == 11) {
return true;
} else {
return false;
}
}
}
大佬帮忙看看我这错哪里呢?
在IDEA上没报错 但是在牛客上出现这个错误,于是我便把他的测试用例每个单独运行,结果都对着
这为啥?
上面报错 是2100这年2月没有29天,但是我测试是发现结果为28天对着,这是为啥,
有没有大佬帮忙分析分析。万分感谢! import java.util.Scanner;
//NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
// 现在给你一段时间区间,请你帮他计算总收益有多少。
//思路结就是把不用的年转换为同一年计算+前面年的盈利 我们把开始和结束时间看作一年 那么这一年的盈利
//=开始月天数-开始日期盈利+开始月+1和结束月-1 之间月份盈利+结束月盈利 +相差n-1年的盈利=总盈利
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()) {
int year1 = scanner.nextInt();
int month1 = scanner.nextInt();
int day1 = scanner.nextInt();
int year2 = scanner.nextInt();
int month2 = scanner.nextInt();
int day2 = scanner.nextInt();
int years = 0;
long count = 0;
for (int i = 1; i <=12; i++) { //计算正常盈利一年挣多少 (不算闰年的时候)
if (isprime(i)) { //如果是素数
for (int j = 0; j < DAYS[i-1]; j++) {
years++;
}
} else {
for (int j = 0; j <DAYS[i-1]; j++) {
years += 2;
}
}
}
while (true) {
if (month1 == month2) { //如果月份相同 就直接后面日期减去前面日期 判断月份素数 加上结果
for (int i = 0; i <= day2 - day1; i++) { //=,记得加上本身那一天
if (isprime(month1)) {
count++;
} else {
count += 2;
}
}
break;
}
//把结果分成三份 一份是开始时间的月日 一份是开始月份和结束月份之间的盈利 一份最后结束月份时的 盈利
if (isprime(month1)) { //如果当前月是素月 那么这个月的天数-已经从几号开始 就代表这个月营业了多少天
for (int j = 0; j <= DAYS[month1 - 1] - day1; j++) {
count++;
}
} else { //如果不是素月就一天+1 这里需要等于 不要忘记第一天也算盈利
for (int j = 0; j<=DAYS[month1 - 1] - day1; j++) {
count += 2;
}
}
int m1 = month1; //这里算两个月之间的月份盈利
int m2 = month2;
for (int j = m1 + 1; j <= m2-1; j++) { //假如是1 月-12月 那么前面算了一月的 后面算12月的 这里计算
if (isprime(j)) { //2-11 整月的盈利就可以了
for (int k = 0; k < DAYS[j-1]; k++) { //如果当前月是素月 那么一次+1 记得数组下标-1
count++;
}
} else {
for (int k = 0; k < DAYS[j-1]; k++) {
count += 2;
}
}
}
if (isprime(month2)) { //这里计算结束月的盈利 判断最后一个月是不是素月
for (int j = 0; j < day2; j++) { //如果是素月就一次+1 一共加结束日次 就可以了
count++;
}
} else {
for (int j = 0; j < day2; j++) {
count += 2;
}
}
if (isleapYear(year2)) { //如果当前年是闰年的话 需要+1 代表2月多出的一天的盈利
if (month2 > 2) {
count = count + 1;
}
}
break; //三部分的和加起来就是这年的盈利
}
for (int i = year1; i < year2; i++) { //如果不在同一年 就从开始年遍历到结束年 每年的盈利就为正常盈利
//如果为闰年就结果+1 相当于从2000年-2998年 +2999年的盈利
if (isleapYear(i)) {
count += years+1;
} else {
count += years;
}
}
System.out.println(count);
}
}
private static boolean isprime(int n){ //判断素数
if(n<=3){
return n>1;
}
for (int i = 2; i <n; i++) {
if(n%i==0){
return false;
}
}
return true;
}
private static final int[] DAYS={31,28,31,30,31,30,31,31,30,31,30,31}; //平年的天数
private static boolean isleapYear(int year) { //判断闰年
if((year%4==0&&year%100!=0)||(year%400)==0){
return true;
}
return false;
}
}
import java.util.*;
/*
* 淘宝网店
*/
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()) {
int fromYear=sc.nextInt();
int fromMonth=sc.nextInt();
int fromDay=sc.nextInt();
int toYear=sc.nextInt();
int toMonth=sc.nextInt();
int toDay=sc.nextInt();
Set<Integer>set=new HashSet<>();//添加素数方便查找
set.add(2);set.add(3);set.add(5);
set.add(7);set.add(11);
int money=0;
int day=0;
if(fromYear==toYear) {
if(fromMonth==toMonth) {
money=sameMonth(fromDay, fromMonth, fromYear, toDay, toMonth, toYear, set);
}else {
money=sameYear(fromDay, fromMonth, fromYear, toDay, toMonth, toYear, set);
}
}else {//不同年
money+=sameYear(fromDay, fromMonth, fromYear, 31, 12, fromYear, set);//第一年
money+=sameYear(1, 1, toYear, toDay, toMonth, toYear, set);//最后一年
int temp=fromYear+1;
while( temp<toYear){
money+=sameYear(1, 1, temp, 31, 12, temp, set);
temp++;
}
}
System.out.println(money);
}
}
//是否是闰年
public static boolean isLeapYear(int year) {
return (year%4==0&&year%100!=0)||year%400==0;
}
//计算某个月多少天
public static int manyDay(int year,int month) {
switch(month) {
case 1:case 3:case 5 :
case 7:case 8: case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
default:
if(isLeapYear(year)==true) {
return 29;
}
else {
return 28;
}
}
}
//同年不同月
public static int sameYear(int fromDay,int fromMonth,int fromYear,int toDay,int toMonth,int toYear,Set<Integer>set) {
int money=0;
int day=0;
day=manyDay(fromYear, fromMonth)-fromDay+1;//初始月的天数,包括初始日期
if(set.contains(fromMonth)) {//是否是素月
money+=day*1;
}else {
money+=day*2;
}
day=toDay;//结束月的天数
if(set.contains(toMonth)) {
money+=day*1;
}else {
money+=day*2;
}
//中间月
for(int i=fromMonth+1;i<toMonth;i++) {
day=manyDay(fromYear, i);
if(set.contains(i)) {
money+=day*1;
}else {
money+=day*2;
}
}
return money;
}
//同年同月
public static int sameMonth(int fromDay,int fromMonth,int fromYear,int toDay,int toMonth,int toYear,Set<Integer>set) {
int money=0;
int day=0;
day=toDay-fromDay+1;//初始月的天数,包括初始日期
if(set.contains(fromMonth)) {//是否是素月
money+=day*1;
}else {
money+=day*2;
}
return money;
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String[] s = in.nextLine().split(" ");
int year1 = Integer.parseInt(s[0]);
int mouth1 = Integer.parseInt(s[1]);
int day1 = Integer.parseInt(s[2]);
int year2 = Integer.parseInt(s[3]);
int mouth2 = Integer.parseInt(s[4]);
int day2 = Integer.parseInt(s[5]);
System.out.println(get(year1, year2, mouth1, mouth2, day1, day2));
}
}
static int[] mouths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static int get(int year1, int year2, int mouth1, int mouth2, int day1, int day2) {
int res = 0;
while (year1 < year2) {
while (mouth1 <= 12) {
res += getM(year1, mouth1, day1);
mouth1++;
day1 = 1;
}
year1++;
mouth1 = 1;
day1 = 1;
}
while (mouth1 < mouth2) {
res += getM(year1, mouth1, day1);
mouth1++;
day1 = 1;
}
int k = day2 - day1 + 1;
if (mouth1 == 2 || mouth1 == 3 || mouth1 == 5 || mouth1 == 7 || mouth1 == 11) {
res += k;
} else {
res += 2 * k;
}
return res;
}
public static boolean getR(int year1) {
if (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) {
return true;
}
return false;
}
public static int getM(int year, int mouth,int day) {
int res = 0;
int k = mouths[mouth - 1] - day + 1;
if (mouth == 2 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 11) {
res += k;
if (getR(year) && mouth == 2) {
res += 1;
}
} else {
res += 2 * k;
}
return res;
}
} import java.util.*;
public class Main{
private static boolean isLeapYear(int y){
return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
private static int profitYear(int y){
return 2 * 31
+ 1 * 28
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 31
+ 2 * 30
+ 2 * 31
+ 1 * 30
+ 2 * 31
+ (isLeapYear(y) ? 1 : 0);
}
private static boolean isPrime(int m){
return m ==2 || m == 3 || m == 5 || m == 7 || m==11;
}
private static int profitThisYear(int y,int m,int d){
int profit = 0;
if(isPrime(m)){
profit = d;
}else{
profit = 2 * d;
}
while(--m > 0){
switch(m){
case 1: case 8: case 10: case 12:
profit+= 62;
break;
case 3: case 5: case 7:
profit += 31;
break;
case 4: case 6: case 9:
profit += 60;
break;
case 11:
profit += 30;
break;
default:
profit += (28 + (isLeapYear(y) ? 1 : 0));
break;
}
}
return profit;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int y1,m1,d1,y2,m2,d2;
int profit = 0;
while(sc.hasNext()){
y1 = sc.nextInt();
m1 = sc.nextInt();
d1 = sc.nextInt();
y2 = sc.nextInt();
m2 = sc.nextInt();
d2 = sc.nextInt();
profit = profitYear(y1) - profitThisYear(y1,m1,d1-1);
profit += profitThisYear(y2,m2,d2);
if(y1 == y2){
profit -= profitYear(y1);
}
for(int i = y1 + 1;i < y2;i++){
profit += profitYear(i);
}
System.out.println(profit);
}
sc.close();
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int year1 = in.nextInt();
int month1 = in.nextInt();
int day1 = in.nextInt();
int year2 = in.nextInt();
int month2 = in.nextInt();
int day2 = in.nextInt();
int income = 0;
// 开始年后收入(例如:2022-5-31 ~ 2022-12-31)
income = yearIncome(year1) - thisYearIncome(year1,month1,day1-1);
// 结束年前收入(例如:2022-1-1 ~ 2022-5-31)
income += thisYearIncome(year2,month2,day2);
// 如是同一年 减去当前年多算的收入(一年的)
if(year1 == year2){
income -= yearIncome(year1);
}
// 期间年收入
for (int i = year1 + 1; i < year2; i++) {
income += yearIncome(i);
}
System.out.println(income);
}
}
//计算从当前年的1月1号到当前年的month月day号的收益
private static int thisYearIncome(int year,int month,int day){
int mIncome = 0;
if(!isPrimeNumber(month)){
mIncome = day*2;
}else {
mIncome = day;
}
while (--month > 0){
switch(month){
case 1: mIncome += 31*2;
break;
case 2: mIncome += (28 + (isLeapYear(year) ? 1 : 0));
break;
case 3: mIncome += 31;
break;
case 4: mIncome += 30*2;
break;
case 5: mIncome += 31;
break;
case 6: mIncome += 30*2;
break;
case 7: mIncome += 31;
break;
case 8: mIncome += 31*2;
break;
case 9: mIncome += 30*2;
break;
case 10: mIncome += 31*2;
break;
case 11: mIncome += 30;
break;
case 12: mIncome += 31*2;
break;
}
}
return mIncome;
}
//判断某月是否是素数
private static boolean isPrimeNumber(int month){
if(month == 2 || month == 3 || month == 5 || month == 7 || month == 11){
return true;
}else {
return false;
}
}
//判断某年一共有多少收益
private static int yearIncome(int year){
return 2 * 31 + 1 * 28 + 1 * 31 + 2 * 30 + 1 * 31 + 2 * 30 + 1 * 31 + 2 * 31 + 2 * 30 + 2 * 31 + 1 * 30 + 2 * 31
+ (isLeapYear(year) ? 1 : 0);
}
//判断是否是闰年
private static boolean isLeapYear(int year){
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
return true;
}else{
return false;
}
}
}
import java.util.*;
public class Main{
//判断是否是闰年
public static boolean isLeapYear(int n){
if((n % 4 == 0 && n % 100 != 0) || n % 400 == 0){
return true;
}
return false;
}
//判断某一年一共有多少收益
public static int profitofYear(int year){
return 2 * 31
+ 1 * 28
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 30
+ 1 * 31
+ 2 * 31
+ 2 * 30
+ 2 * 31
+ 1 * 30
+ 2 * 31
+ (isLeapYear(year) ? 1 : 0);
}
//判断某一月是否是素数
public static boolean isPrime(int month){
return (month == 2 || month == 3 || month == 5 || month== 7 || month == 11);
}
//计算从year年的1月1号到year年的month月day号的收益
public static int profitodThisyear(int year,int month,int day){
int profit = 0;
if(!isPrime(month)){
profit = day * 2;
}else{
profit = day;
}
while (--month > 0) {
switch (month) {
case 1: case 8: case 10: case 12 :
profit += 62;
break;
case 3: case 5: case 7:
profit += 31;
break;
case 4 : case 6 : case 9:
profit += 60;
break;
case 11:
profit += 30;break;
default:
profit += (28 + (isLeapYear(year) ? 1 : 0));
break;
}
}
return profit;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int profit = 0;
int year1 = sc.nextInt();
int month1 = sc.nextInt();
int day1 = sc.nextInt();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int day2 = sc.nextInt();
profit = profitofYear(year1) - profitodThisyear(year1,month1,day1 - 1);
profit += profitodThisyear(year2,month2,day2);
if(year1 == year2){
profit -= profitofYear(year1);
}
for(int i = year1 + 1;i < year2;i++){
profit += profitofYear(i);
}
System.out.println(profit);
}
}
} import java.util.*;
public class Main{
public static int month(int year,int month) {//判断这年这个月共多少天
if (month == 2) {
//判断年是不是闰年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
public static int func(int m) {//判断这个月是不是素数月,每天能拿多少钱
//素数月有2,3,5,7,11
int x=0;
if (m == 2 || m == 3 || m == 5 || m == 7 || m == 11) {//素数月赚1快
x = 1;
} else {
x = 2;
}
return x;
}
public static void main (String[]args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int y1 = scanner.nextInt();
int m1 = scanner.nextInt();
int d1 = scanner.nextInt();
int y2 = scanner.nextInt();
int m2 = scanner.nextInt();
int d2 = scanner.nextInt();
int sum = 0;//标记赚的钱
if (y1 - y2 == 0) {//同一年
for (int m = m1; m <= m2; m++) {
int x = func(m);
if (m1 != m2) {
if (m == m1) {//第一月
sum += (month(y1, m1) - d1 + 1) * x;
} else if (m > m1 && m < m2) {//中间月
sum += month(y1, m) * x;
} else {//最后一月
sum += d2 * x;
}
} else {
sum += (d2 - d1 + 1) * x;
}
}
} else {//不同年
for (int m = m1; m <= 12; m++) {//第一年
int x = func(m);
if (m == m1) {
sum += (month(y1, m1) - d1 + 1) * x;
} else {
sum += month(y1, m) * x;
}
}
if (y2 - y1 > 1) {
int tmpYear = y1 + 1;
while (tmpYear < y2) {
for (int m = 1; m <= 12; m++) {
int x = func(m);
sum += month(tmpYear, m)*x;
}
tmpYear++;
}
}
for (int m = 1; m < m2; m++) {//最后一年
int x = func(m);
sum += month(y2, m) * x;
}
int x = func(m2);
sum += d2*x;
}
System.out.println(sum);
}
}
} // write your code here
import java.util.*;
public class Main{
//判断是否是闰年
public static boolean isLeapYear(int year){
return (year%4==0&&year%100!=0)||year%400==0;
}
//一整年的收益
public static int profitYear(int year){
return 2*31
+1*28
+1*31
+2*30
+1*31
+2*30
+1*31
+2*31
+2*30
+2*31
+1*30
+2*31
+(isLeapYear(year)?1:0);
}
//判断月份是否是素数
public static boolean isPrime(int month){
return month==2||month==3||month==5||month==7||month==11;
}
//本年具体日期的收益
public static int profitThisyear(int year,int month,int day){
int profit=0;
if(isPrime(month)){
profit=day;
}else{
profit=2*day;
}
while(--month>0){
switch(month){
case 1:case 8:case 10:case 12:
profit+=62;
break;
case 3:case 5:case 7:
profit+=31;
break;
case 4:case 6:case 9:
profit+=60;
break;
case 11:
profit+=30;
break;
default:
profit+=(28+(isLeapYear(year)?1:0));
break;
}
}
return profit;
}
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
int profit=0;
int startYear=scanner.nextInt();
int startMonth=scanner.nextInt();
int startDay=scanner.nextInt();
int endYear=scanner.nextInt();
int endMonth=scanner.nextInt();
int endDay=scanner.nextInt();
profit=profitYear(startYear)-profitThisyear(startYear,startMonth,startDay-1);
profit+=profitThisyear(endYear,endMonth,endDay);
if(startYear==endYear){//年份相同的情况
profit-=profitYear(startYear);
}
for(int i=startYear+1;i<endYear;i++){
profit+=profitYear(i);
}
System.out.println(profit);
}
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] m = {31*2,28,31,30*2,31,30*2,31,31*2,30*2,31*2,30,31*2};
while(sc.hasNext()){
int fromYear = sc.nextInt();
int fromMonth = sc.nextInt();
int fromDay = sc.nextInt();
int toYear = sc.nextInt();
int toMonth = sc.nextInt();
int toDay = sc.nextInt();
if(toYear - fromYear == 0){
System.out.println(func(fromYear,fromMonth,fromDay,toMonth,toDay,m));
}else{
int temp = 0;
int sum = func(fromYear,fromMonth,fromDay,12,31,m);
sum += func(toYear,1,1,toMonth,toDay,m);
m[1] = 28;
for(int n : m){
temp += n;
}
for(int i = fromYear + 1;i < toYear;i++){
if(func(i)){
sum += temp + 1;
}else{
sum += temp;
}
}
System.out.println(sum);
}
}
}
private static int func (int year , int fromMonth , int fromDay , int toMonth , int toDay , int[] m){
func(year,m);
if(fromMonth == toMonth){
return m[fromMonth - 1] > 31 ? (toDay + 1 - fromDay) * 2 : toDay + 1 - fromDay;
}else{
int sum = m[fromMonth - 1] + (m[fromMonth-1] > 31 ? 2 : 1) - (m[fromMonth - 1] > 31 ? 2 * fromDay : fromDay);
sum += m[toMonth-1] > 31 ? 2 * toDay : toDay;
for(int i = fromMonth;i < toMonth-1;++i){
sum += m[i];
}
return sum;
}
}
private static void func (int y,int[] m){
if(func(y)){
m[1] = 29;
}else{
m[1] = 28;
}
}
private static boolean func (int y){
return y % 4 == 0 && (y % 100 != 0 || (y % 400 == 0));
}
}
// write your code here cpp
#include<iostream>
using namespace std;
bool isleapyear(int y)
{
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
{
return true;
}
else
{
return false;
}
}
int main()
{
int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int isprime[13] = { 0,0,1,1,0,1,0,1,0,0,0,1,0 };
int year1, month1, day1;
int year2, month2, day2;
while (cin >> year1 >> month1 >> day1 >> year2 >> month2 >> day2)
{
int sum = 0;
int m = month1, d = day1;
while (year1 < year2)
{
if (isleapyear(year1))
{
monthday[2]=29;
}
for (; m <= 12; m++)
{
for (; d <= monthday[m]; ++d)
{
sum += 2 - isprime[m];
}
d = 1;
}
m = 1;
year1++;
if (monthday[2] == 29)
{
monthday[2]--;
}
}
//同一年
if (isleapyear(year2))
{
monthday[2]++;
}
for (; m <month2; m++)
{
for (; d <= monthday[m]; ++d)
{
sum += 2 - isprime[m];
}
d = 1;
}
//同一月
for (; d <= day2; ++d)
{
sum += 2 - isprime[m];
}
cout << sum << endl;
}
return 0;
}