c个变量和方程式

问题描述 投票:-2回答:2

我正在用C编写一个程序,该程序计算对基金的年利息,并确定可以授予多少1000、500和250美元的奖学金。

[如果可能,基金应奖励5笔$ 1000的奖学金,10笔$ 500的奖学金以及250英镑的剩余资金。如果这不可能,该基金将尽可能地提供$ 1000和$ 500的奖学金。例如,如果他们有$ 4,750,他们将奖励4个$ 1000奖学金,1个$ 500奖学金和1个$ 250奖学金。

即凭借$ 15,000的资金,该基金可以授予5个$ 1000的奖学金,10个$ 500的奖学金和20 $ 250的奖学金。该程序应为用户打印此信息。

我有2个问题。

问题1:此问题需要多少个变量?我可以想到Amount_in_fundInterestpercent_RATEscholarship1000scholarship500scholarship250我是否缺少变量来解决此问题?

问题2:获得scholarship1000值的正确方程式是什么?正如您在我的代码中看到的那样,我的方程式有误

我一直在弄乱这段代码。

#include <stdio.h>



int main (){

    double Amount_in_fund = 0 ;
    int yearly_INTEREST = 0, percent_RATE, scholarship1000 = 0, scholarship500 = 0, scholarship250 = 0;

    //takes data and saves it in Amount_in_fund 
    printf("How much was in the fund last year?\n");
    scanf("%lf", &Amount_in_fund);

    //takes data and saves it in percent_RATE
    printf("What is the yearly percentage rate?\n");
    scanf("%d", &percent_RATE); 

    if(scholarship1000 < 5){
        yearly_INTEREST = (Amount_in_fund * percent_RATE)/100;
        scholarship1000 = yearly_INTEREST / 1000;  //***** What would be the right equation here?**
        printf("%d $1000 scholarships will be awarded.\n", scholarship1000);

    }
    else if(scholarship1000 >= 5){

        printf("5 $1000 scholarships will be awarded.\n");

    }   

    system("pause");


return 0;

}

由于Sankalp Bhamare而更新了代码:更新后的代码未在变量twoHundredFiftyScholarship中获得期望的值。

使用调试器时,我看到唯一不正确的值是变量twoHundredFiftyScholarship

进入样本运行#1时变量twoHundredFiftyScholarship的期望值应为1。我得到的值为3。

与样品运行#2相同。期望值为8。我得到的值为48。


示例运行#1

去年基金里有多少钱?

40000

每年的百分比是多少?

2

将授予0美元$ 1000的奖学金。

将授予1 500美元的奖学金。

将颁发1 250美元的奖学金。

示例运行#2

去年基金里有多少钱?

1200000

每年的百分比是多少?

1

将授予5项$ 1000的奖学金。

将颁发10个$ 500的奖学金。

将颁发8项$ 250的奖学金。

进入样本运行#2时变量twoHundredFiftyScholarship的期望值应为8。我得到的值为48。

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


int main(){

    double fundAmount;
    int yearlyInterestRate;
    double yearlyInterest;
    double remainingScholarship;
    int thousandScholarships = 0;
    int fiveHundredScholarship = 0;
    int twoHundredFiftyScholarship = 0;


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

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

    yearlyInterest = fundAmount*yearlyInterestRate/100.0;
    remainingScholarship = yearlyInterest;
    thousandScholarships = remainingScholarship/1000.0;
    fiveHundredScholarship = remainingScholarship/500.0;
    twoHundredFiftyScholarship = remainingScholarship/250.0;

    if(thousandScholarships > 5){
        thousandScholarships = 5;

        remainingScholarship -= thousandScholarships*1000.0;}


    if(fiveHundredScholarship > 10){
        fiveHundredScholarship = 10;

        remainingScholarship -= fiveHundredScholarship*500;}



        remainingScholarship -= twoHundredFiftyScholarship*250;



        printf("%d $1000 scholarships will be awarded.\n",thousandScholarships);
        printf("%d $500 scholarships will be awarded.\n",fiveHundredScholarship);       
        printf("%d $250 scholarships will be awarded.\n",twoHundredFiftyScholarship);



    system("pause");

    return 0;
}

c variables equation
2个回答
1
投票

您可以简单地实现上述内容:

#include <stdio.h>

int main()
{

    double fundAmount = 100350.0;
    double yearlyInterestRate = 9.0;
    double yearlyInterest = fundAmount*yearlyInterestRate/100.0;

    double remainingScholarship = yearlyInterest;

    int thousandScholarships = remainingScholarship/1000.0;
    if(thousandScholarships > 5)
        thousandScholarships = 5;

    remainingScholarship -= thousandScholarships*1000.0;

    int fiveHundredScholarship = remainingScholarship/500.0;
    if (fiveHundredScholarship > 10)
        fiveHundredScholarship = 10;

    remainingScholarship -= fiveHundredScholarship*500;
    int twoHundredFiftyScholarship = remainingScholarship/250.0;
    remainingScholarship -= twoHundredFiftyScholarship*250;

    printf("$1000 Scholarships : %d\n",thousandScholarships);
    printf("$500 Scholarships : %d\n",fiveHundredScholarship);
    printf("$250 Scholarships : %d\n",twoHundredFiftyScholarship);
    printf("Residual Amount : %lf\n",remainingScholarship);
    return 0;
}

1
投票

问题1:此问题需要多少个变量?

这个问题没有道理。假设您有一个变量声明为int x;,则可以编写代码

int x1 = x;
int x2 = x*1;
int x3 = x+0;
int x4 = x|0;

然后xx1x2x3x4都包含相同的整数值,并且可以互换使用。

还要想象您有两个变量和代码

int x= something();
int y= x+1;

然后(如果以后不更改y,则可以用y代替出现的x+1

请务必阅读有关C编程语言(特别是Modern C)和How to debug small programs的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.