2016-09-16 45 views
-2

我很難找出一種方法,從「放棄」超過5美元的1000美元獎學金和10美元500美元的獎學金計劃。我將如何在5和8之後從我已經擁有的東西中剪掉它?我的代碼是問題下面程序員的如何製作最多獎學金?


一個師,共創美好明天就是他們的獎學金捐贈基金。他們 每年向需要1000,500和250美元的學生的學生提供獎學金。 這些獎學金的資金來自以前的捐贈和 投資。

您將創建一個計劃來計算基金的年度利息,並確定可以獎勵1000美元,500美元和250美元的獎學金。 例如,如果基金在2016年9月30日有50萬美元,並且年利率爲3%,那麼基金在9月份的 年底將有515,000美元。這給他們$ 15,000作爲獎學金支付。

如果可能的話,基金會傾向於獎勵5美元1000美元的獎學金,10美元的獎學金和500美元的獎學金,以及更多的 250美元,因爲他們還有錢。 15,000美元的基金可以獎勵5千美元的獎學金,10 500美元的獎學金和20美元的250美元的獎學金。您的程序應該爲 用戶打印此信息。

如果這是不可能的,基金會將盡可能獎勵1000美元和500美元的獎學金。

例如,如果他們有4750美元,他們將獎勵4美元1000美元的獎學金,1500美元的獎學金和 美元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; 
} 
+0

像[這個家庭作業](http://cboard.cprogramming。COM/C編程/ 171235燦別人的幫助-着-GET-workings.html)?這是發佈在這裏的衆多金錢問題之一,新手的錯誤是在浮點工作。 –

+1

@Weather Vane Money在任何數據類型中工作都有問題。 [各種問題](http://stackoverflow.com/a/32214586/2410359) – chux

+0

@chux很好的答案,但你的水晶球說我們當地的儲蓄利率會下降到0.5%? –

回答

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

你的一個最大的問題是,您使用浮點要錢操作,但我把它留給你(或你的教授,誰可能會或可能不會在後面指出這些問題)。

這個代碼還有很多其他的方式可能會失敗,你需要填補所有這些漏洞!