我在想办法“切断”该计划的过程中遇到了麻烦,他们提供了5多个1,000美元的奖学金和10个500美元的奖学金。在已有的5和8点之后,我该如何将其切断?我的代码在问题下方
更好的明天计划的一个部门是他们的奖学金基金会。他们
向需要帮助的学生提供每年1000、500和250的奖学金
美元。
这些奖学金的钱来自以前的捐款和
投资。
您将创建一个程序来计算该基金的年度利息,并
确定可以授予多少$ 1000,$ 500和$ 250奖学金。
例如,如果该基金在2016年9月30日以及每年
利率为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奖学金。
输入规格
截至一年前,基金中的金额n,其中n大于或等于
到0。(n可以包含小数位)
年百分比率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/