C language series operation title

A first two stages A1 = 1, A2 = 1, the later have the following relationship:

An = An-2 + 2An-1 (Note: n-2 and n-1 is a subscript)

The following procedure is prog1.c functions: turn requires M = n for an integer value corresponding to 100, 1000 and 10,000 determined so as to satisfy: Sn <M and Sn + 1> = M, where Sn = A1 + A2 + ... + An, and the n value are sequentially stored in the array element b [0], b [1 ] and b [2] Please prepare jsValue () function to achieve this function, after the function call writeDat () the array B [] in out.dat value is output to a file.
#include <stdio.h>

int b[3];

jsValue ()

{int a1=1,a2=1,a12,sn,k=2;

sn=a1+a2;

while(1)

{A12 = a1 + a2 * 2;

if(sn<100&&sn+a12>=100) b[0]=k;

if(sn<1000&&sn+a12>=1000) b[1]=k;

if(sn<10000&&sn+a12>=10000) {b[2]=k;break;}

sn = sn + a12;

a1 = a2; a2 = a12;

k++;

}

}

main()

{

jsValue ();

printf(“M=100,n=%d\nM=1000,n=%d\nM=10000,n=%d\n”,b[0],b[1],b[2]); writeDat();

}

writeDat()

{

FILE *fp;

fp=fopen(“out.dat”,“w”);

printf("%d\n%d\n%d\n",b[0],b[1],b[2]);

fprintf(fp,"%d\n%d\n%d\n",b[0],b[1],b[2]);

fclose(fp);

}

Operating results as follows:

M=100,n=6

M=1000,n=9

M=10000,n=11

Published 239 original articles · won praise 3 · Views 3166

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105164130