2

我有一个我一直在做的任务。我的代码似乎具有正确的数学运算和正确的顺序(至少按顺序),但我的答案有点古怪。我根据我的输入值正确地获得了每个问题的部分内容,但随着输入值的变化,我永远不会得到相同的部分。是否有一些关于我缺少的 C 操作顺序导致了这种情况?

我觉得问题出在

intPeople = intCarCounter * intTrainCounter * CAR_CAPACITY;

对于样本运行#3,前两个输入有 120 人(正好比正确答案多 20 人),剩余人数应该是样本运行#4 中的人数。在第三个输入中,我得到 112 人,这又是样本运行 #4 的正确答案。最后,在第 4 次样本运行中,我得到了 128 人(比正确答案多 16 人),剩余人数应该在第 3 次样本运行中。有任何想法吗?

作业: http ://cop3223.blogspot.com/2013/01/problem-c-roller-coaster-design-coasterc.html

我的代码:

#include <stdio.h>
#include <stdlib.h>
#define FIRST_CAR_LENGTH 10
#define NORMAL_CAR_LENGTH 8
#define CAR_CAPACITY 4

int main(void)
{
    /* Initialize Variables */
    int intTrack,intMaxTrainSize,intActualTrainSize,intPeople,intCarCounter,intTrainCounter,n;

    /* Ask user for value of N */
    printf("What is the value for N?> ");
    scanf("%d",&n);
    for (int i=0; i<n;i++)
    {
        /* Ask user for total length of track */
        printf("\nWhat is the total length of the track, in feet?> ");
        scanf("%d",&intTrack);

        /* Ask user for maximum length of each train */
        printf("What is the maximum length of a train, in feet?> ");
        scanf("%d",&intMaxTrainSize);

        /* Set/Reset initial values of intActualTrainSize, intCarCounter and intTrainCounter */
        /* Each train will begin with FIRST_CAR_LENGTH -> intActualTrainSize=FIRST_CAR_LENGTH */
        /* Each train will begin with 1 car -> intCarCounter=1 */
        /* Train counter will begin at 0 -> intTrainCounter=0 */
        intActualTrainSize=FIRST_CAR_LENGTH;
        intCarCounter=1;
        intTrainCounter=0;

        /* Continue to add additional cars using NORMAL_CAR_LENGTH until the maximum train size has been reached */
        /* Count how many NORMAL_CAR_LENGTH cars are added -> intCarCounter++*/
        while (intActualTrainSize < intMaxTrainSize)
        {
            intActualTrainSize=intActualTrainSize+NORMAL_CAR_LENGTH;
            intCarCounter++;
        } 

        /* Count how many trains can be added until 25% of the track is used up -> intTrainCounter++ */
        while (intTrainCounter*intActualTrainSize < (int)(intTrack*.25)) 
        {
            intTrainCounter++;
        }

        /* Count how many people can be on the track at one time -> intPeople = intCarCounter * intTrainCounter * CAR_CAPACITY */
        intPeople = intCarCounter * intTrainCounter * CAR_CAPACITY;
        printf("\nYour ride can have at most %d people on it at one time.",intPeople);

        if (intActualTrainSize>intMaxTrainSize)
            printf("\nMaximum Train Length has surplus of %d feet.\n",intActualTrainSize-intMaxTrainSize);
        else if (intMaxTrainSize==intActualTrainSize)
            printf("\nMaximum Length fits exactly.\n");
    }
    system("pause");
    return 0;
}
4

4 回答 4

2

您使用循环来计算火车计数器和汽车计数器,作为另一种选择,您可以使用整数算术来计算这些值。我附上了不涉及循环的解决方案。它使调试更容易。我提供两个版本。版本 1 假定整数除法不进行四舍五入。版本 2 更安全,因为它在除法之前减去余数。

    //VERSION 1:

    #include <stdio.h>

    #define FIRST_CAR_LENGTH 10
    #define NORMAL_CAR_LENGTH 8
    #define CAR_CAPACITY 4

    int main(void)
    {
      int N, i;
      int trackSize;
      int trainSize;
      int numberPeople;
      int numberCars;
      int trainsPerTrack;
      int surplus;

      printf("What is the value for N?");
      scanf("%d", &N);
      for(i=0; i < N; i++)
      {
         printf("What is the total length of the track, in feet?\n");
         scanf("%d", &trackSize);
         printf("What is the maximum length of a train, in feet?\n");
         scanf("%d", &trainSize);
         trainsPerTrack = trackSize / (4 * trainSize);
         numberCars = (trainSize -FIRST_CAR_LENGTH) / NORMAL_CAR_LENGTH + 1;

         numberPeople = trainsPerTrack * numberCars * CAR_CAPACITY;
         printf("Your ride can have at most %d people on it at one time.\n", numberPeople);

         surplus = (trainSize - FIRST_CAR_LENGTH) % NORMAL_CAR_LENGTH;
         if(surplus)
           printf("Maximum Train Length has surplus of %d feet\n\n", surplus);
         else
           printf("Maximum Length fits exactly\n\n");
      }
      return 0;
    }


VERSION 2:
#include <stdio.h>

#define FIRST_CAR_LENGTH 10
#define NORMAL_CAR_LENGTH 8
#define CAR_CAPACITY 4

int main(void)
{
  int N, i;
  int trackSize;
  int trainSize;
  int numberPeople;
  int numberCars;
  int trainsPerTrack;
  int surplus;

  printf("What is the value for N?");
  scanf("%d", &N);
  for(i=0; i < N; i++)
  {
     printf("What is the total length of the track, in feet?\n");
     scanf("%d", &trackSize);
     printf("What is the maximum length of a train, in feet?\n");
     scanf("%d", &trainSize);
     trainsPerTrack = (trackSize- (trackSize % (4*trainSize))) / (4 * trainSize);
     int forSmallerCars = trainSize - FIRST_CAR_LENGTH;
     numberCars = (forSmallerCars - (forSmallerCars % NORMAL_CAR_LENGTH)) / NORMAL_CAR_LENGTH + 1;

     numberPeople = trainsPerTrack * numberCars * CAR_CAPACITY;
     printf("Your ride can have at most %d people on it at one time.\n", numberPeople);

     surplus = (trainSize - FIRST_CAR_LENGTH) % NORMAL_CAR_LENGTH;
     if(surplus)
       printf("Maximum Train Length has surplus of %d feet\n\n", surplus);
     else
       printf("Maximum Length fits exactly\n\n");
  }
  return 0;
}
于 2013-01-15T08:51:49.083 回答
1

不确定它是否直接贡献,但您需要重新检查以下内容

while (intActualTrainSize +NORMAL_CAR_LENGTH  <= intMaxTrainSize )
{                         ^^^^^^^^^^^^^^^^^^    
    intActualTrainSize=intActualTrainSize+NORMAL_CAR_LENGTH;
    intCarCounter++;
} 

您的代码允许实际火车大小超出最大火车大小,因此这可能是额外 20 个中的 10 个。此检查将确保您仅添加仍保持在最大值范围内的汽车。

与 Train counter 类似,它也可能溢出。

如果可以的话,尝试将这样的 while 循环转换为数学公式,这样更高效、更易读。

于 2013-01-15T04:31:08.123 回答
0

你的火车最后排的太长了

    while (intActualTrainSize < intMaxTrainSize)
    {
        intActualTrainSize=intActualTrainSize+NORMAL_CAR_LENGTH;
        intCarCounter++;
    } 

由于您将执行此循环,直到火车太长...

于 2013-01-15T04:36:02.573 回答
0

输入变量名,不推荐使用一个字符的变量名。在循环中增加一个带有固定值的计数器随乘法一起提供,不需要迭代。在循环的最后一次迭代中,实际列车大小超过了最大值。如果用户提供的最大火车尺寸低于 FIRST_CAR_LENGTH 怎么办?首先计算实际列车尺寸的方法是正确的。如果存在过剩,哈桑的回答似乎是不正确的。

const double occ = 0.25;

int track;
int max_train;

int cap;
int num_car;
int num_train = 0;
int train = 0;

printf( "Length of track? "); scanf( "%d", &track );
printf( "Max length of train? "); scanf( "%d", &max_train );

num_car = std::max( 0, max_train - FIRST_CAR_LENGTH + NORMAL_CAR_LENGTH ) / NORMAL_CAR_LENGTH;
if( 0 < num_car )
{
    train = FIRST_CAR_LENGTH + NORMAL_CAR_LENGTH * ( num_car - 1 );
    num_train = ( int ) ( occ * track ) / train;
}
cap = CAR_CAPACITY * num_car * num_train;
printf( "Capacity: %d; Train surplus: %d\n", cap, max_train - train );
于 2015-09-22T16:24:11.960 回答