C 数学运算顺序

标签 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;
}

最佳答案

您使用循环来计算火车计数器和汽车计数器,作为另一种选择,您可以使用整数算术来计算这些值。我附上了我的解决方案,不涉及循环。它使调试更容易。我提供两个版本。版本 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;
}

关于C 数学运算顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14331155/

相关文章:

c - 嵌入式 C UART 约定

objective-c - 我如何在 Objective-C 中播种 rand() 函数?

python - 为什么 fmod(1.0,0.1) == .1?

haskell - 神秘的串行端口行为

programming-languages - 为什么结合性是运算符的基本属性而不是优先级的基本属性

c++ - 如何使用 Telnet 库从服务器发送查询并从客户端接收回显

c - 数组初始化程序返回什么?

mysql - 如何从mysql上的多个表计算账单?

r - 点边标签中的下标和希腊字母

c - c中运算符的优先级