整数倒置和求水仙花数

 

 

#include<stdio.h>
#include<stdlib.h>
void numReverse(int num);
void numReverseWithoutArr(int num);
void main(){
	numReverse(123459);
	getchar();
}
void shuiXian(){
	//求水仙花数(一个三位数,其各位数字的立方和等于该数本身)
	int num, f, s, t;
	for (num = 100; num <= 999; num++){
		f = num % 10;
		s = num % 10 / 10;
		t = s / 100;
		if (num == f*f*f + s*s*s + t*t*t){
			printf("%d,%d,%d,%d\n", num, f, s, t);
		}
	}
}
int get10(int n ){ //获取n*10的数
	int res = 1;
	for (int i = 0; i < n; i++){
		res *= 10;
	}
	return res;
}
void numReverse(int num){ //整数倒置, 用str数组暂存数据
	int str[10];
	int i = 0;
	int length; //变化后的数组长度
	int res = 0;
	int j = 0;

	for (;num;i++){
		str[i] = num % 10;
		num /= 10;
		
	}
	length = i; 
	
	while (j < length){
		res += str[j] *get10(length-j-1);
		j++;
	}
	printf("%d", res);
}

void numReverseWithoutArr(int num){ //整数倒置, 不用str数组暂存数据
	//123
	int res = 0;
	for (int i = 0;num; i++){
		res = num % 10 *get10(i-1);
		num /= 10;
	}
	printf("%d", res);
}

 

转载于:https://www.cnblogs.com/luoxuw/p/11258468.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值