gpt4 book ai didi

C 数学运算顺序

转载 作者:行者123 更新时间:2023-12-04 05:15:30 28 4
gpt4 key购买 nike

我有一个我一直在做的作业。我的代码似乎有正确的数学运算和正确的顺序(至少在顺序上),但我的答案有点乱。我根据我的输入值正确获取每个问题的部分,但不会随着输入值的变化而得到相同的部分。是否有我遗漏的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com