c变量和方程
c variables and equation
感谢 Sankalp Bhamare 更新了代码和问题:更新后的代码未获得变量 twoHundredFiftyScholarship
中的预期值。
使用调试器时,我可以看到唯一不正确的值是变量 twoHundredFiftyScholarship
.
输入样本时变量twoHundredFiftyScholarship
的期望值运行#1应该是1,我得到的值是3。
与样本 运行 #2 相同。期望值为8,我得到的值为48。
样本运行#1
去年基金有多少?
40000
每年的百分比是多少?
2
将颁发 0 美元 1000 美元的奖学金。
1 人将获得 500 美元的奖学金。
1 将获得 250 美元的奖学金。
样本运行#2
去年基金有多少?
1200000
每年的百分比是多少?
1
5人将获得1000美元的奖学金。
将颁发 10 笔 500 美元的奖学金。
将颁发 8 笔 250 美元的奖学金。
输入样本时变量twoHundredFiftyScholarship
的期望值运行#2应该是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 00 scholarships will be awarded.\n",thousandScholarships);
printf("%d 0 scholarships will be awarded.\n",fiveHundredScholarship);
printf("%d 0 scholarships will be awarded.\n",twoHundredFiftyScholarship);
system("pause");
return 0;
}
Question 1: How many variables are needed for this problem?
这个问题毫无意义。假设你有一个变量声明为 int x;
然后你可以编码
int x1 = x;
int x2 = x*1;
int x3 = x+0;
int x4 = x|0;
那么 x
、x1
、x2
、x3
、x4
都包含相同的整数值,可以互换使用。
再假设您有两个变量和代码
int x= something();
int y= x+1;
然后(如果 y
以后没有更改)您可以用 x+1
.
替换出现的 y
一定要阅读有关 C 编程语言的更多信息(特别是 Modern C) and How to debug small programs。
您可以简单地实现以上内容:
#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("00 Scholarships : %d\n",thousandScholarships);
printf("0 Scholarships : %d\n",fiveHundredScholarship);
printf("0 Scholarships : %d\n",twoHundredFiftyScholarship);
printf("Residual Amount : %lf\n",remainingScholarship);
return 0;
}
感谢 Sankalp Bhamare 更新了代码和问题:更新后的代码未获得变量 twoHundredFiftyScholarship
中的预期值。
使用调试器时,我可以看到唯一不正确的值是变量 twoHundredFiftyScholarship
.
输入样本时变量twoHundredFiftyScholarship
的期望值运行#1应该是1,我得到的值是3。
与样本 运行 #2 相同。期望值为8,我得到的值为48。
样本运行#1
去年基金有多少?
40000
每年的百分比是多少?
2
将颁发 0 美元 1000 美元的奖学金。
1 人将获得 500 美元的奖学金。
1 将获得 250 美元的奖学金。
样本运行#2
去年基金有多少?
1200000
每年的百分比是多少?
1
5人将获得1000美元的奖学金。
将颁发 10 笔 500 美元的奖学金。
将颁发 8 笔 250 美元的奖学金。
输入样本时变量twoHundredFiftyScholarship
的期望值运行#2应该是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 00 scholarships will be awarded.\n",thousandScholarships);
printf("%d 0 scholarships will be awarded.\n",fiveHundredScholarship);
printf("%d 0 scholarships will be awarded.\n",twoHundredFiftyScholarship);
system("pause");
return 0;
}
Question 1: How many variables are needed for this problem?
这个问题毫无意义。假设你有一个变量声明为 int x;
然后你可以编码
int x1 = x;
int x2 = x*1;
int x3 = x+0;
int x4 = x|0;
那么 x
、x1
、x2
、x3
、x4
都包含相同的整数值,可以互换使用。
再假设您有两个变量和代码
int x= something();
int y= x+1;
然后(如果 y
以后没有更改)您可以用 x+1
.
y
一定要阅读有关 C 编程语言的更多信息(特别是 Modern C) and How to debug small programs。
您可以简单地实现以上内容:
#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("00 Scholarships : %d\n",thousandScholarships);
printf("0 Scholarships : %d\n",fiveHundredScholarship);
printf("0 Scholarships : %d\n",twoHundredFiftyScholarship);
printf("Residual Amount : %lf\n",remainingScholarship);
return 0;
}