输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
2000 3 2000 31 2000 40 2000 60 2000 61 2001 60
2000-01-03 2000-01-31 2000-02-09 2000-02-29 2000-03-01 2001-03-01
#include<stdio.h>//1.判断闰年2.找月数,累减n int main() { int y,n,month,day,run,i; int a[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}}; scanf("%d%d",&y,&n); if(y%4==0&&y%100!=0||y%400==0) run=1;//判断闰年 else run=0; for(i=1;i<=12;i++) { if(n>a[run][i])//天数大于整月 n-=a[run][i];//减去满月的天数 else{//不大于这个月的天数 month=i;day=n;break;} } printf("%d-%02d-%02d",y,month,day);//格式控制两位有效数字不够的话前补0 }
#include<stdio.h> (737)#define ISLEAPYEAR(x) x%4==0 && x%100!=0 || x%400==0 ? 1 : 0 #define ABS(x) x>0 ? x : -x int daysOfMonth[13][2] = { 0,0, 31,31, 28,29, 31,31, 30,30, 31,31, 30,30, 31,31, 31,31, 30,30, 31,31, 30,30, 31,31 }; struct Date { int year; int month; int day; void nextDay() { day++; if (day > daysOfMonth[month][ISLEAPYEAR(year)]) { day = 1; month++; if (month > 12) { month = 1; year++; } } } }; Date dateCalc[2000000]; int main() { Date temp; temp.year = 0; temp.month = 1; temp.day = 1; int cnt = 0; while (cnt < 2000000) { temp.nextDay(); dateCalc[cnt].year = temp.year; dateCalc[cnt].month = temp.month; dateCalc[cnt].day = temp.day; cnt++; } int year, month=1, day=1, i_year, i_days, count=0; while (scanf("%d%d", &i_year, &i_days) != EOF) { int ctrl = 0; count = i_year * 365; while (dateCalc[count++].year != i_year); year = dateCalc[count + i_days - 2].year; month = dateCalc[count + i_days - 2].month; day = dateCalc[count + i_days - 2].day; printf("%04d-%02d-%02d", year, month, day); } return 0; }
import java.text.DecimalFormat; import java.time.LocalDate; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int year = scanner.nextInt(); int days = scanner.nextInt(); LocalDate day = LocalDate.ofYearDay(year, days); DecimalFormat f = new DecimalFormat("00"); System.out.println(day.getYear()+"-"+f.format(day.getMonth().getValue())+"-"+f.format(day.getDayOfMonth())); } } }
#include <bits/stdc++.h> using namespace std; bool isrunnian(int y) { return (y%4==0&&y%100!=0)||(y%400==0); } int main() { int y,m,d,n; int date[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} }; while(cin>>y>>n) { m=1; // d=0; for(int i=1; i<=12; i++) { if(n-date[isrunnian(y)][i]<=0) { printf("%d-%02d-%02d\n",y,m,n); break; } else { m++; n-=date[isrunnian(y)][i]; } } } return 0; }
#include <iostream> #include <iomanip> using namespace std; bool is_rui(int year){ return year%400==0||((year%100!=0&&year%4==0)); } int main(){ int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int pin[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int year,month=0,day; while(cin>>year>>day){ if(is_rui(year)){ while(day-rui[month]>0){ day-=rui[month]; ++month; } cout<<year<<'-'<<setw(2)<<setfill('0')<<++month<<'-'<<setw(2)<<setfill('0')<<day<<endl; }else{ while(day-pin[month]>0){ day-=pin[month]; ++month; } cout<<year<<'-'<<setw(2)<<setfill('0')<<++month<<'-'<<setw(2)<<setfill('0')<<day<<endl; } } return 0; }
#include<stdio.h> int month[13][2] = { {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30}, {31,31},{31,31},{30,30},{31,31},{30,30},{31,31} }; int isLeap(int year){ return (year % 4 == 0 && year%100 != 0)||(year%400 == 0); } int main(){ int year,n; while(scanf("%d %d",&year,&n)!=EOF){ int i; for (i=0; i<13; i++ ){ if(n > month[i][isLeap(year)]) n -= month[i][isLeap(year)]; else break; } printf("%04d-%02d-%02d\n",year,i,n); } }
#include<iostream> #include<cstring> using namespace std; int main() { int y, n; while (cin >> y >> n) { int sum = 0, date = 0, m = 0; int mon[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; if (y % 100 == 0 && y % 400 == 0 || y % 100 != 0 && y % 4 == 0) mon[1] = 29; for (int i = 0; i < 12; i++) { sum += mon[i]; if (sum >= n) { m = i + 1; sum -= mon[i]; date = n - sum; break; } } printf("%04d-%02d-%02d\n", y, m, date); } }
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { int year, day; while (cin >> year >> day) { int d[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) d[2]++; int month = 1; while ((day - d[month]) > 0) { day -= d[month++]; } printf("%d-%02d-%02d\n", year, month, day); } }
package com.speical.first;
import java.util.Scanner;
/**
* 给定年份和天数
*
* 给出是当年那月那日
*
* 注意闰年,提前累加好天数简单点
* 放假了,学校暖气不给力了啊,手好冷,md
* @author Special
* @time 2018/02/10 17:46:57
*/
public class Pro219 {
static int[] months = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
public static boolean isLoopYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
int year = input.nextInt();
int days = input.nextInt();
boolean isLoop = isLoopYear(year);
int month = 1;
for(month = 1; month < 13; month++) {
if(days <= months[month] + (isLoop && month >= 2 ? 1 : 0)) {
break;
}
}
int day = days - (months[month - 1] + (isLoop && month - 1 >= 2 ? 1 : 0));
System.out.println(year + "-" + (month >= 10 ? "" : "0") + month + "-"
+ (day >= 10 ? "" : "0") + day);
}
}
}
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cld = Calendar.getInstance(); while (in.hasNext()) { cld.set(in.nextInt(), 0, in.nextInt()); System.out.println(sdf.format(cld.getTime())); } } }
#!/usr/bin/env python #encoding:utf-8 import sys class test(object): def __init__(self,year,days): self.dict = [31,28,31,30,31,30,31,31,30,31,30,31] self.year = year self.days = int(days) self.month = 0 self.day = 0 def j_run(self): if (self.year %400 ==0) or (self.year %4 ==0 and self.year %100 !=0): # print 'leap year' return 1 #leap year return 0 # nonleap year def start(self): if self.j_run(): self.dict[1] = 29 # Leap year has 29 days in February # print self.dict for a in range(12): self.month = 0 self.day = 0 if self.days > self.dict[a]: self.days-=self.dict[a] else: self.month,self.day = a+1,self.days break r ='%04d-%02d-%02d' % (self.year,self.month,self.day) return r for line in sys.stdin: nums=line.strip().split(' ') yy=int(nums[0]) n=int(nums[1]) a = test(yy,n) print a.start()
用数组记录每个月份下累计日数的边界值
搜索判断当前日数位于哪个区间
搜索初始位置设置为 day/30 , 这样大部分情况下只需要一次判断就能得出答案
#include <stdio.h> int isLeapYear(int year) { if(year%4==0 && year%100!=0 || year%400==0) return 1; else return 0; } int main(void) { int months_day_max[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365}; //int months_day_max[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366}; int year; int day; int month; int leap; //////////////////////////////////////////////////////////////////////// while(scanf("%d %d", &year, &day) != EOF) { if(day <= 31) { month = 1; } else { leap = isLeapYear(year); if(leap) months_day_max[1] = 30; else months_day_max[1] = 31; int f = day / 30; while(1) { int front = months_day_max[f - 1] + leap; int current = months_day_max[f] + leap; int back = months_day_max[f + 1] + leap; if(day <= front) { f--; } else if(day > front && day <= current) { month = f; day = day % front; break; } else if(day > current && day <= back){ month = f + 1; day = day % current; break; } else { f++; } } } printf("%04d-%02d-%02d\n", year, month, day); } return 0; }
<?php $a = [0,31,28,31,30,31,30,31,31,30,31,30,31];//非润年 $b = [0,31,29,31,30,31,30,31,31,30,31,30,31];//闰年 while(fscanf(STDIN,"%d %d",$year,$day)){ $month = 0; if($year%4==0&&$year%100!=0||$year%400==0){ while($day>$b[$month]){ $day -= $b[$month]; $month++; } printf("%04d-%02d-%02d\n",$year,$month,$day); }else{ while($day>$a[$month]){ $day -= $a[$month]; $month++; } printf("%04d-%02d-%02d\n",$year,$month,$day); } }
#include <iostream> using namespace std; int ping[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int run[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool isPing( int year ) { if( year % 400 == 0 ) return false; else if( year % 4 == 0 && year % 100 != 0 ) return false; return true; } int main() { int year, month, day, n; while( cin >> year >> n ) { if( isPing(year) ) { month = 0; while( n>0 ) { day = n; n -= ping[month]; month++; } } else { month = 0; while( n>0 ) { day = n; n -= run[month]; month++; } } if( month < 10 && day >= 10 ) cout << year << "-0" << month << "-" << day << endl; else if( month >= 10 && day < 10) cout << year << "-" << month << "-0" << day << endl; else if( month < 10 && day < 10 ) cout << year << "-0" << month << "-0" << day << endl; else cout << year << "-" << month << "-" << day << endl; } return 0; }
#include<iostream> using namespace std; bool isleap(int year) { if((year%4==0&&year%100!=0)||(year%400==0)) return true; return false; } void getDay(int year,int num) { bool flag=isleap(year); int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int y[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int sum=0; if(flag) { for(int i=0;i<12;i++) { sum+=leap[i]; if(sum>=num) { if(i+1>=10) { if((leap[i]-sum+num)>=10) cout<<year<<"-"<<i+1<<"-"<<(leap[i]-sum+num)<<endl; else cout<<year<<"-"<<i+1<<"-0"<<(leap[i]-sum+num)<<endl; } else { if(leap[i]-sum+num>=10) cout<<year<<"-0"<<i+1<<"-"<<(leap[i]-sum+num)<<endl; else cout<<year<<"-0"<<i+1<<"-0"<<(leap[i]-sum+num)<<endl; } break; } } } else { for(int i=0;i<12;i++) { sum+=y[i]; if(sum>=num) { if(i+1>=10) { if((y[i]-sum+num)>=10) cout<<year<<"-"<<i+1<<"-"<<(y[i]-sum+num)<<endl; else cout<<year<<"-"<<i+1<<"-0"<<(y[i]-sum+num)<<endl; } else { if(y[i]-sum+num>=10) cout<<year<<"-0"<<i+1<<"-"<<(y[i]-sum+num)<<endl; else cout<<year<<"-0"<<i+1<<"-0"<<(y[i]-sum+num)<<endl; } break; } } } } int main() { int year,num; while(cin>>year>>num) { getDay(year,num); } return 0; }
/** 只要建立一个数组,用于查表就好,把闰年的情况独立出来,也就是说把二月改一下即可。 而且天数只会最多有366天,也就是加一年而已,完全不用再考虑第二年情况!! */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ fun(in.nextInt(), in.nextInt()); } in.close(); } public static void fun(int a, int b){ int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(a % 4 == 0 && a % 100 != 0 || a % 400 == 0) month[1] = 29; int i; for(i = 0; i < 12; i++){ if(b <= month[i]) break; else b -= month[i]; } if(i == 11 && b > 31) a++; String str = String.format("%d-%02d-%02d",a, i+1, b); System.out.println(str); } }
建立两个数组分别存放闰年和非闰年的各个月份对应天数。
#include <cstdio> using namespace std; int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}, b[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; int main(){ int year, day, month; while(scanf("%d%d", &year, &day) != EOF){ month=0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) while (day > b[month]) day -= b[month++]; else while (day > a[month]) { day -= a[month++]; printf("%04d-%02d-%02d\n", year, month, day); }return 0; }
思路:
1. 先判断输入的年是闰年还是平年,闰年2月是29天,平年二月是28天
闰年的判定方法:
---能被4整除,不能被100整除 或者是 ---能被400整除
2. 定位是哪一个月
(1).定义一个天数数组,遍历天数数组,
(2).如果(n - 当前月的天数) >0
n -= 当前月的天数,跳到下一个月
直到 n<= 当前月的天数
3. 定位到月之后,再判断是哪一天,在第二步得到n的值即可
#include <iostream>
using namespace std;
void printDate(int year, int month, int day)
{
cout << year << '-' ;
if (month < 10)
{
cout << '0' << month << '-' ;
}
else cout << month << '-' ;
if (day < 10)
{
cout << '0' << day << endl;
}
else cout << day << endl;
}
int main()
{
static char daytab[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 }
};
int year, day_of_year, leap, i;
while (cin >> year >> day_of_year)
{
leap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
for (i = 0; i < 13; i++)
{
if (day_of_year > daytab[leap][i])
{
day_of_year -= daytab[leap][i];
}
else break;
}
if (i == 13) i -= 12;
printDate(year, i, day_of_year);
}
return 0;
}
最笨的方法就是跟我一样,纯属新手练习,不笑。 #include<iostream> using namespace std; int main() { int m,n,month_2; while(cin>>m>>n) { if(((m%4==0)&&(m%100!=0))||(m%400==0)) month_2=29; else month_2=28; if(n<=31) { if(n>9) cout<<m<<"-"<<"01"<<"-"<<n<<endl; else cout<<m<<"-"<<"01"<<"-0"<<n<<endl; } if((n>31)&&(n<=31+month_2)) {if(n-31>9) cout<<m<<"-"<<"02"<<"-"<<n-31<<endl; else cout<<m<<"-"<<"02"<<"-0"<<n-31<<endl; } if((n>31+month_2)&&(n<=31+month_2+31)) {if(n-31-month_2>9) cout<<m<<"-"<<"03"<<"-"<<n-31-month_2<<endl; else cout<<m<<"-"<<"03"<<"-0"<<n-31-month_2<<endl; } if((n>31+month_2+31)&&(n<=31+month_2+31+30)) {if(n-31-month_2-31>9) cout<<m<<"-"<<"04"<<"-"<<n-31-month_2-31<<endl; else cout<<m<<"-"<<"04"<<"-0"<<n-31-month_2-31<<endl; } if((n>31+month_2+31+30)&&(n<=31+month_2+31+30+31)) {if(n-31-month_2-31-30>9) cout<<m<<"-"<<"05"<<"-"<<n-31-month_2-31-30<<endl; else cout<<m<<"-"<<"05"<<"-0"<<n-31-month_2-31-30<<endl; } if((n>31+month_2+31+30+31)&&(n<=31+month_2+31+30+31+30)) { if(n-31-month_2-31-30-31>9) cout<<m<<"-"<<"06"<<"-"<<n-31-month_2-31-30-31<<endl; else cout<<m<<"-"<<"06"<<"-0"<<n-31-month_2-31-30-31<<endl; } if((n>31+month_2+31+30+31+30)&&(n<=31+month_2+31+30+31+30+31)) { if(n-31-month_2-31-30-31-30>9) cout<<m<<"-"<<"07"<<"-"<<n-31-month_2-31-30-31-30<<endl; else cout<<m<<"-"<<"07"<<"-0"<<n-31-month_2-31-30-31-30<<endl; } if((n>31+month_2+31+30+31+30+31)&&(n<=31+month_2+31+30+31+30+31+31)) { if(n-31-month_2-31-30-31-30-31>9) cout<<m<<"-"<<"08"<<"-"<<n-31-month_2-31-30-31-30-31<<endl; else cout<<m<<"-"<<"08"<<"-0"<<n-31-month_2-31-30-31-30-31<<endl; } if((n>31+month_2+31+30+31+30+31+31)&&(n<=31+month_2+31+30+31+30+31+31+30)) { if(n-31-month_2-31-30-31-30-31-31>9) cout<<m<<"-"<<"09"<<"-"<<n-31-month_2-31-30-31-30-31-31<<endl; else cout<<m<<"-"<<"09"<<"-0"<<n-31-month_2-31-30-31-30-31-31<<endl; } if((n>31+month_2+31+30+31+30+31+31+30)&&(n<=31+month_2+31+30+31+30+31+31+30+31)) {if(n-31-month_2-31-30-31-30-31-31-30>9) cout<<m<<"-"<<"10"<<"-"<<n-31-month_2-31-30-31-30-31-31-30<<endl; else cout<<m<<"-"<<"10"<<"-0"<<n-31-month_2-31-30-31-30-31-31-30<<endl; } if((n>31+month_2+31+30+31+30+31+31+30+31)&&(n<=31+month_2+31+30+31+30+31+31+30+31+30)) { if(n-31-month_2-31-30-31-30-31-31-30-31>9) cout<<m<<"-"<<"11"<<"-"<<n-31-month_2-31-30-31-30-31-31-30-31<<endl; else cout<<m<<"-"<<"11"<<"-0"<<n-31-month_2-31-30-31-30-31-31-30-31<<endl; } if((n>31+month_2+31+30+31+30+31+31+30+31+30)&&(n<=31+month_2+31+30+31+30+31+31+30+31+30+31)) {if(n-31-month_2-31-30-31-30-31-31-30-31-30>9) cout<<m<<"-"<<"12"<<"-"<<n-31-month_2-31-30-31-30-31-31-30-31-30<<endl; else cout<<m<<"-"<<"12"<<"-0"<<n-31-month_2-31-30-31-30-31-31-30-31-30<<endl; } } }