我正在做关于计算美洲驼数量的cs50代码,但它停在中间,没有返回任何东西。代码要求用户输入起始大小和结束大小的数量,然后计算达到结束大小需要多少年
注1:代码使用<cs50.h>header。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// TODO: Prompt for start size
int start_size;
do
{
start_size = get_int("enter the start size\n");
}
while (start_size < 9);
// TODO: Prompt for end size
int end_size;
do
{
end_size = get_int("enter the end size\n");
}
while (end_size < start_size);
// TODO: Calculate number of years until we reach threshold
int years = 0;
do
{
start_size = start_size + (start_size/3);
start_size = start_size - (start_size/4);
years++;
}
while (start_size < end_size);
// TODO: Print number of years
printf("the needed number of years is: %i\n",years);
}
注2:我试图在用scanf
替换get_int
后在在线IDE中执行代码,但仍然无法工作
1条答案
按热度按时间ppcbkaq51#
OP没有实现实际的问题陈述,即使用当年的起始人口进行 * 增长 * 和 * 收缩 * 计算。)
问题就在这里:
这实现了表达式:
n = 4/3 * n * 3/4
任何孩子都会告诉你,n = 12/12 * n
,意味着人口没有增加。简单的修复方法是不要在第二个语句中使用第一个语句的左值。在单个赋值语句中执行整个计算:
在这里,右侧的3个
start_size
都具有相同的值;今年的人口基数。最少9头 Camel 开始确保牛群将随着时间的推移而增长。一个较小的 * 种子 * 人口将不会得到牵引力(至少增加
1
),不会增长。编辑:
简化: