功能: 根据里程计算车速
原型:
int CalcSpeedOfCar(int iCurrentMileage, int * piCarSpeed);
输入参数:
int pcCurrentMileage:当前里程数(为对称数)(pcCurrentMileage ≤ 999999)
输出参数:
汽车速度(公里/小时)
返回值:
0:成功计算出车速
1:里程范围内没有回文数,无法计算出车速
#include <stdlib.h>
int IsSymmetryNum(int num)
{
int numIn = num;
int cnt = 0;
int *tempInt = NULL;
while(numIn!=0)
{
cnt++;
numIn = numIn/10;
}
tempInt = (int *)malloc(cnt*sizeof(int));
cnt = 0;
numIn = num;
while (numIn!=0)
{
tempInt[cnt] = numIn%10;
cnt++;
numIn = numIn/10;
}
for (int i=0;i<(cnt/2);i++)
{
if (tempInt[i] != tempInt[cnt-i-1])
{
free(tempInt);
return -1;//no
}
}
free(tempInt);
return 1;//yes
}
int CalcSpeedOfCar(int iCurrentMileage, int * piCarSpeed)
{
int i = 0;
/*在这里实现功能*/
if ((iCurrentMileage>999999)||(iCurrentMileage<0))
{
return -1;
}
for (i=iCurrentMileage+1;i<=999999;i++)
{
if (1 == IsSymmetryNum(i))
{
*piCarSpeed = (int)((i-iCurrentMileage)/2);
return 0;
break;
}
}
return 1;
}
int main()
{
int spd=0;
int n= CalcSpeedOfCar(95859,&spd);
return 0;
}