加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

整数倒置和求水仙花数

发布时间:2020-12-14 04:47:49 所属栏目:百科 来源:网络整理
导读:? ? #includestdio.h#includestdlib.hvoid numReverse(int num);void numReverseWithoutArr(int num);void main(){numReverse(123459);getchar();}void shuiXian(){//求水仙花数(一个三位数,其各位数字的立方和等于该数本身)int num,f,s,t;for (num = 100

?

?

#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,%dn",num,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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读