C 模和余数

发布于 2024-11-09 03:00:44 字数 256 浏览 0 评论 0原文

嘿,我正在努力弄清楚如何显示这个结果。举例来说,我输入一个数字,例如 59。根据该数字,我得到的剩余结果为 1 周 2 天 5 小时。当然,为了获得该输出,这是假设一周有 40 小时、1 天有 7 小时。任何朝着正确方向的帮助都会有所帮助。到目前为止,我已经将其设置如下:

scanf("%d %d %d", &totalWeeksWorked, &totalDaysWorked, &totalHoursWorked);

Hey, I'm having the toughest time figuring out how to display this result. Say for example I enter a number such as 59. Based off that number, I get a remaining result of 1 week(s) 2 Day(s) and 5 Hour(s). This is of course assuming one week has 40 hours and 1 day has 7 hours in order to get that output. Any help in the right direction would be helpful. So far I've set it up like so:

scanf("%d %d %d", &totalWeeksWorked, &totalDaysWorked, &totalHoursWorked);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城烟雨 2024-11-16 03:00:44

这不是最快的方法,但可能是最具说明性的:

int numodweeks = input/(7*24);
int numofdays  =input/24;
int numofhours = 24 - (input/24);

使用模数:

        int numofweeks = input/(7*24);
        int numofdays = (input%numofweeks)/7;
        int numofhours = (input%(numofdays*24));

然后按照您想要的方式显示它们。

This is not the fastest way, but is perhaps the most illustrative:

int numodweeks = input/(7*24);
int numofdays  =input/24;
int numofhours = 24 - (input/24);

Using modulo:

        int numofweeks = input/(7*24);
        int numofdays = (input%numofweeks)/7;
        int numofhours = (input%(numofdays*24));

Then display them how you want.

遇到 2024-11-16 03:00:44
#include <stdio.h>

int const HOURS_PER_WEEK = 40;
int const HOURS_PER_DAY = 7;

int main() {
  int total_hours = 59;  // this is the input you get

  int remaining = total_hours;  // 'remaining' is scratch space

  int weeks = remaining / HOURS_PER_WEEK;
  remaining %= HOURS_PER_WEEK;

  int days = remaining / HOURS_PER_DAY;
  remaining %= HOURS_PER_DAY;

  int hours = remaining;

  printf("%d hours = %d weeks, %d days, %d hours\n",
         total_hours, weeks, days, hours);

  return 0;
}
#include <stdio.h>

int const HOURS_PER_WEEK = 40;
int const HOURS_PER_DAY = 7;

int main() {
  int total_hours = 59;  // this is the input you get

  int remaining = total_hours;  // 'remaining' is scratch space

  int weeks = remaining / HOURS_PER_WEEK;
  remaining %= HOURS_PER_WEEK;

  int days = remaining / HOURS_PER_DAY;
  remaining %= HOURS_PER_DAY;

  int hours = remaining;

  printf("%d hours = %d weeks, %d days, %d hours\n",
         total_hours, weeks, days, hours);

  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文