gpt4 book ai didi

c - 我如何获得最大数量的奖学金?

转载 作者:行者123 更新时间:2023-11-30 20:35:27 25 4
gpt4 key购买 nike

我无法找到一种方法来“切断”该计划提供超过 5 个 1,000 美元奖学金和 10 个 500 美元奖学金的机会。在 5 和 8 之后我如何将它从我已经拥有的东西中删除?我的代码位于问题下方

<小时/>

“程序员共创美好明天”的一个部门是他们的奖学金捐赠基金。他们每年为需要帮助的学生提供金额为 1000、500 和 250 的奖学金美元。这些奖学金的资金来自以前捐款的利息和投资。

您将创建一个程序来计算基金的年利息并确定可以授予多少 1000 美元、500 美元和 250 美元的奖学金。例如,如果基金在 2016 年 9 月 30 日有 500,000 美元,并且每年利率为 3%,则本期结束时基金内有 515,000 美元九月。这为他们提供了 15,000 美元作为奖学金。

如果可能,基金倾向于颁发 5 名 1000 美元奖学金、10 名 500 美元奖学金,以及尽可能多的奖学金250 美元,因为他们还有钱。凭借 15,000 美元,该基金可颁发 5 名 1000 美元的奖学金、10 名500 美元奖学金和 20 名 250 美元奖学金。您的程序应该打印此信息用户。

如果不可能,该基金将尽可能提供 1000 美元和 500 美元的奖学金。

例如,如果他们有 4,750 美元,他们将颁发 4 份 1000 美元的奖学金、1 份 500 美元的奖学金,以及1 美元 250 美元的奖学金。

输入规范

  1. 截至一年前的基金金额 n,其中 n 大于或等于到 0。(n 可以包含小数位)
  2. 年百分比 p,为整数,其中 p 大于零。

输出规范
使用以下格式输出结果:

X $1000 scholarships will be awarded.
Y $500 scholarships will be awarded.
Z $250 scholarships will be awarded

到目前为止我的代码:

#include <stdio.h>
#include <math.h>


//main function
int main() {

int ten, five, twofive, leftovers_ten, leftovers_five, scholarship_money;
float fund, interest;

printf("How much was in the fund last year?\n");
scanf("%f", &fund);

printf("What is the yearly percentage rate?\n");
scanf("%f", &interest);

scholarship_money = fund * (interest / 100);

{
if(ten < 5) {
ten = scholarship_money / 1000;
printf("%d $1000 scholarships will be awarded.\n", ten);

}
else {
ten = 5;
printf("5 $1000 scholarships will be awarded.\n");
}
}

leftovers_ten = scholarship_money - (ten * 1000);
{

if(five < 10) {
five = leftovers_ten / 500;
printf("%d $500 scholarships will be awarded.\n", five);

}

else {
five = 10;
printf("10 $500 scholarships will be awarded.\n");
}
}

leftovers_five = leftovers_ten - (five * 500);

twofive = leftovers_five / 250;
printf("%d $250 scholarships will be awarded.\n", twofive);

return 0;
}

最佳答案

最简单的调试工具就是打印中间结果。这样你就会发现评论者试图告诉你什么。如果我可以冒昧地给你一个简短的草图:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// ALL CHECKS OMMITTED!

/*
If possible, the Fund prefers to award 5 $1000 scholarships, 10 $500 scholarships, and as many $250 as they have money left for. With $15,000 the Fund can award 5 $1000 scholarships, 10 $500 scholarships, and 20 $250 scholarships.

If that is not possible, the Fund will award as many $1000 and $500 scholarships as they can.

Input Specification
1. The amount of money in the fund, n, as of one year ago where n is greater than
or equal to 0. (n may include decimal places)
2. The yearly percent rate, p, as an integer where p is greater than zero.

Output Specification

Output the result using the format below:
X $1000 scholarships will be awarded.
Y $500 scholarships will be awarded.
Z $250 scholarships will be awarded
*/

int main()
{

int ten, five, twofive, interest;
int res;
float fund, leftovers_ten, leftovers_five, scholarship_money;

printf("How much was in the fund last year?\n");
// scanf returns the number of elements it had read
res = scanf("%f", &fund);
if (res != 1) {
// just bail out here for simplicity
fprintf(stderr, "Input for fund incorrect\n");
exit(EXIT_FAILURE);
}
// 1. The amount of money in the fund, n, as of one year ago where n is greater than
// or equal to 0. (n may include decimal places)
if (fund < 0.0) {
fprintf(stderr, "Fund must be bigger than or equal to zero but is %f\n",
fund);
exit(EXIT_FAILURE);
}

printf("What is the yearly percentage rate?\n");
res = scanf("%d", &interest);
if (res != 1) {
// just bail out here for simplicity
fprintf(stderr, "Input for interrest incorrect\n");
exit(EXIT_FAILURE);
}
// 2. The yearly percent rate, p, as an integer where p is greater than zero.
if (interest <= 0) {
fprintf(stderr, "Interest must be bigger than zero but is %d\n", interest);
exit(EXIT_FAILURE);
}

// some of the casts that have been added for clarity are redundant
scholarship_money = fund * (1.0 + (float) interest / 100.0);
printf("scholarship_money: %.20f\n", scholarship_money);
ten = (int) floor(scholarship_money / 1000.0);
// the Fund prefers to award 5 $1000 scholarships...
if (ten > 5) {
scholarship_money = scholarship_money - (5000.0);
ten = 5;
}
printf("ten: %d\n", ten);

leftovers_ten = scholarship_money - (ten * 1000.0);
printf("leftovers_ten: %.20f\n", leftovers_ten);
five = (int) floor(leftovers_ten / 500.0);
// ... 10 $500 scholarships ...
if (five > 10) {
leftovers_ten = leftovers_ten - (5000.0);
five = 10;
}
printf("five: %d\n", five);

leftovers_five = scholarship_money - (float) (ten * 1000 + five * 500);
printf("leftovers_five: %.20f\n", leftovers_five);
// ... and as many $250 as they have money left for
twofive = (int) floor(leftovers_five / 250.0);
printf("twofive: %d\n", twofive);

printf("%d $1000 scholarships will be awarded.\n", ten);
printf("%d $500 scholarships will be awarded.\n", five);
printf("%d $250 scholarships will be awarded.\n", twofive);
printf("Sum: %d\n", ten * 1000 + five * 500 + twofive * 250);
printf("Rest: %.20f\n",
scholarship_money - (float) (ten * 1000 + five * 500 + twofive * 250));

exit(EXIT_SUCCESS);
}

您最大的问题之一是,您使用浮点进行货币运算,但我会将其留给您(或您的教授,他稍后可能会或可能不会指出这些问题)。

此代码还有很多其他方式可能会失败,您需要填补所有这些漏洞!

关于c - 我如何获得最大数量的奖学金?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39541122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com