我如何获得最多奖学金?

时间:2016-09-16 22:54:21

标签: c

我无法找到一种方法来切断"切断"该计划提供超过5美元的奖学金和10美元的500美元奖学金。我将如何从已有的5和8之后切断它?我的代码低于问题

更好的明天程序员的一个部门是他们的奖学金捐赠基金。他们 为需要手数1000,500和250的学生提供年度奖学金 美元。 这些奖学金的资金来自对之前捐赠的兴趣 投资。

您将创建一个程序来计算基金的年度利息和 确定可以奖励多少1000美元,500美元和250美元的奖学金。 例如,如果基金在2016年9月30日和每年有500,000美元 利率是3%,然后基金将在此结束时拥有515,000美元 九月。这给了他们15,000美元作为奖学金支付。

如果可能的话,基金会更愿意奖励5美元奖学金,10美元奖学金,以及多少奖学金 他们有钱留下250美元。凭借15,000美元,该基金可以奖励5美元奖金,10美元 500美元奖学金,20美元奖学金250美元。您的程序应该打印此信息 用户。

如果无法做到这一点,基金将尽可能多地奖励1000美元和500美元的奖学金。

例如,如果他们有4,750美元,他们将获得4千美元的奖学金,1 500美元的奖学金,以及 1 250美元的奖学金。

输入规范

  1. 基金的资金金额,n,一年前n大于或等于 到0.(n可能包括小数位)
  2. 年度百分比率p,为整数,其中p大于零。
  3. 输出规格
    使用以下格式输出结果:

    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;
    }
    

1 个答案:

答案 0 :(得分: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);
}

你最大的问题之一就是你使用浮点资金操作,但我会留给你(或你的教授,以后可能会或可能不会指出这些问题)。

这段代码还有许多其他方法可能会失败,你需要填补所有这些漏洞!