📜  程序计算给定年份中的奇数天数

📅  最后修改于: 2021-04-23 15:59:58             🧑  作者: Mango

给定整数N ,任务是查找1N之间的年份中的奇数天数。
奇数天:奇数天数是指将某年中的天数转换为周数时剩余的天数。假设普通一年有365天,也就是52周零零零一天。这意味着,在平常的365天中,有364天将转换为52周,而剩下的日子将是一天。这一天称为1个奇数日。

  • 简单地将模数总天数乘以7(一周中的天数)即可得出奇数天数。
  • 它的值仅介于0到6之间。 [0,6]
  • 年:每年可被400或4整除,但不能被100整除
  • 普通年份:Le年以外的年份
  • 每个普通年有1个奇数天。
  • 每个Le年都有2个奇数天。

例子:

方法:

  1. 计算给定N年中的普通年数和of年数。
  2. 计算总天数。
  3. 打印总天数的modulo(7)。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of odd days
int oddDays(int N)
{
 
    // Count of years divisible
    // by 100 and 400
    int hund1 = N / 100;
    int hund4 = N / 400;
 
    // Every 4th year is a leap year
    int leap = N >> 2;
    int ord = N - leap;
 
    // Every 100th year is divisible by 4
    // but is not a leap year
    if (hund1) {
        ord += hund1;
        leap -= hund1;
    }
 
    // Every 400th year is divisible by 100
    // but is a leap year
    if (hund4) {
        ord -= hund4;
        leap += hund4;
    }
 
    // Total number of extra days
    int days = ord + leap * 2;
 
    // modulo(7) for final answer
    int odd = days % 7;
 
    return odd;
}
 
// Driver code
int main()
{
 
    // Number of days
    int N = 100;
    cout << oddDays(N);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the count of odd days
    static int oddDays(int N)
    {
        // Count of years divisible
        // by 100 and 400
        int hund1 = N / 100;
        int hund4 = N / 400;
 
        // Every 4th year is a leap year
        int leap = N >> 2;
        int ord = N - leap;
 
        // Every 100th year is divisible by 4
        // but is not a leap year
        if (hund1 > 0) {
            ord += hund1;
            leap -= hund1;
        }
 
        // Every 400th year is divisible by 100
        // but is a leap year
        if (hund4 > 0) {
            ord -= hund4;
            leap += hund4;
        }
 
        // Total number of extra days
        int days = ord + leap * 2;
 
        // modulo(7) for final answer
        int odd = days % 7;
 
        return odd;
    }
 
    // Driver code
    public static void main(String args[])
    {
 
        // Number of days
        int N = 100;
        System.out.print(oddDays(N));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the count of odd days
def oddDays(N):
 
    # Count of years divisible
    # by 100 and 400
    hund1 = N // 100
    hund4 = N // 400
 
    # Every 4th year is a leap year
    leap = N >> 2
    ordd = N - leap
 
    # Every 100th year is divisible by 4
    # but is not a leap year
    if (hund1):
        ordd += hund1
        leap -= hund1
 
    # Every 400th year is divisible by 100
    # but is a leap year
    if (hund4):
        ordd -= hund4
        leap += hund4
 
    # Total number of extra days
    days = ordd + leap * 2
 
    # modulo(7) for final answer
    odd = days % 7
 
    return odd
 
# Driver code
 
# Number of days
N = 100
print(oddDays(N))
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the count of odd days
    static int oddDays(int N)
    {
        // Count of years divisible
        // by 100 and 400
        int hund1 = N / 100;
        int hund4 = N / 400;
 
        // Every 4th year is a leap year
        int leap = N >> 2;
        int ord = N - leap;
 
        // Every 100th year is divisible by 4
        // but is not a leap year
        if (hund1 > 0)
        {
            ord += hund1;
            leap -= hund1;
        }
 
        // Every 400th year is divisible by 100
        // but is a leap year
        if (hund4 > 0)
        {
            ord -= hund4;
            leap += hund4;
        }
 
        // Total number of extra days
        int days = ord + leap * 2;
 
        // modulo(7) for final answer
        int odd = days % 7;
 
        return odd;
    }
 
    // Driver code
    static void Main()
    {
 
        // Number of days
        int N = 100;
        Console.WriteLine(oddDays(N));
    }
}
 
// This code is contributed by mits


PHP
> 2;
    $ord = $N - $leap;
 
    // Every 100th year is divisible by 4
    // but is not a leap year
    if ($hund1)
    {
        $ord += $hund1;
        $leap -= $hund1;
    }
 
    // Every 400th year is divisible by 100
    // but is a leap year
    if ($hund4)
    {
        $ord -= $hund4;
        $leap += $hund4;
    }
 
    // Total number of extra days
    $days = $ord + $leap * 2;
 
    // modulo(7) for final answer
    $odd = $days % 7;
 
    return $odd;
}
 
// Driver code
 
// Number of days
$N = 100;
 
echo oddDays($N);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
5