小学奥数题Java编程(2)

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


[img]http://dl2.iteye.com/upload/attachment/0091/4383/6fc35b84-a9e5-3455-a0dc-fd4aa383b5dd.png[/img]


 解:设顶点分别为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;
}
}


输出结果为:
[list]

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]

[/list]

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值