关于整数正序分解和整数逆序

1.正序分解,举例如下:

#include<stdio.h>
int main()
{
	int x;      //输入一个整数
	scanf("%d",&x);

	int mask = 1;
	int t = x;
	while (t > 9){      //判断整数有多少位
		t /= 10;
		mask *= 10;
	}
	do{
		int d = x / mask;
		printf("%d", d);
		if (mask > 9){
			printf(" ");
		}
		x %= mask;
		mask /= 10;
	} while (mask > 0);
	printf("\n");	
	return 0;
}

 

2.整数逆序

 

#include<stdio.h>
int main()
{
	int x;       //输入一个整数
	scanf("%d", &x);
	int n = x;
	int t = 0;

	while (n > 0){
		int d = n % 10;
		t = t * 10 + d;
		n /= 10;
	}
	printf("%d 的逆序数为 %d\n", x,t);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/zkk258369/article/details/81167220