基本 C 编程帮助

发布于 2024-11-09 00:38:00 字数 756 浏览 0 评论 0原文

嘿,我需要帮助编写一个简单的程序。我希望能够演示整数的使用以及使用模数的余数。我陷入了如何计算信息的困境。任何帮助将不胜感激,但这是总体思路。该程序包含以下内容:

1 week = 40 hours ($200 per week)
1 day = 7 hours   ($45 per day)
                  ($2 per hour)

示例运行:

Enter the total hours:59 (59 is just an example.)
You have:
         1week
         2day(s)
         5hr(s)
Payment: $300.00

这是我到目前为止所想到的......

int main(){

   int totalWeekHrs = 0,
   totalDayHrs = 0,
   totalWorkedHrs = 0;

   float totalPayment = 0,
     payPerWeek = 0,
     payPerDay = 0,
     PayPerHr = 0;

   // Input process   
   printf("Enter the total hours :");
   scanf("%i",&totalWeekHrs,&totalDayHrs,&totalWorkedHrs);

  // Calculative process

system("pause");    
}

Hey, I need help writing a simple program. I want to be able to demonstrate the use of integers and the remainders using a modulus. I'm stuck at how I should calculate the information. Any help would be much appreciated, but here's the general idea. The program encompasses the following:

1 week = 40 hours ($200 per week)
1 day = 7 hours   ($45 per day)
                  ($2 per hour)

Sample run:

Enter the total hours:59 (59 is just an example.)
You have:
         1week
         2day(s)
         5hr(s)
Payment: $300.00

Here's what I've come up with so far...

int main(){

   int totalWeekHrs = 0,
   totalDayHrs = 0,
   totalWorkedHrs = 0;

   float totalPayment = 0,
     payPerWeek = 0,
     payPerDay = 0,
     PayPerHr = 0;

   // Input process   
   printf("Enter the total hours :");
   scanf("%i",&totalWeekHrs,&totalDayHrs,&totalWorkedHrs);

  // Calculative process

system("pause");    
}

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

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

发布评论

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

评论(2

鸠书 2024-11-16 00:38:00

这听起来像家庭作业,所以我将解释模数的工作原理。

模运算符 % 执行整数除法并返回余数。例如:

int foo = 6;
int bar = 4;
int remainder = foo % bar;

在该示例中,余数 将设置为 2。

您可以阅读有关模运算符的更多信息 此处

This smells like homework so I will explain how modulus works.

The modulus operator, %, performs integer division and returns the remainder. For example:

int foo = 6;
int bar = 4;
int remainder = foo % bar;

In that example, remainder will be set to 2.

You can read more about the modulus operator here.

寄意 2024-11-16 00:38:00

我不会用代码回答你的问题,因为这看起来像是家庭作业。当你真正应该开始编码的时候,你也已经停下来了!

该问题是“更改返回”典型问题的另一个皮肤。这是一种急切的算法,试图以它可以采取的最大步骤来解决目标。

您必须有两个平行向量:

{ 40,   7, 1 }  // hours worked (week, day, hour)
{ 200, 45, 2 }  // salary for each item above.

请注意,第一个向量已排序,并且每个位置与第二个向量中的相同位置匹配。在您的示例中,目标是 59。

对于第一个向量中的每个位置,您必须除以您的目标,并注释剩余的位置。

例如,对于第一个位置,最大数量是 1,对于第二个位置,最大数量是 2...

First step:
( 59 / 40 ) == 1
( 59 % 40 ) == 19

Second step:
( 19 / 7 ) == 2
( 19 % 7 ) == 5

Third step:
( 5 / 1 ) == 5
( 5 % 1 ) == 0

您最终将得到一个与第一个位置一样长的向量,并带有结果:

{ 1, 2, 5 } // one week, two days and five hours.

为了显示结果,只需运行向量,并将每个位置乘以第二个向量中的相同位置:

1 week(s)  ( 1 * 200 )
2 day(s)   ( 2 * 45 )
5 hour(s)  ( 5 * 2 )

( 1 * 200 ) + ( 2 * 45 ) + ( 5 * 2 ) = 300

这样您就可以获得所需的结果。

I won't answer your question with code, since it seems homework. You have also stopped when you should really start coding!

The problem is another skin for the "change return" typical question. This is a eager algorithm which tries to resolve the objective with the biggest step it can take.

You have to have two paralell vectors:

{ 40,   7, 1 }  // hours worked (week, day, hour)
{ 200, 45, 2 }  // salary for each item above.

Notice that the first vector is sorted and that each position matches the same position in the second. The objective is 59 in your example.

For each position in the first vector, you have to divide by your objective, and annotate the remaining.

For example, for the first position, the biggest amount is 1, for the second, 2...

First step:
( 59 / 40 ) == 1
( 59 % 40 ) == 19

Second step:
( 19 / 7 ) == 2
( 19 % 7 ) == 5

Third step:
( 5 / 1 ) == 5
( 5 % 1 ) == 0

You'll finally get a vector as long as the first one with the results:

{ 1, 2, 5 } // one week, two days and five hours.

In order to show the results, just run over the vector, and multiply each position by the same position in the second vector:

1 week(s)  ( 1 * 200 )
2 day(s)   ( 2 * 45 )
5 hour(s)  ( 5 * 2 )

( 1 * 200 ) + ( 2 * 45 ) + ( 5 * 2 ) = 300

This way you get the result you need.

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