今年的第几天?
输入年、月、日,计算该天是本年的第几天。
import datetime
while True:
try:
y, m, d = map(int, input().split())
date = datetime.datetime(y, m, d)
print(date.strftime("%j").lstrip("0"))
except:
break
需要注意的是:
可以写成一行:
import datetime
while True:
try:
print(datetime.datetime(*map(int, input().split())).strftime("%j").lstrip("0"))
except:
break
import java.util.Scanner;
/**
* 计算一年中的第几天
* 今年的第几天?
* 输入年、月、日,计算该天是本年的第几天。
* 输入描述:
* 包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
* 输出描述:
* 输入可能有多组测试数据,对于每一组测试数据,
* 输出一个整数,代表Input中的年、月、日对应本年的第几天。
* 输入例子1:
* 1990 9 20
* 2000 5 1
* 输出例子1:
* 263
* 122
*
* @author shijiacheng
* @date 2018/2/21
*/
public class DayOfYeafSolution {
public static void main(String[] args){
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int Y = sc.nextInt();
int M = sc.nextInt();
int D = sc.nextInt();
if (isLeapYear(Y)){
days[1] = days[1]+1;
}
int day = 0;
for (int i = 1; i < M; i++) {
day+=days[i-1];
}
day +=D;
System.out.println(day);
}
}
/**
* 判断是否是闰年
* @param year
* @return
*/
public static boolean isLeapYear(int year){
if (year%4==0&&year%100!=0){
return true;
}else if (year%400==0){
return true;
}
return false;
}
}
#include <iostream> #include <vector> using namespace std; int main(void) { int year, month, day; while (cin >> year >> month >> day) { int count = 0; int Feb = 28; if (year % 4 == 0 || year % 400 == 0) { Feb = 29; } vector<int> monthcount = { 0,31,Feb,31,30,31,30,31,31,30,31,30,31 }; for (int i = 0; i < month; i++) { count += monthcount[i]; } count += day; cout << count << endl; } return 0; }
傻瓜解法 #include <iostream> using namespace std; int main() { int d1[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int d2[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int year,month,day; while(cin>>year>>month>>day) { int num=0; if((year % 4 == 0 && year % 100 != 0)||(year%400 ==0)) { for(int i=0;i<month-1;i++) { num += d1[i]; } num=num+day; cout<<num<<endl; } else { for(int i=0;i<month-1;i++) { num += d2[i]; } num=num+day; cout<<num<<endl; } } return 0; } ps: 请教一下,请看到的大神解答一下,为什么下面这个不行#include <iostream> using namespace std; int main() { int d1[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int d2[12]={31,28,31,30,31,30,31,31,30,31,30,31}; while(cin) { int year,month,day; int num=0; cin>>year>>month>>day; if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { for(int i=0;i<month-1;i++) { num += d1[i]; } num=num+day; } else { for(int i=0;i<month-1;i++) { num += d2[i]; } num=num+day; } cout<<num<<endl; } return 0; }
import java.util.*; public class Main { private static final int[] M_DAYS = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int year = sc.nextInt(); int month = sc.nextInt(); int day = sc.nextInt(); if ( (year % 400 == 0) || (year % 4 == 0 && year % 100 !=0) ) { M_DAYS[2] = 29; } int ans = 0; for (int i=1; i<month; i++) { ans += M_DAYS[i]; } ans += day; System.out.println(ans); M_DAYS[2] = 28; } return; } }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class TestDate { public static void main(String[] argvs) throws ParseException { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String dateStr=sc.nextLine(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy MM dd"); Date date= null; date=sdf.parse(dateStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int index=calendar.get(Calendar.DAY_OF_YEAR); System.out.println(index); } } }
import java.util.Scanner;
public class Ymd {
Scanner i=new Scanner(System.in);
int Y=i.nextInt();
int M=i.nextInt();
int D=i.nextInt();
int k;
public void jisuan() {
int s;
if((Y/400)%2==0||(Y/400)%2==1) {
s=29;
}
else {
s=28;
}
k=s+D+(M-2)*30+(M-1)/2;
System.out.println(k);
}
public static void main(String[]args) {
Ymd test=new Ymd();
test.jisuan();
}
}
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int[] arrs=new int[3]; for(int i=0;i<3;i++){ arrs[i]=sc.nextInt(); } System.out.println(findDay(arrs)); } } private static int findDay(int[] arrs) { int sum=0; int n=arrs[1]-1; while(n>0){ if(n==1||n==3||n==5||n==7||n==8||n==10||n==12){ sum+=31; }else if(n==2){ sum+=isRun(arrs[0])?29:28; }else{ sum+=30; } n--; } sum+=arrs[2]; return sum; } private static boolean isRun(int i) { if(i%400==0){ return true; } if(i%4==0&&i%100!=0){ return true; } return false; } }
#include<stdio.h> int main(void) { int year = 0, month = 0, day = 0, sum = 0; int month_num[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int i = 0; while(scanf("%d%d%d",&year, &month, &day) != EOF) { for(i=0; i<month-1; i++) { sum += month_num[i]; } sum += day; if(month>=3) { if(((year%4==0)&&(year%100!=0)) || (year%400==0)) { sum++; } } printf("%d\n",sum); } }
#include<iostream> #include<string> #include<sstream> #include<queue> #include<utility> #include<set> #include<vector> #include<map> #include<stack> #include<algorithm> #include<unordered_map> using namespace std; int main() { int year,month,day; while(cin>>year>>month>>day){ map<int,int> Month; Month[1]=31; Month[2]=(year%4==0)?29:28; Month[3]=31; Month[4]=30; Month[5]=31; Month[6]=30; Month[7]=31; Month[8]=31; Month[9]=30; Month[10]=31; Month[11]=30; Month[12]=31; if(1==month){ cout<<day<<endl; }else{ int cnt=0; for(int i=1;i<month;++i){ cnt+=Month[i]; } cout<<cnt+day<<endl; } } return 0; }
#include<iostream> using namespace std; int main() { int base[2][12]={{ 0, 31, 60, 91,121,152,182,213,244,274,305,335}, { 0, 31, 59, 90,120,151,181,212,243,273,304,334}}; int Y, M, D; int flag; while(cin >> Y >> M >> D) { if(((Y%4 == 0)&&(Y%100 != 0))||(Y%400== 0))//闰年 flag = 0; else flag = 1; cout << base[flag][M-1] + D; } return 0; }
import java.util.*; public class Main8{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int Y = input.nextInt(); int M = input.nextInt(); int D = input.nextInt(); int[] Yes = {31,29,31,30,31,30,31,31,30,31,30,31}; int[] No = {31,28,31,30,31,30,31,31,30,31,30,31}; int total = 0; if((Y%4==0&&Y%100!=0)||Y%400 == 0){ for(int i =0;i<M-1;i++){ total = total+Yes[i]; } total +=D; } else{ for(int i =0;i<M-1;i++){ total = total+No[i]; } total +=D; } System.out.println(total); } }