C数学运算顺序

时间:2013-01-15 04:24:56

标签: c math operator-precedence

我有一份我一直在做的工作。我的代码似乎具有正确的数学运算和正确的顺序(至少顺序),但我的答案有点棘手。我根据输入值得到了每个问题的一部分,但输入值从未与输入值相同。有什么关于我缺少的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 个答案:

答案 0 :(得分:2)

您使用循环来计算列车计数器和汽车计数器,相反,您可以使用整数运算来计算这些值。我附上了我的解决方案,不涉及循环。它使调试更容易。我提供两个版本。版本1 sassumes整数除法不圆整。版本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;
}

答案 1 :(得分:1)

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

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

您的代码允许实际列车大小超出最大列车大小,因此可能是额外20的10。此检查将确保您只添加车辆,如果它仍然保持在最大值。

与列车计数器类似,也可能会溢出。

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

答案 2 :(得分:0)

你的火车在说明

的路线上结束的时间太长了
    while (intActualTrainSize < intMaxTrainSize)
    {
        intActualTrainSize=intActualTrainSize+NORMAL_CAR_LENGTH;
        intCarCounter++;
    } 

因为你将执行这个循环,直到火车太长......

答案 3 :(得分: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 );