求车速

功能: 根据里程计算车速
原型:
    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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值