新手求教将数列1,3,3,3,5,5,5,5,5,7,7,7,7,7,7,7的前40项按顺序存储到数组a中(a[0]=1)这道题
将数列1,3,3,3,5,5,5,5,5,7,7,7,7,7,7,7的前40项按顺序存储到数组a中(a[0]=1)这道题怎么做?#include<stdio. h>
#include<stdlib. h>
#define N 40
void main()
int a[N],i=1,s=1,j;
a[0]=1;
#include <stdio.h> int main( void ) { enum{ N=40 }; int a[N]; size_t i = 0; for( int j=1; i!=N; j+=2 ) for( int k=0; k!=j && i!=N; ++k ) a[i++] = j; for( size_t i=0; i!=N; ++i ) printf( "%d%c", a[i], " \n"[i+1==N] ); }
#include <stdio.h> int main( void ) { enum{ N=40 }; int a[N]; for( int i=0,p=1,q=0; i!=N; ++i ) { a[i] = p; ++q; if( q == p ) p+=2, q=0; } for( size_t i=0; i!=N; ++i ) printf( "%d%c", a[i], " \n"[i+1==N] ); }
#include <stdio.h> #include <math.h> int main( void ) { enum{ N=40 }; int a[N]; for( size_t i=0; i!=N; ++i ) a[i] = 1 + 2*(int)floor(sqrt(i+0.0)); for( size_t i=0; i!=N; ++i ) printf( "%d%c", a[i], " \n"[i+1==N] ); }