`

小学奥数题Java编程(2)

阅读更多
8.把0—9这十个数字填到右图的圆圈内,使得五条线上的数字和构成一个等差数列,而且这个等差数列的各项之和为55,那么这个等差数列的公差有2种可能的取值。 





 解:设顶点分别为ABCDE,有45+A+B+C+D+E=55, 
所以A+B+C+D+E=10,则除之外的另外5个数(即边上的)为45-10=35, 利用求和公式 5(A1+A1+4D)/2=55, 得A1+2d=11,A1大于等于6,且为奇数, 只能取7或9   这样d只能为1或者2    经试验能填出来  
两种情况  公差d等于1或者2。

package com.ssll.collect.pc;

import java.util.Arrays;
import java.util.List;

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo permutation :");
    //for(List<Integer> list : Permutation.of(Arrays.asList(0,1,2,3,4,5,6,7,8,9)))
      //System.out.println(list);
    for(List<Integer> list : Permutation.of(Arrays.asList(0,1,2,3,4))){
    	for(List<Integer> list2 : Permutation.of(Arrays.asList(5,6,7,8,9))){
    		isArithmeticProgression(list, list2);
        }	
    }
  }
  
  public static int  sum(int i, List<Integer> list1, List<Integer> list2){
	 return i<4? list1.get(i)+list1.get(i+1)+list2.get(i) :list1.get(i)+list1.get(0)+list2.get(i);
  }
  
  public static boolean isArithmeticProgression(List<Integer> list1, List<Integer> list2){
	  int presum =0;
	  int predef = 0;
	  int thissum =0;
	  presum= sum(0, list1,list2);
	  thissum= sum(1, list1,list2);
	  predef = thissum-presum;
	  for(int i=2; i<5; i++){
		  presum = thissum;
		  thissum= sum(i, list1,list2);
		  if(predef!=thissum-presum) return false;
	  }
	  System.out.println("def="+predef+" list1="+list1+" list2="+list2);
	  return true;
  }
}


输出结果为:


    def=1 list1=[0, 1, 2, 3, 4] list2=[8, 7, 6, 5, 9]
    def=0 list1=[0, 2, 4, 1, 3] list2=[9, 5, 6, 7, 8]
    def=0 list1=[0, 3, 1, 4, 2] list2=[8, 7, 6, 5, 9]
    def=1 list1=[0, 3, 2, 1, 4] list2=[6, 5, 8, 7, 9]
    def=-1 list1=[0, 4, 1, 2, 3] list2=[9, 7, 8, 5, 6]
    def=-1 list1=[0, 4, 3, 2, 1] list2=[9, 5, 6, 7, 8]
    def=1 list1=[1, 0, 4, 2, 3] list2=[8, 6, 5, 7, 9]
    def=1 list1=[1, 2, 0, 4, 3] list2=[6, 8, 7, 5, 9]
    def=1 list1=[1, 3, 0, 2, 4] list2=[5, 7, 9, 6, 8]
    def=0 list1=[1, 3, 0, 2, 4] list2=[7, 8, 9, 5, 6]
    def=-1 list1=[1, 3, 2, 4, 0] list2=[9, 7, 5, 6, 8]
    def=-1 list1=[1, 3, 4, 0, 2] list2=[9, 5, 7, 8, 6]
    def=0 list1=[1, 4, 2, 0, 3] list2=[6, 5, 9, 8, 7]
    def=-1 list1=[1, 4, 2, 0, 3] list2=[8, 6, 9, 7, 5]
    def=2 list1=[2, 0, 1, 3, 4] list2=[5, 8, 7, 6, 9]
    def=1 list1=[2, 0, 1, 4, 3] list2=[7, 9, 6, 5, 8]
    def=2 list1=[2, 0, 3, 1, 4] list2=[5, 6, 7, 8, 9]
    def=0 list1=[2, 0, 3, 1, 4] list2=[9, 8, 7, 6, 5]
    def=1 list1=[2, 1, 0, 3, 4] list2=[6, 9, 8, 5, 7]
    def=1 list1=[2, 1, 0, 4, 3] list2=[6, 9, 7, 5, 8]
    def=1 list1=[2, 1, 4, 0, 3] list2=[6, 5, 7, 9, 8]
    def=-1 list1=[2, 3, 0, 4, 1] list2=[8, 9, 7, 5, 6]
    def=-1 list1=[2, 3, 4, 0, 1] list2=[8, 5, 7, 9, 6]
    def=-1 list1=[2, 3, 4, 1, 0] list2=[8, 5, 6, 9, 7]
    def=0 list1=[2, 4, 1, 3, 0] list2=[5, 6, 7, 8, 9]
    def=-2 list1=[2, 4, 1, 3, 0] list2=[9, 8, 7, 6, 5]
    def=-1 list1=[2, 4, 3, 0, 1] list2=[7, 5, 8, 9, 6]
    def=-2 list1=[2, 4, 3, 1, 0] list2=[9, 6, 7, 8, 5]
    def=1 list1=[3, 0, 2, 4, 1] list2=[6, 8, 5, 7, 9]
    def=0 list1=[3, 0, 2, 4, 1] list2=[8, 9, 5, 6, 7]
    def=1 list1=[3, 1, 0, 4, 2] list2=[5, 9, 7, 6, 8]
    def=1 list1=[3, 1, 2, 0, 4] list2=[5, 7, 9, 8, 6]
    def=0 list1=[3, 1, 4, 2, 0] list2=[7, 6, 5, 9, 8]
    def=-1 list1=[3, 1, 4, 2, 0] list2=[9, 7, 5, 8, 6]
    def=-1 list1=[3, 2, 4, 0, 1] list2=[8, 6, 7, 9, 5]
    def=-1 list1=[3, 4, 0, 2, 1] list2=[6, 8, 9, 7, 5]
    def=1 list1=[4, 0, 1, 2, 3] list2=[5, 9, 8, 7, 6]
    def=1 list1=[4, 0, 3, 2, 1] list2=[5, 7, 6, 9, 8]
    def=-1 list1=[4, 1, 2, 3, 0] list2=[8, 9, 6, 7, 5]
    def=0 list1=[4, 1, 3, 0, 2] list2=[6, 7, 8, 9, 5]
    def=0 list1=[4, 2, 0, 3, 1] list2=[5, 9, 8, 7, 6]
    def=-1 list1=[4, 3, 2, 1, 0] list2=[6, 7, 8, 9, 5]



这里面用了来自于 http://blog.csdn.net/raistlic/article/details/7844812
的排列组合类,使得计算大大简化,输出结果中list1为五个顶点上的数,list2为五条边上的数,从结果上可以看出有五个差,0,+1,-1, +2,-2,很多解都仅提到了1和2,这也是这道题的争议之处。

  • 大小: 8.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics