zoukankan      html  css  js  c++  java
  • 立方和等式 考虑方程式:a^3 + b^3 = c^3 + d^3 其中:“^”表示乘方。a、b、c、d是互不相同的小于30的正整数。

    /*
    考虑方程式: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.Arrays;
    import java.util.List;
    import java.util.ArrayList;
    public class 立方和等式 {
    	// 输出
    	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;
    	}
    	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

    方法二:(用全排列实现)(m排n)

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Set;
    import java.util.HashSet;
    public class sortMN {
    	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);
    			}
    		}
    	}
    	public static void f(int m,int[] n,int k) {
    		if(k==n.length){
    			if(check(n)){
    				oper(n);
    			}
    			return ;
    		}
    		for(int i=0;i<m;i++){
    			n[k] = i;
    			f(m, n,k+1);
    		}
    	}
    	public static void main(String[] args) {
    		int m = 30;
    		int n = 4;
    		f(m,new int[n],0);
    		print(lis);
    	}
    }

    运行结果:

    1,9,10,12
    2,9,15,16
    2,18,20,24
    10,19,24,27


  • 相关阅读:
    委托的例子,from C# advanced program
    C#线程通信与异步委托
    进程间通信
    线程间通信
    投影转换(AE)
    FTP多任务下载实现类
    堆内存和栈内存详解(转)
    How threads differ from processes
    实现远程FTP特定时间轨道号MODIS数据的搜索
    委托的例子
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3037396.html
Copyright © 2011-2022 走看看