为什么程序中的循环不会终止?

时间:2015-09-06 17:52:14

标签: c performance while-loop greedy cs50

我的代码是为了显示一个人在一定金额上需要的硬币数量最少。我将值转换为美分,然后使用循环测试每个不同的硬币。您可能会看到printf用于我用于测试目的的变量,以查看它出错的地方。循环只是永远持续,并且不会像循环重复那样多次减去C的值。恩。每次循环重复时,c-25保持在475美分而不是下降25美分(价值5.00美元)。

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
float c=0;
int i=0;
int y=0;

    printf("How much change do you owe?\n");
    c=GetFloat();
    c=c*100;
    printf("%f\n", c);
    do
    {
        c-25;
        printf("%d\n", c-25);
        if (c>25)
        {
            i++;
            printf("%d\n", i);
        }
    }
        while (c>=25);
    printf("%d\n", i);

    do
    {
        y=c-10;
        if (y>0)
        {
            i++;
        }
    }
        while (y>10);
    if (y<=10)
    {
        printf("%d\n", i);
    }
    do
    {
        y=c-5;
        if (y>0)
        {
            i++;
        }
    }
        while (y>5);
    if (y<=5)
    {
        printf("%d\n", i);
    }
    do
    {
        y=c-1;
        if (y>0)
        {
            i++;
        }
    }
        while (y>1);
    if (y==0)
    {
        printf("%d\n", i);
    }
}

注意:这是CS50

中的“更改时间”程序

2 个答案:

答案 0 :(得分:4)

.site-header{ border-bottom: 1px solid #DADADA; } .site-branding{ width: 30%; padding-top:5px; } .navigation-container{ width: 70%; } .main-navigation ul { clear: both; display: block; float: left; } .main-navigation{ padding-top: 5px; display: block; } .main-navigation li{ display:block; border-right: 1px dotted #DADADA; } .main-navigation a{ padding-right:3px; padding-left:3px; font-size:75%; font-weight: lighter; display: block; } .main-navigation li:last-of-type{ border-right:none } .main-navigation a ul{ display:none; } .main-navigation ul li:hover ul{ right: 50%; margin-right: -150%; width:180px; position: absolute; } .main-navigation ul ul a{ font-size:10px; font-weight: lighter; width:180px; } .main-navigation ul ul li{ font-size:50%; } 永远不会改变。

对声明没有影响:

c

应该是:

c-25;

c = c - 25;

答案 1 :(得分:1)

以下是对您的代码的一些评论:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void){
// use self-explanatory and meaningful names of your variables 
float c = 0;
int i = 0;
int y = 0;

// input
printf("How much change do you owe?\n");
c = GetFloat();
c = c * 100;
// print input
printf("%f\n", c);

// add comment explaining the purpose of this loop 
do{
    c = c - 25; // corrected to decrement by 25

    printf("%d\n", c);
    if (c > 25){
        i++; 
        printf("%d\n", i);
    }
}while(c >= 25);

printf("%d\n", i);

// add comment explaining the purpose of this loop 
do{
    y = c - 10;
    if (y > 0){
        i++;
    }
}while(y > 10);

// add comment explaining the purpose of this condition statement
if (y <= 10){
    printf("%d\n", i);
}

// add comment explaining the purpose of this loop
do{
    y = c-5;
    if (y > 0){
        i++;
    }
}while(y > 5);

// add comment explaining the purpose of this condition statement
if (y <= 5){
    printf("%d\n", i);
}

// add comment explaining the purpose of this condition statement
do{
    y = c-1;
    if (y > 0){
        i++;
    }
}while(y > 1);
// add comment explaining the purpose of this condition statement
if(y == 0){
    printf("%d\n", i);
}
}

主要问题来自终止条件计数器变量递增/递减,这些错误或缺失会导致无限循环。