Java编程题汇总 代码设计

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 152 收藏 0 点赞 0 评论 0

代码设计
考虑方程式:a^3 + b^3 = c^3 + d^3
其中:“^”表示乘方。a、b、c、d是互不相同的小于30的正整数。
这个方程有很多解。比如:
a = 1,b=12,c=9,d=10 就是一个解。因为:1的立方加12的立方等于1729,而9的立方加10的立方也等于1729。
当然,a=12,b=1,c=9,d=10 显然也是解。
如果不计abcd交换次序的情况,这算同一个解。
你的任务是:找到所有小于30的不同的正整数解。把a b c d按从小到大排列,用逗号分隔,每个解占用1行。比如,刚才的解输出为:
1,9,10,12
不同解间的顺序可以不考虑。
代码如下:
public class test08 {
public static void main(String[] args) {
int i,j,k,p;
for(i=1;i<30;i++){
for(j=i+1;j<30;j++){
for(k=j+1;k<30;k++){
for(p=k+1;p<30;p++){
if(i*i*i+j*j*j==k*k*k+p*p*p||i*i*i+k*k*k==j*j*j+p*p*p
||i*i*i+p*p*p==j*j*j+k*k*k){
System.out.println(i+”,”+j+”,”+k+”,”+p);
}
}
}
}
}
}
}
或者:
public class p8 {
public static void main(String[] args) {
for(int a=1;a<30;a++)
for(int b=a;b<30;b++)
for(int c=a;c<30;c++)
for(int d=c;d<30;d++){
double a3=Math.pow(a,3);
double b3=Math.pow(b,3);
double c3=Math.pow(c,3);
double d3=Math.pow(d,3);
if(a3+b3-c3-d3==0 &&a!=b && a!=c && a!=d && b!=c && b!=d &&c!=d)
System.out.println(a+”,”+c+”,”+d+”,”+b);}
}
}
代码设计
整数的分划问题。
如,对于正整数n=6,可以分划为:
6
5+1
4+2, 4+1+1
3+3, 3+2+1, 3+1+1+1
2+2+2, 2+2+1+1, 2+1+1+1+1
1+1+1+1+1+1+1
现在的问题是,对于给定的正整数n,编写算法打印所有划分。
用户从键盘输入 n (范围1~10)
程序输出该整数的所有划分。
代码如下:
import java.io.*;
import java.util.*;
public class test9 {
static String ss=””;
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in );
System.out.println(“输入1~10的数:”);
int a=in.nextInt() ;
String str=””;
for(int i=a;i>0;i–)
{
if(i==a)
{
System.out.println(a);
}
else
{
ss=””;
Create(i+str,i,(a-i));
System.out.println(ss.substring(0,ss.length()-1));
}
}
}
//a:保存原来的字符,orign:分解后还需要分解的数。other:分解后的数还需0ther数相加才能等于原来的数
public static void Create(String a,int orig,int other)
{
if(other==0)
{
ss+=a+”,”;
}
for(int i=other;i>0;i–)
{
if(i<=orig)//确保减一的数大于还需要分解的数
{
Create(a+”+”+i,i,other-i);
}

}

}

}
代码设计
一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。
例如:
当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。
当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。
当N=5时,92727满足条件。
实际上,对N的每个取值,可能有多个数字满足条件。

程序的任务是:求N=21时,所有满足条件的花朵数。注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。
如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。因为这个数字很大,请注意解法时间上的可行性。要求程序在3分钟内运行完毕。
代码如下:
import java.math.BigInteger;
import java.util.Arrays;

public class Narcissistic {
private static BigInteger[] table = new BigInteger[10];

public static void main(String[] args) {
long time = System.nanoTime();
find(21);
time = System.nanoTime() – time;
System.out.println(time / 1000000000.0 + “s”);
}

public static void find(int n) {
for (int i = 0; i < 10; i++)
table = BigInteger.valueOf(i).pow(n);
int[] nums = new int[n];
int index = 0;
int num = 0;
BigInteger sum = BigInteger.ZERO;
BigInteger MIN = BigInteger.TEN.pow(n – 1);
BigInteger MAX = BigInteger.TEN.pow(n).subtract(BigInteger.ONE);
while (true) {
if (index < nums.length && num < 10) {
BigInteger temp = sum.add(table[num]);
if (temp.compareTo(MAX) < 0) {
nums[index] = num;
index++;
sum = temp;
continue;
}
} else if (index >= nums.length && sum.compareTo(MIN) > 0) {
int[] temp = getArray(sum);
if (check(nums, true, temp, false))
System.out.println(sum);
} else if (index <= 0) {
break;
}
index–;
num = nums[index];
sum = sum.subtract(table[num]);
num++;
}
}

public static boolean check(int[] a1, boolean copy1, int[] a2, boolean copy2) {
if (a1.length != a2.length)
return false;
if (copy1)
a1 = a1.clone();
if (copy2)
a2 = a2.clone();
Arrays.sort(a1);
Arrays.sort(a2);
return Arrays.equals(a1, a2);
}

public static int[] getArray(BigInteger big) {
String s = String.valueOf(big);
int length = s.length();
int[] res = new int[length];
for (int i = 0; i < length; i++)
res = s.charAt(i) – ‘0’;
return res;
}
}

评论列表
文章目录