书上的练习题,自己不会做,在网上复制别人的代码的,自己不理解,而且出现很多错误!
/*需求:打印出1~10000范围中的所有“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数立方和等于该数本身。
思路:1,水仙花数是一个三位数,所以设置三个变量,
2,这三个变量的立方相加等于这三个变量自由组合的数字。
步骤:1,定义三个变量a,b,c.这三个变量为整数。
2,a的立方+b的立方+c的立方=abc,acb,bac,bca,cab,cba.
*/
/*
public class Test01{
public static void main(String arg[])
{
for(int x=100;x<10000;x++)
if(isNumber(x))
System.out.println(x+"is Number");
}
static Boolean isNumber(int x)
{
int i,j,k;
i=x/100;
j=(x-i*100)/10;
k=x%10;
if(x==i*i*i+j*j*j+k*k*k)
return true;
else
return false;
}
}
153,370,371,407,这个只把三位数算在里面了,还把1000.1001算在里面啦!为什么没有显示4位数的水仙花数。
*//*
public class Test01{
public boolean fun(int i) {
int s = 0;
int n = (i + "").length();
for (int k = 1; k <= n; k++) {
int t1 = (int) (i / Math.pow(10, k - 1));
int t2 = (int) Math.pow((t1 % 10), n);
s += t2;
}
if (s == i) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Test sub = new Test();
for (int a = 100; a <= 999999999; a++) {
if (sub.fun(a)) {
System.out.println(a + "是水仙花数");
}
}
}
}
*/
/*需求:打印出1~10000范围中的所有“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数立方和等于该数本身。
思路:1,水仙花数是一个三位数,所以设置三个变量,
2,这三个变量的立方相加等于这三个变量自由组合的数字。
步骤:1,定义三个变量a,b,c.这三个变量为整数。
2,a的立方+b的立方+c的立方=abc,acb,bac,bca,cab,cba.
*/
/*
public class Test01{
public static void main(String arg[])
{
for(int x=100;x<10000;x++)
if(isNumber(x))
System.out.println(x+"is Number");
}
static Boolean isNumber(int x)
{
int i,j,k;
i=x/100;
j=(x-i*100)/10;
k=x%10;
if(x==i*i*i+j*j*j+k*k*k)
return true;
else
return false;
}
}
153,370,371,407,这个只把三位数算在里面了,还把1000.1001算在里面啦!为什么没有显示4位数的水仙花数。
*//*
public class Test01{
public boolean fun(int i) {
int s = 0;
int n = (i + "").length();
for (int k = 1; k <= n; k++) {
int t1 = (int) (i / Math.pow(10, k - 1));
int t2 = (int) Math.pow((t1 % 10), n);
s += t2;
}
if (s == i) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Test sub = new Test();
for (int a = 100; a <= 999999999; a++) {
if (sub.fun(a)) {
System.out.println(a + "是水仙花数");
}
}
}
}
*/
{
public static void main(String[] args)
{
//x用来临时保存百位十位和个位
int x=0,y=0,temp=0; for(int i = 100;i<150;i++)
{
//System.out.println("i:" + i);
//temp=i;
x=i/100; // 取出百位 3 345
y=x*x*x; //用y计数,得到了百位数的立方和。
//System.out.println("y_1:" + y);
i-=x*100;// 得到2位数 45
x=i/10;//取得十位上的数字4
y+=x*x*x; //把第二位数的立方和加上 y=9+64
//System.out.println("y_2:" + y); i-=x*10; //得到个位上的数
y+=i*i*i;
//System.out.println("y_3:" + y); if(i==y)
{
System.out.println("temp:" + temp);
} } }
}
你也可以用Math.pow()这个方法算立方值
{
public static void main(String args[])
{
A aa = new A();
}
}class A {
private int a, b, c, d, sum;
public A(){
for(a=100; a<1000; a++){
b = a/100;
c = (a-b*100)/10;
d = a%10;
sum = b*b*b + c*c*c + d*d*d;
if(sum == a)
System.out.println(a);
}
}
}楼主学过其他语言吗?
可以好好想想其流程
这个和那个一样——
class kl {
public static void main(String args[]) {
int a, b, c, d, sum;
for (a = 100; a < 1000; a++) {
b = a / 100;
c = (a - b * 100) / 10;
d = a % 10;
sum = b * b * b + c * c * c + d * d * d;
if (sum == a)
System.out.println(a);
}
}
}
水仙花数十三位数,是1~1000以内,楼主你怎么都10000了
import java.math.*;
import java.util.*;public class NarcissisticNumber { public static void main(final String[] args) {
System.out.println("水仙花数列表");
for (BigInteger bigInteger = new BigInteger("0");
!bigInteger.equals(new BigInteger("115132219018763992565095597973971522402"));
bigInteger = bigInteger.add(new BigInteger("1"))) {
if (isNarcissisticNumber(bigInteger)) {
System.out.println(getDateTime() + "\t" + bigInteger);
}
}
} /**
* 判断一个数是否为水仙花数:一个N位整数,其各位数字的N次方的和等于该数本身
*
* @param number
* @return 当输入的参数为水仙花数时返回true,否则返回false
*/
public static boolean isNarcissisticNumber(final BigInteger number) {
BigInteger sumOfDigitPower = new BigInteger("0"); // 各位数字的N次方的和
char[] digitArray = number.toString().toCharArray(); // 各位数字的数组
for (char digit : digitArray) {
sumOfDigitPower = sumOfDigitPower.add( // 求和
BigInteger.valueOf(
Character.digit(digit, 10) // 各位数字
).pow(digitArray.length) // N次方
);
}
return sumOfDigitPower.equals(number);
} /**
* @return 返回包含当前日期和时间的字符串
*/
public static String getDateTime() {
Calendar calendar = Calendar.getInstance();
return ""
+ calendar.get(Calendar.YEAR) + "/"
+ (calendar.get(Calendar.MONTH) + 1) + "/"
+ calendar.get(Calendar.DATE) + " "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + ":"
+ calendar.get(Calendar.SECOND) + ":"
+ calendar.get(Calendar.MILLISECOND);
}
}
运行结果:
水仙花数列表
2014/6/7 18:3:12:297 0
2014/6/7 18:3:12:323 1
2014/6/7 18:3:12:324 2
2014/6/7 18:3:12:324 3
2014/6/7 18:3:12:324 4
2014/6/7 18:3:12:324 5
2014/6/7 18:3:12:324 6
2014/6/7 18:3:12:324 7
2014/6/7 18:3:12:325 8
2014/6/7 18:3:12:325 9
2014/6/7 18:3:12:329 153
2014/6/7 18:3:12:336 370
2014/6/7 18:3:12:336 371
2014/6/7 18:3:12:337 407
2014/6/7 18:3:12:353 1634
2014/6/7 18:3:12:398 8208
2014/6/7 18:3:12:402 9474
2014/6/7 18:3:12:613 54748
2014/6/7 18:3:12:689 92727
2014/6/7 18:3:12:689 93084
2014/6/7 18:3:13:484 548834
2014/6/7 18:3:15:395 1741725
2014/6/7 18:3:19:467 4210818
2014/6/7 18:3:28:652 9800817
2014/6/7 18:3:28:857 9926315
2014/6/7 18:3:53:991 24678050
2014/6/7 18:3:53:992 24678051
2014/6/7 18:5:45:778 88593477
2014/6/7 18:7:28:62 146511208
2014/6/7 18:17:30:115 472335975
2014/6/7 18:19:24:537 534494836
2014/6/7 18:31:6:765 912985153上面是前32个水仙花数
对于10进制数来讲,地球上总共有89个水仙花数,最大的是115132219018763992565095597973971522401
由于没有“矿机”,想用个人电脑把所有水仙花数都打出来还是有一定难度的
final int[] cubes = {0,1,8,27,64,125,216,343,512,729};
for(int a = 1; a < 10; ++a)
for(int b = 0; b < 10; ++b)
for(int c = 0; c < 10; ++c){
int r = cubes[a] + cubes[b] + cubes[c];
if(r == 100 * a + 10 * b + c)
System.out.println(r);
}
}
public class Main {
public static void main(String[] args) {
int k,m,n;
for(int i=101;i<1000;i++){
k=Integer.parseInt((i+"").substring(0, 1));//百位
m=Integer.parseInt((i+"").substring(1, 2));//十位
n=Integer.parseInt((i+"").substring(2, 3));//个位
if((k*k*k+m*m*m+n*n*n)==i)
System.out.println(i);
}
}
}
换一种:public class Main {
public static void main(String[] args) {
int k,m,n;
char c[]=new char[3];
for(int i=101;i<1000;i++){
c=(i+"").toCharArray();
k=c[0]-'0';//百位
m=c[1]-'0';//十位
n=c[2]-'0';//个位
if((k*k*k+m*m*m+n*n*n)==i)
System.out.println(i);
}
}
}
{
public static void main(String args[])
{
shuixianhua sxh = new shuixianhua();
}
}
class shuixianhua {
private int a, b, c, d, sum;
public shuixianhua(){
for(a=100; a<1000; a++){
b = a/100;
c = (a-b*100)/10;
d = a%10;
sum = b*b*b + c*c*c + d*d*d;
if(sum == a)
System.out.println(a);
}
}
}
int i;
static final int MAX = X;// (X填写你要寻找的水仙花数的范围)
int a,b,c;
for(i=0;i<=X;i++)
{
a=pow(i/100,3);//百位~
b=pow((i%100)/10,3);//十位!
c=pow((i%10),3);//个位`
if((a+b+c)==i)
{
System.out.println("The Flower number is " + i);
}
}
public class Test3 { /**
* 题目:打印出所有的 "水仙花数 ",
* 所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。
* 例如:153是一个 "水仙花数 ",
* 因为153=1的三次方+5的三次方+3的三次方。
*/
static int num1;
static int num2;
static int num3;
public static void main(String[] args) {
Test3.showInfo(1000);
} public static void showInfo(int n)
{
for(int sum=101; sum<n; sum++)//三位数,100不可能是
{
num1 = sum / 100;//第一位
num2 = sum % 100 / 10;//求余,两位,除10小数点左移,得第二位
num3 = sum % 10;//求余,一位,得第三位
if((num1*num1*num1 + num2*num2*num2 + num3*num3*num3) == sum)
{
System.out.println(sum+"是一个水仙花数");
}
}
}
}
int a,b,c,s;
for(int i = 100; i <= 999; i++){
a = i / 100;
b = i % 100 / 10;
c = i % 10;
s = a*a*a + b*b*b + c*c*c;
if(i == s){
System.out.println(i);
}
}
}
* 打印出1~10000范围中的所有“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数立方和等于该数本身。
* @author lenovo
*
*/
public class TestNarcissus {
static int result = 0;
static int unit=0;//各位
static int decade = 0;//十位
static int hunder = 0;//百位
public static int accountNumber(int a,int b,int c){
result = a*a*a+b*b*b+c*c*c;
return result;
}
public static boolean Calculation(int toCal){
hunder = toCal/100;
decade = (toCal-100*hunder)/10;
unit = toCal - 100*hunder-10*decade;
if(toCal==accountNumber(hunder, decade, unit)){
return true;
}
return false;
}
public static void main(String[] args){
for(int i = 1;i<10000;i++){
if(Calculation(i)){
System.out.println(i);
}
}
}
}
public class Narcissistic{ public static boolean narcissistic(int num){
boolean flag = false;
int n=0, sum=0, copy=num;
n = (num+"").length();
for(int i=0; i<n; i++){
sum += (int)Math.pow(num%10, n);
num /= 10;
}
if(sum == copy)
flag = true;
return flag;
} public static void main(String args[]){
for(int i = 0; i<=99999; i++){
if(narcissistic(i))
System.out.print(i+" ");
}
}
}