将天转换为周的程序

编写一个程序将给定的天数转换为周数。

例子:
输入: 14 天
输出: 2 周

输入: 30 天
输出: 4 周零 2 天

方法:要解决该问题,请遵循以下思路:
将给定的天数除以 7 即可得到周数。其余就是剩下的日子了。

分步算法

  • 读取天数。
  • 计算周数和剩余天数。
  • 显示结果。

C++

include <iostream>

using namespace std;

int main()
{
    int totalDays = 30;
    int weeks = totalDays / 7;
    int remainingDays = totalDays % 7;

    cout << totalDays << " days is equal to " << weeks
        <<
" weeks and " << remainingDays << " days."
        << endl;

    return 0;
}


C语言

include <stdio.h>

int main()
{
    int totalDays = 30;
    int weeks = totalDays / 7;
    int remainingDays = totalDays % 7;

    printf("%d days is equal to %d weeks and %d days.\n",
        totalDays, weeks, remainingDays);

    return 0;
}

Java

public class DaysToWeeksConverter {
    public static void main(String[] args)
    {
        int totalDays = 30;
        int weeks = totalDays / 7;
        int remainingDays = totalDays % 7;

        System.out.println(totalDays + " days is equal to "
                        + weeks +
" weeks and "
                        + remainingDays +
" days.");
    }
}


Python3

total_days = 30
weeks = total_days // 7
remaining_days = total_days % 7

print(f
"{total_days} days is equal to {weeks} weeks and {remaining_days} days.")

C#

using System;

class Program {
    static void Main()
    {
        int totalDays = 30;
        int weeks = totalDays / 7;
        int remainingDays = totalDays % 7;

        Console.WriteLine(
            $"{totalDays} days is equal to {weeks} weeks and {remainingDays} days."
        );
    }
}

Javascript

let totalDays = 30;
let weeks = Math.floor(totalDays / 7);
let remainingDays = totalDays % 7;

console.log(`${totalDays} days is equal to ${weeks} weeks and ${remainingDays} days.`);