当前位置:网站首页>Judging the number of leap years from 1 to N years

Judging the number of leap years from 1 to N years

2022-06-25 14:50:00 䨁 逦

According to the current calendar ( Gregorian calendar ): Leap every four years , Every hundred years does not leap , Leap every 400 years .

namely : The number of years is 4 It's not a multiple of 100 Multiple , Or years 400 Multiple , It's a leap year , The rest are ordinary years .

Please write the program , Year of importation  y, Calculate ad  1  Year to  y  The total number of leap years in a year  n.

Input format

y

Output format

n

sample input

1995

My original code

#include<stdio.h>
int ruennian(int x){
    long long n=0;
    for(long long i=1;i<=x;i++){
    if((i%4 == 0 &&i%100!= 0)||(i%400 == 0))
        n++;
    }
    return n;

int main(){
    long long x=0;
    scanf("%d",&x);
    long long c=ruennian(x);
    printf("%d\n",c);
    return 0;

Optimize the code

#include<stdio.h>
int main(){
    long long n,a,b,c;
    scanf("%ld",&n);
    a=n/4;
    b=n/100;
    c=n/400;
    printf("%ld\n",a-b+c);
    return 0;
}

原网站

版权声明
本文为[䨁 逦]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200518466659.html