在改章节中,我们主要介绍立方输出的内容,自我感觉有个不错的建议和大家分享下
每日一道理
父亲对于儿子来说,是座耸立的高山,而儿子只是颗石子,源于山,却并不了解山。生活中诸多爱的密码,是需用细节来解读的,在亲情的沃土上,要想搞得最美的果实,惟有期待那存在于瞬间的心与心的共鸣,爱与爱的默契。
父亲对于儿子来说,是座耸立的高山,而儿子只是颗石子,源于山,却并不了解山。生活中诸多爱的密码,是需用细节来解读的,在亲情的沃土上,要想搞得最美的果实,惟有期待那存在于瞬间的心与心的共鸣,爱与爱的默契。
/*
考虑方程式: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
不同解间的次序可以不考虑。
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
import java.util.List;
public class 立方和等式 {
public static int[] a = new int [4];
public static boolean[] vis = new boolean[4];
public static List<int[]> lis = new ArrayList<int[]>();
private static void print(List<int[]> lis) {
for(int i=0;i<lis.size();i++){
for(int j=0;j<lis.get(i).length-1;j++){
System.out.print(lis.get(i)[j]+",");
}
System.out.println(lis.get(i)[lis.get(i).length-1]);
}
}
public static boolean check(List<int[]> lis,int[] n){
if(lis.size()==0){
return true;
}else{
int count = 0;
for(int i=0;i<lis.size();i++){
for(int j=0;j<n.length;j++){
if(lis.get(i)[j]!=n[j]){
count = 0;
break;
}else{
count++;
}
}
if(count==4) return false;
}
}
return true;
}
public static boolean check(int[] n){
Set<Integer> sets = new HashSet<Integer>();
for(int i=0;i<n.length;i++){
sets.add(n[i]);
}
if(sets.size()==4){
return true;
}
return false;
}
public static void oper(int[] n){
int a = (int)Math.pow(n[0], 3);
int b = (int)Math.pow(n[1], 3);
int c = (int)Math.pow(n[2], 3);
int d = (int)Math.pow(n[3], 3);
if(a+b==c+d){
int[] temp = new int[]{n[0],n[1],n[2],n[3]};
Arrays.sort(temp); // 排序
if(check(lis,temp)){// 不重复,添加
lis.add(temp);
}
}
}
static void f(int m,int start,int end)
{
if(start==end)
{
if(check(a)){
oper(a);
}
return;
}
else
{
for(int i=1;i<m;i++)
{
if(!vis[start])//没有赋值
{
a[start]=i;
vis[start]=true;
f(m,start+1,end);
vis[start]=false;
}
}
}
}
public static void main(String[] args) {
int m = 30;
int n = 4;
f(m,0,n);
print(lis);
}
}
方法二
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class 立方和等式 {
// 输出
private static void print(List<int[]> lis) {
for (int[] i : lis) {
for (int j : i)
System.out.print(j + " ");
System.out.println();
}
}
public static boolean check(List<int[]> lis, int[] n) {
if (lis.size() == 0) {
return true;
} else {
for (int i = 0; i < lis.size(); i++) {
int j;
for (j = 0; j < n.length; j++) {
if (lis.get(i)[j] != n[j]) {
break;
}
}
if (j == n.length)
return false;
}
}
return true;
}
private static void f(List<int[]> lis) {
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
for (int k = 0; k < 30; k++) {
for (int m = 0; m < 30; m++) {
if (i == j || i == k || i == m || j == k || j == m
|| k == m)
continue;
int a = (int) Math.pow(i, 3);
int b = (int) Math.pow(j, 3);
int c = (int) Math.pow(k, 3);
int d = (int) Math.pow(m, 3);
if (a + b == c + d) {
int[] temp = new int[] { i, j, k, m };
Arrays.sort(temp); // 排序
if (check(lis, temp)) {// 不重复,添加
lis.add(temp);
}
}
}
}
}
}
}
public static void main(String[] args) {
List<int[]> lis = new ArrayList<int[]>();
f(lis); // 得到结果
print(lis); // 输出
}
}
运行结果
1,9,10,12
2,9,15,16
2,18,20,24
10,19,24,27
文章结束给大家分享下程序员的一些笑话语录: 很多所谓的牛人也不过如此,离开了你,微软还是微软,Google还是Google,苹果还是苹果,暴雪还是暴雪,而这些牛人离开了公司,自己什么都不是。
---------------------------------
原创文章 By
立方和输出
---------------------------------