2017北航题练习

题目为:

回文串

给一个数,比如567,将它和其回文数765相加,得到1332,如果1332是回文的,则停止,将整个运算过程输出;否则从1332开始继续重复上述流程。

#include 
#include 
#include 
int huiwenshu(int a) 
{
	//int a;用来输入数值
	//scanf("%d", &a);
	int h[100];
	int i = 0;
	while (a / 10 > 0) //求回文数
	{
		h[i] = a % 10;
		a = a / 10;
		i++;
	}
	h[i] = a % 10;
	/*for (int j = 0; j <= i; j++)
	{
	printf("%d", h[j]);
	}*/
	int k; k = i;
	int b = 0;//得到一个数的回文数
	for (int j = 0; j <= i; j++)
	{
		b = b + h[j] * pow(10, k);
		k = k - 1;
	}
	//printf("%d", b);
	return b;
}
bool IsHui(int a) 
{
	int b;
	b = huiwenshu(a);
	if (a == b)
		return true;
	else
		return false;
}
int main(void) 
{
	int shuru;
	scanf("%d", &shuru);
	int hui;
	hui = huiwenshu(shuru);
	int sum;
	sum = shuru + hui;
	while (!IsHui(sum)) 
	{
		printf("过程中间的sum%d\n", sum);
		shuru = sum;
		hui = huiwenshu(shuru);
		sum = shuru + hui;
	}
	printf("终于为回文数 %d\n", sum);
	/*printf("%d", huiwenshu(shuru));*/
	system("PAUSE");
	return 0;
}

 

你可能感兴趣的:(专业课学习)