谁能给我一个用C语言写的等差数列函数

如题所述

int fun(int a){ int b[9],i,j,k=0; //10个数字之间的差共有9个所以b数组为9 j=a[1]-a[0]; //现确定下等差 for(i=0;i<9;i++) b[i]=a[i+1]-a[i]; //将a数组中的前后两项的数的差值存入b数组中 for(i=0;i<9;i++){ if(j==b[i]) //判断b数组中的数是否于开始确定的差值相等 k++; } if(k==9) //判断是否所有的差值都相等 return 1;elsereturn 0;}main(){ int a[10],i,j; for(i=0;i<10;i++) scanf("%d",&a[i]); //输入10个数字 j=fun(a); //调用函数fun if(j==1) printf(" yes\n"); else printf(" no\n");}你没说要判断的数列是多长我自己定义了10个,你可以自己改。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-19
int function(int *a, int num) // num 为数组总数, 指针a 指向要检验的数组的首地址。
{
int NUMBER = a[num - 1 ] - a[num - 1 - 1];
--num;
while ( --num >= 0 )
if ( a[num] - a[num - 1] != NUMBER )
return 1; // 不是等差数组, 返回1

return 0; // 是等差数组
}
第2个回答  2013-11-19
#include "stdio.h"int count=0; // 若count为0,即为等差数列int compete(int a[],int n){ int i,d; if(n==1) return 0; else{ d=a[2]-a[1]; for(i=0;i<n-1;i++) if((a[i+1]-a[i])!=d) count++; }return count;}
第3个回答  2013-11-19
include<stdio.h>
int compute(int,int,int);
void main()
{
int a1,d,n,sn;
printf("输入a1:");
scanf("%d",&a1);
printf("输入等差d:");
scanf("%d",&d);
printf("输入项数n:");
scanf("%d",n);
sn=compute(a1,d,n);
printf("前n项和为%d",sn);
}
int compute(int a1,int d,int n)
{
return 2/(n*(a1+((n-1)*d)));
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网