又是!斐波纳契数列!!
#include <stdio.h>
int show (int);
int main (void)
{
int n,num;
printf("please:\n");
scanf("%d",&n); /*读取要取个数*/
num=show (n); /*返回会值*/
printf("num=%d",num);
return 0;
}
int show (int n)
{
int a[60]={1,1};
int i;
for (i=2;i<n;i++) /*存入数组(自动更)*/
*(a+i)=*(a+i-2)+*(a+i-1);
return *(a+n-1); /*在数组中提取数据*/
}
这样可以
在内存分配上还存在不太好的地方!帮帮我!!想用malloc()来但不知何做好?
我想:
读取要取个数
存入数组(自动更)
在数组中提取数据
返回会值
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int * ptd;
int max;
int i=0;
printf("you want your ./pormax number:\n");
while (scanf("%d" ,&max) ==1)
{
ptd = (int *)malloc (max*sizeof(int));
if (ptd ==NULL)
{
printf("memory allocation failed .bye.");
exit (EXIT_FAILURE);
}
ptd[0] =1;
ptd[1] =1;
for (i=2; i <max; i++)
*(ptd+i)=*(ptd+i-2)+*(ptd+i-1);
printf("here are your %d entteries\n",i);
printf("the %d number enteredis:%d\n",i, *(ptd+i-1));
printf("please key your now want number or");
printf("ket 'q' to exit.\n");
free (ptd);
}
printf("you key is not anumber.bye.\n");
return 0;
}