当我输入 6 (int) 作为值时,运行它会给我一个 seg 错误(gcc filename.c -lm)。请帮我解决这个问题。预期的功能尚未实现,但我需要知道为什么我已经陷入了段错误。
谢谢!
#include<stdio.h>
#include<math.h>
int main (void)
{
int l = 5;
int n, i, tmp, index;
char * s[] = {"Sheldon", "Leonard", "Penny", "Raj", "Howard"};
scanf("%d", &n);
//Solve Sigma(Ai*2^(i-1)) = (n - k)/l
if (n/l <= 1)
printf("%s\n", s[n-1]);
else
{
tmp = n;
for (i = 1;;)
{
tmp = tmp - (l * pow(2,i-1));
if (tmp <= 5)
{
// printf("Breaking\n");
break;
}
++i;
}
printf("Last index = %d\n", i); // ***NOTE***
//Value lies in next array, therefore
++i;
index = tmp + pow(2, n-1);
printf("%d\n", index);
}
return 0;
}