等差數列判斷算法

// 08A.cpp : Defines the entry point for the console application.

//




#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>


int _tmain(int argc, _TCHAR* argv[])
{
int isarithmetic(int array[],int n);
void bubble(int array[],int n);
int num,m,i=0,array1[100],array2[100][1000];
scanf_s("%d",&num);
while(num>0)
{
int j;
array1[i]=num;
for(j=0;j<num;j++)
{
scanf_s("%d",&array2[i][j]);
}
i++;
scanf_s("%d",&num);
}
for(m=0;m<i;m++)
{
bubble(array2[m],array1[m]); //此處array2[m]爲取二維數組中的一維構成一位數組
if(isarithmetic(array2[m],array1[m]))
printf("yes\n");
else
printf("no\n");
}
system("pause");
return 0;
}
/*
*isarithmetic:判斷數組是否爲等差數列
*/
int isarithmetic(int array[],int n)
{
int i,differ;
differ=array[0]-array[1];
for(i=1;i<n-1;i++)
{
if(differ!=array[i]-array[i+1])
return 0;
}
return 1;
}
//bubble sort
void bubble(int array[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章