我在 C 之前学习了 Objective-C,现在我要回到 C,我不明白为什么printf()
循环内部不起作用?有人可以建议我吗?
如果有人想知道,该程序是 Skiena 和 Revilla 所著“编程挑战”一书中的第一个挑战。
#include <stdio.h>
#include <stdbool.h>
static int inputInt;
static int secondInt;
int returnCycleNumber(int givenNumber);
int returnCycleNumber(int givenNumber) {
bool initial = true;
int counter = 1;
do
{
if (givenNumber % 2 != 0)
{
givenNumber = givenNumber * 3 + 1;
counter = counter + 1;
printf("\n%i", givenNumber);
}
else
{
givenNumber = givenNumber / 2;
counter = counter + 1;
printf("\n%i", givenNumber);
}
if (givenNumber == 1) {
initial = false;
}
} while (initial == true && givenNumber > 1);
return counter;
}
int main(int argc, const char * argv[])
{
scanf("%i %i", &inputInt, &secondInt);
fflush(stdout);
int arrayCount[secondInt];
for (int counter = 0; counter == (secondInt - inputInt); counter++ ) {
arrayCount[counter] = returnCycleNumber(inputInt + counter);
}
printf("\n%i", arrayCount[1]);
return 0;
}