2, the reverse order of an integer number of methods

2, the reverse order of an integer number of methods

A, reverse recursion : enter a positive integer n, which is output in reverse order. It requires the definition of the function calls and Reverse (num), whose function is to reverse num output required to achieve recursive.
Second, the classical common : the key code is d = x% 10; ret = ret * 10 + d;
three simpler : only for three integers
four, more complex : three integers only for
five :

Description: x / 100 x have the hundreds digit, x% 100 is removed hundreds digit of x, x% 10 available bits of x, and so on.

//一、递归法逆序:
#include<stdio.h>
void reverse(int n);
int main(){
 	int n;
 	scanf("%d", &n);
 	reverse(n);
	return 0;
}
void reverse(int n){
 	if(n<10){
  		printf("%d",n);
	}else{
  		printf("%d",n%10);
  		reverse(n/10);
 	}
 }
//二、经典常用的:
#include <stdio.h>
int main(){
	int x, d, ret=0;
	scanf("%d", &x);
	while( x > 0){
		d = x%10;
		ret = ret*10 + d;
		x /=10;
	}
	printf ("%d", ret); 
	return 0;
} 
//三、更简单的(仅针对三位整数):
#include<stdio.h>
int main()
{
	int digit= 0, a ,b , c;
	scanf ("%d", &digit);
	
	a= digit%10;		//a为个位
	b= digit%100/10;	//b为十位
	c= digit/100;		//c为百位
	digit= a*100 +b*10 +c;
	printf ("%d", digit);
	return 0;
 }
//四、更复杂的(仅针对三位整数):
#include <stdio.h>
int main(){
	int figure, key; 
	scanf("%d",&figure);
	
	if(figure%10==0){
		if(figure%100==0){
			key=figure/100;					//个位十位均为0的情况
		}else{
			key=figure/100+figure%100;		//仅个位为0的情况
		}
	}else{
		if(figure%100<10 ){
			key=figure/100+figure%100*100;	//仅十位为0的情况
		}else{
			key=figure/100+figure%100%10*100+figure%100/10*10;
		}									//个位十位均不为0的情况
	}
	printf("%d",key);
	return 0;
}
//五、
Published 16 original articles · won praise 0 · Views 333

Guess you like

Origin blog.csdn.net/NAU_LHT/article/details/104144239