1  有十二个球,大小形状相同,其中有一个是劣质球,或轻或重无法用感觉判断,现有一无刻度的天平,请用三次称量确定那个劣质球。(我想了很长时间,没想出来,朋友说有算法,我也不知道怎么算,求解)2  写出一个测试输入的三个值是三角形的三个边的程序。(这个会写,想看看大家是怎么写的)

解决方案 »

  1.   


    三角形的。。任何2边相加都大于第3边
    Scanner input = new Scanner(System.in);
    int i = input.nextInt();
    int j = input.nextInt();
    int k = input.nextInt();

    if((i+j)>k && (i+k)>j && (k+j)>i
    && i>0 && j>0 && k>0)
    {
    System.out.println("ok");
    }
      

  2.   


    public class Tritangle {
    private double a;
    private double b;
    private double c;

    public Tritangle(double a,double b,double c){
    this.a = a;
    this.b = b;
    this.c = c;
    }

    public boolean isTritangle(){
    if(a+b>c && b+c>a && c+a>b){
    return true;
    }
    return false;
    }

    public static void main(String[] args){
    Tritangle t = new Tritangle(2,3,5);
    System.out.println("isTritangle="+t.isTritangle());
    }
    }
      

  3.   

    改判断条件import java.util.Scanner;public class Tritangle {
    private double a;
    private double b;
    private double c;

    public Tritangle(double a,double b,double c){
    this.a = a;
    this.b = b;
    this.c = c;
    }

    public boolean isTritangle(){
    if(a+b>c && b+c>a && c+a>b && a>0 && b>0 && c>0){
    return true;
    }
    return false;
    }

    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    Tritangle t = new Tritangle(a,b,c);
    System.out.println("isTritangle="+t.isTritangle());
    }
    }
      

  4.   

    http://wenku.baidu.com/view/c67fde7931b765ce0508146b.html
      

  5.   

    为啥没人看我引用的 链接
    我又找了一个链接  http://tieba.baidu.com/p/326192655?pn=1  这里也有,先声明,我自己想不出来的
      

  6.   


    1234,5678,9abc
    1234--5678
    相等:12345678正常,9abc不正常
    123--abc
    相等: 9劣质
    不相等: 
    b--c
    相等: a劣质
    不相等: 根据123--abc的轻重,判断劣质球的轻重\
    不相等:9abc正常
    1235--4abc(abc 替换678;4与5交换)
    相等: 678不正常
    不相等: 
    若平衡发生了变化,说明4,5不同; 则4或5劣质,
    若平衡没有发生变化,说明abc,678,45都是正常的。则123有问题
    二选一,三选一和上面就一样了。
      

  7.   

    Not closing pre-bound Hibernate Session after HibernateTemplate怎么回事
      

  8.   

    [Quote=引用 28 楼 syelove977 的回复:]1 有十二个球,大小形状相同,其中有一个是劣质球,或轻或重无法用感觉判断,现有一无刻度的天平,请用三次称量确定那个劣质球。(我想了很长时间,没想出来,朋友说有算法,我也不知道怎么算,求解)将十二个球,分3组(每组4个),2组对比,轻劣质球(组),如果相同,劣质球(组)为第三组. 
    将劣质球所在的组分为3组球(组1,1,2个),2组对比(1对1)。如果同行。说明劣质球在第3组.
    或轻的就是劣质球
      

  9.   

    第一次题没看清。
    公式:12=1(4)+2(4)+3(4);
    第一次对比:1(4)=2(4)?Y:N   得到1组
    4=1(1)+2(1)+3(2)
    第二次对比:1(1)=2(1)?Y:N  如果结果为1(1)/2(1) 称重结束
    2=1(1)+2(1)
    第三次对比:1(1)=2(1)?Y:N 得到结果1(1)/2(1)
      

  10.   

    1、将12个球分成3堆,分别为(a1、a2、a3、a4),(b1、b2、b3、b4),(c1、c2、c3、c4)2、比较(a1、a2、a3、a4),(b1、b2、b3、b4)      2.1 当(a1、a2、a3、a4)==(b1、b2、b3、b4)时,劣质球在(c1、c2、c3、c4)中;比较c1 和c2            2.1.1当c1!=c2时,劣质球在c1、c2中;            2.1.2当c1==c2时,劣质球在c3、c4;     2.2当(a1、a2、a3、a4)>(b1、b2、b3、b4)时;比较(a1、a2、b1)和(a3、a4、b2),有两种情况           2.2.1当(a1、a2、b1)>(a3、a4、b2),则劣质球在a1,a2,b2中;比较a1,a2                  2.2.1.1当a1>a2,a1为劣质球;                  2.2.1.2当a1==a2,b2为劣质球;                  2.2.1.3当a1<a2,a2为劣质球;           2.2.2当(a1、a2、b1)==(a3、a4、b2),则劣质球在b3,b4中;           2.2.3当(a1、a2、b1)<(a3、a4、b2),则劣质球在a3,a4,b1中;                  2.2.3.1当a3>a4,a3为劣质球;                  2.2.3.2当a3==a4,b1为劣质球;                  2.2.3.3当a3<a4,a4为劣质球;
      

  11.   

    看了半天没往下看,说说我的想法: A > B :A比B沉   1~0 ab 12个球
    1) 1234=5678  -> 123=90a    -> b有问题                    
                    -> 123 > 90a  -> 9=0 ->  a异常-轻             
                                -> 9>0 ->  0异常-轻             
    2) 1234>5678  -> 4567=890a  -> 1=2 ->  3异常-重                                      
                                -> 1>2 ->  1异常-重                            
                    -> 4567>890a  -> 1=4 ->  8异常-轻
                                    -> 4>1 ->  4异常-重
                    -> 4567<890a  -> 5=6 ->  7异常-轻
                                    -> 5>6 ->  6异常-轻                                                      
      

  12.   

    public class Test {
    private static  int[]  ll ;
    private static  int    l ;
    private static  int  j   ;
    private static int    index;
    private static  String  type;
    public static void main(String[] args){
    init();
    start();
    showResult();
    }
    private static void init(){
    l  = 0;
    ll = new int[12];
    for(int i = 0 ; i<12 ; i++){
    ll[i] = 5;
    }
    j = new Double(Math.random()*12).intValue();
    //j = 9;
    while(ll[j]==5){
    ll[j] = new Double(Math.random()*10).intValue()+1;
    }
    for(int i = 0 ; i<12 ; i++){
    System.out.print(ll[i]+"--");
    }
    System.out.println("初始化完成");
    }
    private static void start(){
    switch(doCompare(ll[0]+ll[1]+ll[2]+ll[3],ll[4]+ll[5]+ll[6]+ll[7],"1,2,3,4","5,6,7,8")){
    case 0 : {
    switch(doCompare(ll[0]+ll[1],ll[8]+ll[9],"1,2","9,10")){
    case 0 :{
    switch(doCompare(ll[0],ll[10],"1","11")){
    case 0  : index = 12; type="未能判断轻重"; break;
    case -1 : index = 11; type="重"; break;
    case 1  : index = 11; type="轻"; break;
    }
    break;
    }
    case -1 :{
    switch(doCompare(ll[0],ll[8],"1","9")){
    case 0  : index = 10; type="重"; break;
    case -1 : index = 9; type="重"; break;
    case 1  : System.out.println("此为异常状况!");System.exit(0);
    }
    break;
    }
    case 1 :{
    switch(doCompare(ll[0],ll[8],"1","9")){
    case 0  : index = 10; type="轻"; break;
    case -1 : System.out.println("此为异常状况!");System.exit(0);
    case 1  : index = 9; type="轻"; break;
    }
    break;
    }
    }
    break;
    }
    case -1 :{
    switch(doCompare(ll[3]+ll[4]+ll[5]+ll[6],ll[7]+ll[8]+ll[9]+ll[10],"4,5,6,7","8,9,10,11")){
    case 0 : {
    switch(doCompare(ll[0],ll[1],"1","2")){
    case 0  : index = 3 ; type="轻" ; break;
    case -1 : index = 1 ; type="轻" ; break;
    case 1  : index = 2 ; type="轻" ; break;
    }
    break;
    }
    case -1 : {
    switch(doCompare(ll[0],ll[3],"1","4")){
    case 0  : index = 8 ; type="重" ; break;
    case -1 : System.out.println("此为异常状况!");System.exit(0);
    case 1  : index = 4 ; type="轻" ; break;
    }
    break;
    }
    case 1 : {
    switch(doCompare(ll[4],ll[5],"5","6")){
    case 0  : index = 7 ; type="重" ; break;
    case -1 : index = 6 ; type="重" ; break;
    case 1  : index = 5 ; type="重" ; break; 
    }
    break;
    }
    }
    break;
    }
    case 1 :{
    switch(doCompare(ll[3]+ll[4]+ll[5]+ll[6],ll[7]+ll[8]+ll[9]+ll[10],"4,5,6,7","8,9,10,11")){
    case 0 : {
    switch(doCompare(ll[0],ll[1],"1","2")){
    case 0  : index = 3 ; type="重" ; break;
    case -1 : index = 2 ; type="重" ; break;
    case 1  : index = 1 ; type="重" ; break;
    }
    break;
    }
    case -1 : {
    switch(doCompare(ll[4],ll[5],"5","6")){
    case 0  : index = 7 ; type="轻" ; break;
    case -1 : index = 5 ; type="轻" ; break;
    case 1  : index = 6 ; type="轻" ; break;
    }
    break;
    }
    case 1 : {
    switch(doCompare(ll[0],ll[3],"1","4")){
    case 0  : index = 8 ; type="轻" ; break;
    case -1 : index = 4 ; type="重" ; break;
    case 1  : System.out.println("此为异常状况!");System.exit(0); 
    }
    break;
    }
    }
    break;
    }
    }
    }
    private static int doCompare(int a,int b,String aa, String bb){
    //System.out.println
    System.out.println("----左侧放入"+aa+"球,右侧放入"+bb+"球;");
    System.out.print("进行第"+(++l)+"次比较,");
    if(a==b){
    System.out.println("左右平衡;");
    return 0;
    }else if(a>b){
    System.out.println("左面比右面重;");
    return 1;
    }else{
    System.out.println("左面比右面轻;");
    return -1;
    }
    }
    private static void showResult(){
    //System.out.println
    System.out.println("--计算结果为:第"+index+"球劣质("+type+"),应为5,实际为"+ll[index-1]);
    if(j!=--index){
    System.out.println("计算出现错误,跳出!");
    System.exit(0); 
    }
    }
    }