基本 C 编程帮助
嘿,我需要帮助编写一个简单的程序。我希望能够演示整数的使用以及使用模数的余数。我陷入了如何计算信息的困境。任何帮助将不胜感激,但这是总体思路。该程序包含以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像家庭作业,所以我将解释模数的工作原理。
模运算符 % 执行整数除法并返回余数。例如:
在该示例中,
余数
将设置为 2。您可以阅读有关模运算符的更多信息 此处。
This smells like homework so I will explain how modulus works.
The modulus operator, %, performs integer division and returns the remainder. For example:
In that example,
remainder
will be set to 2.You can read more about the modulus operator here.
我不会用代码回答你的问题,因为这看起来像是家庭作业。当你真正应该开始编码的时候,你也已经停下来了!
该问题是“更改返回”典型问题的另一个皮肤。这是一种急切的算法,试图以它可以采取的最大步骤来解决目标。
您必须有两个平行向量:
请注意,第一个向量已排序,并且每个位置与第二个向量中的相同位置匹配。在您的示例中,目标是 59。
对于第一个向量中的每个位置,您必须除以您的目标,并注释剩余的位置。
例如,对于第一个位置,最大数量是 1,对于第二个位置,最大数量是 2...
您最终将得到一个与第一个位置一样长的向量,并带有结果:
为了显示结果,只需运行向量,并将每个位置乘以第二个向量中的相同位置:
这样您就可以获得所需的结果。
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:
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...
You'll finally get a vector as long as the first one with the results:
In order to show the results, just run over the vector, and multiply each position by the same position in the second vector:
This way you get the result you need.