下面的代碼放入一個app文件就行了。其中checkNumber(),frog_count1()和frog_count2()爲算法實現的三個函數,其他是測試及測試輔助函數,可以不要,如下:
#include"stdlib.h"
#include"stdio.h"
//fill the array
void fillArray(int *ptr,int m,int n);
//show the input array
void testArray(int * ptr, int m, int n);
//check out if number is in the array
bool checkNumber(const int * ptr, int num,int m,int n);
//frog can jump 1 or 2 steps one time
//param n is the steps need to jump, return the number of methods
int frog_count1(int n);
//frog can jump 1,2...n steps one time
//param n is the steps need to jump, return the number of methods
int frog_count2(int n);
int main()
{
/************************************************************************************************/
/*************************first one : find the value from the array*******************************/
/************************************************************************************************/
//input array
int m, n,val;
int * ptr = NULL;
m = n = 0;
printf("Input the row size of array: \n");
scanf("%d",&m);
printf("Input the colum size of array: \n");
scanf("%d",&n);
if(m > 0 && n > 0)
{
ptr = (int*)malloc(m * n * sizeof(int));
if(ptr == NULL)
{
printf("failed to allocate mem!\n");
return 0;
}
fillArray(ptr,m,n);
}
testArray(ptr,m,n);
printf("input the number you want to check: \n");
scanf("%d",&val);
//check if number is in the array
bool isIn = checkNumber(ptr,val,m,n);
printf("%d is%sin the array.",val,(isIn?" ":" not "));
/*********************************************************************************************/
/*************************second one : frog jumping quesion*******************************/
/*********************************************************************************************/
n = 0;
int count = 0;
printf("please input that how many steps you want the frog to jump first time :\n");
scanf("%d",&n);
if(n >= 0)
count = frog_count1(n);//1 or 2 steps one time
else
printf("there are no steps below the frog,you should know that\n");
printf("there are %d way%s to do this in case of 1 or 2 steps one time.\n",count, count == 1 ? "": "s");
printf("please input that how many steps you want the frog to jump second time :\n");
scanf("%d",&n);
if(n >= 0)
count = frog_count2(n);//1,2,3,....n times one time
else
printf("there are no steps below the frog,you should know that\n");
printf("there are %d way%s to do this in case of 1-%d steps one time.\n",count, count < 2 ? "": "s",n);
return 0;
}
void fillArray(int *ptr,int m,int n)
{
for(int i = 0; i < m; i++)
{
printf("please input the data for line %d,using space between the data : \n", i);
for(int j = 0; j < n; j++)
scanf("%d", ptr + i * n + j);
}
}
void testArray(int * ptr, int m, int n)
{
for(int i=0; i<m; i++)
{
for(int j = 0; j<n; j++)
{
printf("%3d ",*(ptr + i * n + j));
}
printf("\n");
}
}
bool checkNumber(const int *ptr, int num,int m, int n)
{
int i , j,x1,x2,y1,y2;
x1 = n - 1;
x2 = 0;
y1 = 0;
y2 = m - 1;
//ensure the num is in the range of [x2-x1] by colume
if(ptr[x1] == num) return true;
while(ptr[x1] > num)
{
x1--;
if(ptr[x1] == num)
return true;
}
if(ptr[n * (m - 1) + x2] == num) return true;
while(ptr[n * (m - 1) + x2] < num)
{
x2++;
if(ptr[n * (m - 1) + x2] == num)
return true;
}
//ensure the num is in the range of [y1-y2] by row
while(ptr[x1 + y1 * n] < num)
{
y1++;
if(ptr[x1 + y1 * n] == num)
return true;
}
while(ptr[x2 + y2 * n] > num)
{
y2--;
if(ptr[x2 + y2 * n] == num)
return true;
}
for(i = y1; i < y2 + 1; i++)
{
for(j = x2; j < x1 + 1; j++)
{
if(ptr[i * n + j] == num)
return true;
}
}
return false;
}
int frog_count1(int n)
{
int ret;
if(n == 0)
return 0 ;
if(n == 1)
ret = 1;
if(n == 2)
ret = 2;
if(n > 2)
ret = frog_count1(n - 1) + frog_count1(n - 2);
return ret;
}
int frog_count2(int n)
{
int ret = 1;
if(n == 1)
ret = 1;
if(n > 1)
{
while(n > 1)
{
ret += frog_count2(n-1);
n--;
}
}
return ret;
}