int main()
{
//声明变量
float num;
float a, b, c;
int numtwonies;
int numjonnies;
int numloonies;
int numquarters;
int numdimes;
int numnickles;
int numpennies;
int max;
int numcoins;
//得到输入
printf("please input value: ");
scanf_s("%f", &num);
//为方便计算,转换单位为分
b = num * 100;
//得到硬币最大的上限
max = b / 126;
///由0开始,增量a,增量不大于b
for (a = 0; a <= b; a++)
{
//初始全部为0
c = 0;
numcoins = a;
numjonnies = a;
numtwonies = 0;
numdimes = 0;
numloonies = 0;
numnickles = 0;
numquarters = 0;
numpennies = 0;
//开始增量
for (numjonnies = 0; numjonnies <= max; numjonnies++)
{
numjonnies++;
numcoins++;
b = b - 126;
//计算twonies
while (b >= 200)
{
numtwonies++;
numcoins++;
b = b - 200;
}
//计算loonies
while (b >= 100)
{
numloonies++;
numcoins++;
b = b = 100;
}
//计算quarters
while (b >= 25)
{
numquarters++;
numcoins++;
b = b - 25;
}
//计算dimes
while (b >= 10)
{
numdimes++;
numcoins++;
b = b - 10;
}
//计算nickles
while (b >= 5)
{
numnickles++;
numcoins++;
b = b - 5;
}
//计算pennies
while (b >= 1)
{
numpennies++;
numcoins++;
b = b - 1;
}
}
}
//输出所有硬币数量
printf("the number of coins is %.0f\n", c);
//输出每种硬币的数量
printf("the number of twonies is %i\n", numtwonies);
printf("the number of jonnies is %i\n", numjonnies);
printf("the number of loonies is %i\n", numloonies);
printf("the number of quarters is %i\n", numquarters);
printf("the number of dimes is %i\n", numdimes);
printf("the number of nickles is %i\n", numnickles);
printf("the number of pennies is %i\n", numpennies);
return 0;
}
用vs2013写的
提问:当我运行程序的时候,我输入20,结果应该是显示twonies 10个,但是其他的也显示出来了,我个人感觉是max = b / 126; 和for循环那里出问题了,而且不知道为神马,while循环有点不管用。
{
//声明变量
float num;
float a, b, c;
int numtwonies;
int numjonnies;
int numloonies;
int numquarters;
int numdimes;
int numnickles;
int numpennies;
int max;
int numcoins;
//得到输入
printf("please input value: ");
scanf_s("%f", &num);
//为方便计算,转换单位为分
b = num * 100;
//得到硬币最大的上限
max = b / 126;
///由0开始,增量a,增量不大于b
for (a = 0; a <= b; a++)
{
//初始全部为0
c = 0;
numcoins = a;
numjonnies = a;
numtwonies = 0;
numdimes = 0;
numloonies = 0;
numnickles = 0;
numquarters = 0;
numpennies = 0;
//开始增量
for (numjonnies = 0; numjonnies <= max; numjonnies++)
{
numjonnies++;
numcoins++;
b = b - 126;
//计算twonies
while (b >= 200)
{
numtwonies++;
numcoins++;
b = b - 200;
}
//计算loonies
while (b >= 100)
{
numloonies++;
numcoins++;
b = b = 100;
}
//计算quarters
while (b >= 25)
{
numquarters++;
numcoins++;
b = b - 25;
}
//计算dimes
while (b >= 10)
{
numdimes++;
numcoins++;
b = b - 10;
}
//计算nickles
while (b >= 5)
{
numnickles++;
numcoins++;
b = b - 5;
}
//计算pennies
while (b >= 1)
{
numpennies++;
numcoins++;
b = b - 1;
}
}
}
//输出所有硬币数量
printf("the number of coins is %.0f\n", c);
//输出每种硬币的数量
printf("the number of twonies is %i\n", numtwonies);
printf("the number of jonnies is %i\n", numjonnies);
printf("the number of loonies is %i\n", numloonies);
printf("the number of quarters is %i\n", numquarters);
printf("the number of dimes is %i\n", numdimes);
printf("the number of nickles is %i\n", numnickles);
printf("the number of pennies is %i\n", numpennies);
return 0;
}
用vs2013写的
提问:当我运行程序的时候,我输入20,结果应该是显示twonies 10个,但是其他的也显示出来了,我个人感觉是max = b / 126; 和for循环那里出问题了,而且不知道为神马,while循环有点不管用。