1 条题解

  • 0
    @ 2025-4-12 21:45:14

    C :

    #include<stdio.h>
    
    int leap(int y)
    {
    	return y%4==0&&y%100!=0||y%400==0?1:0;
    }
    
    int main()
    {
    	int t,y,m,d,i,s;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d-%d-%d",&y,&m,&d);
    		s=6570;
    		if(m==2&&d==29)
    		{
    			if(!leap(y+18))
    				printf("-1\n");
    			else
    			{
    				for(i=y+1;i<=y+18;i++)
    					if(!leap(i))
    						s--;
    				printf("%d\n",s);
    			}
    		}
    		else
    		{
    			if(m<=2)
    			{
    				for(i=y;i<y+18;i++)
    					if(leap(i))
    						s++;
    			}
    			else
    				for(i=y+1;i<=y+18;i++)
    					if(leap(i))
    						s++;
    			printf("%d\n",s);
    		}
    	}
    	return 0;
    }
    

    C++ :

    #include<stdio.h>
    
    int leap(int y)
    {
    	return y%4==0&&y%100!=0||y%400==0?1:0;
    }
    
    int main()
    {
    	int t,y,m,d,i,s;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d-%d-%d",&y,&m,&d);
    		s=6570;
    		if(m==2&&d==29)
    		{
    			if(!leap(y+18))
    				printf("-1\n");
    			else
    			{
    				for(i=y+1;i<=y+18;i++)
    					if(!leap(i))
    						s--;
    				printf("%d\n",s);
    			}
    		}
    		else
    		{
    			if(m<=2)
    			{
    				for(i=y;i<y+18;i++)
    					if(leap(i))
    						s++;
    			}
    			else
    				for(i=y+1;i<=y+18;i++)
    					if(leap(i))
    						s++;
    			printf("%d\n",s);
    		}
    	}
    	return 0;
    }
    

    Pascal :

    var n,b,c,d,e,s,i,j,x:integer;a:string;
    begin
     readln(n);
     for i:=1 to n do
      begin
       readln(a);
       val(copy(a,1,4),b,x);
       val(copy(a,6,2),c,x);
       val(copy(a,9,2),d,x);
       if (c=2)and(d=29) then writeln('-1') else
        begin
         s:=6570;
         if (c<=2)and((b mod 400=0)or((b mod 4=0)and(b mod 100<>0)))then s:=s+1;
         for j:=b+1 to b+17 do
          if (j mod 400=0)or((j mod 4=0)and(j mod 100<>0))then s:=s+1;
         if (c>2)and(((b+18) mod 400=0)or(((b+18) mod 4=0)and((b+18) mod 100<>0)))then s:=s+1;
         writeln(s)
        end
      end
    end.
    

    Java :

    public class Main {
    public static void main(String[] args) {
    	java.util.Scanner in = new java.util.Scanner(System.in);
    	while (in.hasNext()) {
    		int N=in.nextInt();
    		for(int i=0;i<N;i++) {
    			String [] a=in.next().split("-");
    			int b=Integer.parseInt(a[0]);
    			int c=Integer.parseInt(a[1]);
    			int d=Integer.parseInt(a[2]);
    			int sum=0;
    			if(c==2&&d==29)
    				if((b%4==0&&b%100!=0)||b%400==0) {
    					System.out.println(-1);continue;}
    			for(int n=b;n<=b+17;n++) {
    				if((n%4==0&&n%100!=0)||n%400==0) {
    					sum=sum+366;
    				}
    				else
    					sum=sum+365;
    			}	
    				int n=b;int x=0;
    				for(int j=1;j<c;j++) {
    					if(j==1||j==3||j==5||j==7||j==8||j==10||j==12) {
    						x=x+31;
    					}
    					if(j==4||j==6||j==9||j==11) {
    						x=x+30;
    					}
    					if(j==2) {
    					if((n%4==0&&n%100!=0)||n%400==0){
    						x=x+29;
    					}
    					else 
    						x=x+28;
    					}
    			}
    				x=x+d;
    				sum=sum-x;
    			int f=b+18;
    			for(int j=1;j<c;j++) {
    				if(j==1||j==3||j==5||j==7||j==8||j==10||j==12) {
    					sum=sum+31;
    				}
    				if(j==4||j==6||j==9||j==11) {
    					sum=sum+30;
    				}
    				if(j==2) {
    					if((f%4==0&&f%100!=0)||f%400==0){
    						sum =sum+29;
    					}
    					else 
    						sum=sum+28;
    					}
    				}
    			sum=sum+d;
    			System.out.println(sum);
    		}
    	}
    	in.close();
    }
    }
    

    Python :

    def isLeap(year):
        if (year%4==0 and year%100!=0) or year%400==0:
            return True
        else:
            return False
    def compute(date):
        #compute tail-year days
        month_leap = [31,29,31,30,
                 31,30,31,31,
                 30,31,30,31]
        month_noleap = [31,28,31,30,
                 31,30,31,31,
                 30,31,30,31]
        num = 0
        if isLeap(date[0])and date[2]<=month_leap[date[1]-1]:
            for i in month_leap[0:date[1]-1]:num+=i
        elif not isLeap(date[0])and date[2]<=month_noleap[date[1]-1]:
            for i in month_noleap[0:date[1]-1]:num+=i
        else:
            return -1
        num += date[2]
        return num
    
    t = input()
    while t:
        date = [int(i) for i in raw_input().split('-')]
        #compute tail-year days
        num = compute(date)
        if isLeap(date[0]):num=366-num
        else:num=365-num
        #compute 16-year days
        for i in range(1,18):
            if isLeap(date[0]+i):num+=366
            else :num+=365
        #compute tail-year days
        date[0]+=18
        #print date
        if compute(date)!=-1:
            num+=compute(date)
            print num
        else:print -1
        t -= 1
        if t==0:break
    
    
    • 1

    信息

    ID
    1413
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者